Checking if CTFE is used?

berni someone at somemail.de
Tue Dec 18 14:23:38 UTC 2018


On Tuesday, 18 December 2018 at 13:53:01 UTC, Stefan Koch wrote:
> Why would you need to know?

Well, first out of curiosity. But there are also other reasons: 
I've got a large immutable array. Without CTFE I'd use some php 
script and add it as a large literal. With CTFE I don't need that 
php script nor do I need to put that large literal in the source 
code. But I need to know, that indeed the array is calculated by 
the compiler, else the solution with the php script would result 
in faster code.

> if(__ctfe) assert(0, "Yep, CTFE used");

That seems to work, but unfortunately, my script seems not to be 
evaluated at compile time... Here is a small example, similar to 
my code, where CTFE is not used:

> import std.stdio;
>
> class A
> {
>     static immutable int[4] clue;
> 
>     static this()
>     {
>         if(__ctfe) assert(0, "Yep, CTFE used");
>         foreach (i;0..4) clue[i] = i;
>     }
> }

> void main()
> {
>     auto tmp = new A();
>     writeln(tmp.clue[2]);
> }

Can you help me, finding the problem here?


More information about the Digitalmars-d-learn mailing list