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

Patrick Schluter Patrick.Schluter at bbox.fr
Thu Oct 22 09:49:32 UTC 2020


On Wednesday, 21 October 2020 at 21:49:11 UTC, Walter Bright 
wrote:
> 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.

The question is then: why did it compile?
gcc gives
    error: lvalue required as increment operand

when one tries it in C.


More information about the Digitalmars-d mailing list