__traits getMember is context sensetive?

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 14 03:29:08 PDT 2015


On Sunday, 14 June 2015 at 10:10:51 UTC, ketmar wrote:
> i.e. when it need a value in compile time. the interpreter is 
> invoked, it evaluates (interprets) the given code (function or 
> template instantiation), and then it returns result (or raises 
> an error).

One important thing I didn't see stated clearly by anyone in here:

CTFE may run at compile time but it follows the same rules as run 
time evaluation (plus some restrictions).

This means, you can't use dynamic values (e.g. function 
parameters) in static contexts (e.g. __traits).

Example:
----
int f(int x)
{
     if (__ctfe)
     {
         enum e = x; /* nope, not even during CTFE */
         alias t = some_template!x; /* nope */
         pragma(msg, x); /* nope */
     }
	return x;
}

enum result = f(1); /* CTFE-ing f */
----



More information about the Digitalmars-d-learn mailing list