Determine if CTFE or RT
Mr.Bingo
Bingo at Namo.com
Sun Jun 24 14:43:09 UTC 2018
let is(CTFE == x) mean that x is a compile time constant. CTFE(x)
converts a x to this compile time constant. Passing any compile
time constant essentially turns the variable in to a compile time
constant(effectively turns it in to a template with template
parameter)
void foo(size_t i)
{
static if (is(CTFE == i))
{
pragma(msg, CTFE(i));
} else
{
writeln(i);
}
}
which, when called with a compile time constant acts effectively
like
void foo(size_t i)()
{
pragma(msg, i);
}
so
foo(1), being a CTFE'able, triggers the pragma, while foo(i) for
volatile i triggers the writeln.
More information about the Digitalmars-d-learn
mailing list