write a function template specialisation that tests if an argument is known at compile time

Cecil Ward d at cecilward.com
Sat Aug 11 05:17:51 UTC 2018


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.

Basically there is a fast path for certain known values of a 
(second in this case) argument where the compiler could produce 
superb trivial code or where I can work out a shortcut myself. 
for example myfunc( x, 0 ) == 0 and myfunc( x, -1 ) == x and 
various other good things, and for some values of mask the thing 
behaves like an AND operation so I want the compiler to just 
generate that.

The default slow path where the arg is unknown involves calling 
asm so the compiler cannot use its intelligence as it does not 
know the detailed semantics.

Also:

To add further complication: if both arguments of myfunc() are 
known at compile-time, then I definitely want to take an 
alternative path because then I can apply CTFE and calculate a 
compile-time result.


More information about the Digitalmars-d-learn mailing list