www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to detect ctfe

reply "Vlad Levenfeld" <vlevenfeld gmail.com> writes:
Is there any way that I can detect whether or not a function is 
being evaluated at compile time? Specifically I want to switch 
between to use/not to use memoize!func without having to figure 
out when its CTFE-able and calling it with different syntax.
May 31 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Vlad Levenfeld:

 Is there any way that I can detect whether or not a function is 
 being evaluated at compile time? Specifically I want to switch 
 between to use/not to use memoize!func without having to figure 
 out when its CTFE-able and calling it with different syntax.
Calling it with a different syntax seems not easy to do. But there is a run-time variable __ctfe that can be used to disable/enable the memoization. Bye, bearophile
May 31 2014
prev sibling parent reply "Philpax" <phillip philliplarkson.com> writes:
__ctfe can be used for this purpose:

"The __ctfe boolean pseudo-variable, which evaluates to true at 
compile time, but false at run time, can be used to provide an 
alternative execution path to avoid operations which are 
forbidden at compile time. Every usage of __ctfe is evaluated 
before code generation and therefore has no run-time cost, even 
if no optimizer is used." ( http://dlang.org/function.html )
May 31 2014
parent Philippe Sigaud via Digitalmars-d-learn writes:
But let's keep in mind it's a *runtime* value. You cannot use it at
compile-time as a boolean for a 'static if'.
So:

if (__ctfe) // yes
{
    // compile-time path
}
else
{
    // runtime path
}

But not:

static if (__ctfe) // no
{
    // compile-time path, including new global declarations
}
else
{
    // runtime path
}

Why would you want to do that? I needed an associative array for my
code. AA don't work that well at compile-time. So I wanted to replace
them with my own Associative struct, maybe less efficient/generic, but
that works at CT. The idea was:

static if (__ctfe)
    alias Type = Associative!(string, string);
else
    alias Type = string[string];

// then, use Type, whatever the path.
Jun 01 2014