how to detect ctfe

Philippe Sigaud via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 1 00:34:30 PDT 2014


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.


More information about the Digitalmars-d-learn mailing list