Why this code can't take advantage from CTFE?

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 3 07:59:05 PST 2016


On 03.02.2016 16:34, Andrea Fontana wrote:
> void main()
> {
>          enum first = very_very_long_function(10);
>          writeln("First is ", first);
>
>          auto second = very_very_long_function(12);
>          writeln("Second is ", second);
>
>          auto third = first;
>          third += 1;
>          writeln("Third is ", third);
> }
>
> Why second init doesn't work with CTFE? It could be something like
> third, but with one less copy. What am I missing?

The compiler doesn't try to apply CTFE everywhere, because it could take 
forever. So CTFE is only done when the programmer requests it by using a 
function in a static context.

The compiler is still free to precompute the value at compile time, if 
it thinks that's a good idea, but that's just an optimization. And while 
it's evaluation of a function at compile time, it's not CTFE in the 
sense of __ctfe.


More information about the Digitalmars-d-learn mailing list