Any reason why ++1 is not folded to a new constant?

Basile B. b2.temp at gmx.com
Tue Oct 20 13:16:53 UTC 2020


On Tuesday, 20 October 2020 at 12:41:48 UTC, Basile B. wrote:
> I've reached a similar problem in another language and I wanted 
> to see what is the D policy. I've been surprised by the result. 
> It seems that there's no special case for compile-time-only 
> values, eg this case of RValue:
> ---
> void main()
> {
>     writeln(++1); // NG: cannot modify constant `1`
> }
> ---
>
> is there any reasons why ++1 is not optimized to 2 ?
>
> Thanks for the enlightment.

well my first guess is that it's because it's lowered to a binass 
operation before optimization.

when you think that the expression optimizer sees

   `writeln(++1)`

actually it sees

   `writeln(1 += 1)`

hence the error...


More information about the Digitalmars-d mailing list