static if or version for CTFE

Jarrett Billingsley kb3ctd2 at yahoo.com
Wed Aug 27 22:12:51 PDT 2008


"bmeck" <bmeck at stedwards.edu> wrote in message 
news:g956t5$18vg$1 at digitalmars.com...
>I have a situation where I want to use a template if CTFE is possible and a 
>function if it can only be processed at runtime. is there a version or 
>static if which can check this?

I think you may be able to do this in D2 with __traits(compiles).  I can't 
test it, but you could try writing some code that would mixin a constant 
declaration with the expression you wish to evaluate.  I.e.

static if(__traits(compiles, mixin("enum x = yourExpression;")))
    // it can be evaluated at compile time
else
    // at runtime, or not at all if it's just an error

In D1 I don't think there's any way. 





More information about the Digitalmars-d mailing list