Structures and CTFE
bauss
jj_1337 at live.dk
Mon Sep 3 13:52:24 UTC 2018
On Monday, 3 September 2018 at 13:39:25 UTC, agorkvmh wrote:
> Hi all,
>
> Running this:
> ---
> static assert(Foo(1).pos == 2);
>
>
> struct Foo
> {
> this(int i){ advance(); }
>
> size_t pos =1;
>
> void advance()
> {
> pragma(msg, pos);
> pos = pos+1;
> pragma(msg, pos);
> }
> }
>
> dmd -o- -unittest source/pgs/parser.d
> 1LU
> 1LU
> ---
>
> The static assert passes, but why the second pragma print '1'?
>
> Thanks
It prints 1, because pragma(msg) is called when the compiler is
analyzing the code through the semantic process and not when the
body of the function is executed during CTFE.
And since "pos = pos+1" is a runtime construct (technically) then
the expression is ignored, unless the function is called during
CTFE, but by the time the function is called during CTFE then the
pragma(msg) has already been executed.
More information about the Digitalmars-d-learn
mailing list