On Saturday, 11 August 2018 at 18:11:15 UTC, Paul Backus wrote:
On Saturday, 11 August 2018 at 05:17:51 UTC, Cecil Ward wrote:
T myfunc(T)( T x, uint mask )
if ( mask == 3 )
{
return fast_func( x, mask );
}
but of course this doesn't work because mask is not known at
compile-time. so I wondered if there is a way to do something
like static if ( isKnownAtCompileTime( mask ) ) but that would
not necessarily help me and probably isn't the right way.
You can create an overload where `mask` is passed as a template
parameter:
T myfunc(uint mask, T)(T x)
{
static if(mask == 3) {
return fast_func(x, mask);
} else {
return func(x, mask);
}
}
The same technique is used by `std.format.format` in the
standard library to pass a format string that's known at
compile time.
Paul, what would the calls look like?
I am about to misunderstand things completely so here goes :-)
It would be a bit kludgy having to switch from one calling syntax
to another, putting the mask argument in the template parameters
or in the normal position. Or have I misunderstood? And if the
caller did not use the right call syntax variant then the
optimisation would not happen. Thing is, as it is the details are
nicely hidden and the caller does not even need to thing about
the fact that an (eponymous) template is being used.