I have been reading about the template system in the D language and came upon a unusual construct, static if.
From what I managed to grasp it is evaluated at compile time, but from what I have searched, the example shown here did not quite enlighten me.
template Factorial(ulong n)
{
static if(n < 2)
const Factorial = 1;
else
const Factorial = n * Factorial!(n - 1);
}
What does static if do, and when should I use it?
the D
static ifis the base for “conditional compilation”, and plays an important role wherever a compile time decision about a variant of a code must be taken.Since D doesn’t have a preprocessor, things like
can become
similarly, metaprogramming can happen also via static if:
can be