Any reason why ++1 is not folded to a new constant?
Walter Bright
newshound2 at digitalmars.com
Wed Oct 21 21:49:11 UTC 2020
On 10/20/2020 5:41 AM, 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 ?
Because ++ is supposed to operate on an lvalue, and `1` is an rvalue. `++1` is
nonsense.
I don't see the point to adding a special case for it - special cases are warts
and need strong justifications to add.
More information about the Digitalmars-d
mailing list