Is it possible to force CTFE?

Timon Gehr timon.gehr at gmx.ch
Sun Jun 10 03:23:08 PDT 2012


On 06/10/2012 09:04 AM, Tommi wrote:
> Three related questions:
>
> 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 there is not. You could use a template that calls a private function 
at compile time instead. What is your use case?

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

This has been discussed before, but there is not.


1-2) could be introduced later when D gets AST macros.

> 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);

Yes, DMD/GDC/LDC should be able to do this to different extents.


More information about the Digitalmars-d-learn mailing list