Is it possible to force CTFE?

jerro a at a.com
Sun Jun 10 03:16:22 PDT 2012


> 1) Is there a way to force a function to be always executed at 
> compile time (when it's possible to do so) no matter what 
> context it's called in?

No, but you could wrap it in a template to force it to always
execute at compile time. Of course it could then only be called
at compile time.

> 2) Is it possible to specialize a function based on whether or 
> not the parameter that was passed in is a compile time constant?

No.

> 3) Does any D compiler currently optimize out a conditional 
> branch which _can_ be evaluated at compile time (but which 
> isn't forced into CTFE)? Like:
>
> int getValue(bool b)
> {
>     return b ? 123 : 456;
> }
>
> //...
>    auto value = getValue(true);

DMD, LDC and GDC all do this when compiling with optimizations
turned on. They all compile these functions to exactly the
same code:

auto foo()
{
     return getValue(true);
}

auto bar()
{
     return 123;
}






More information about the Digitalmars-d-learn mailing list