"++" vs "+=" on function parameter

user1234 user1234 at 12.de
Sun Mar 8 10:43:16 UTC 2026


On Sunday, 8 March 2026 at 10:26:54 UTC, claptrap wrote:
> On Sunday, 8 March 2026 at 10:16:56 UTC, user1234 wrote:
>> On Sunday, 8 March 2026 at 03:30:55 UTC, claptrap wrote:
>>> [...]
>>> Just seems like p++ should behave like p+=N for parameters, I 
>>> mean if you had never seen that syntax before you would 
>>> probably assume similar behaviour.
>>
>> 1. This would break code
>
> agreed
>
>> 2. that would be inconsistent with the C version
>
> agreed
>
>> 3. that would be globally inconsistent with the definition, 
>> that can be reduced to "return the value of the sub-expression 
>> before the side-effect".
>
> Doesnt that mean "p++" is inconsistent? Arn't "p++" and "p+=1" 
> essentially shorthand for "p=p+1"

No. The PostIncr yields a load (so a **rvalue**) that's performed 
before the side-effect. The AddAssign version yields a **lvalue**.

So using a pseudo IR, postIncr is:

```
%0 = ...            ; let's say it's a global or an alloca, so a 
pointer
%1 = load i32 %0    ; rvalue before the side effect
%2 = add %1, i32 1  ; new rvalue
store %0, %2        ; store to the lvalue
return %1           ; before the side effect
```

while AddAssign is

```
%0 = ...
%1 = load i32 %0
%2 = add %1, 1
store %0, %2
return %0        ; return the lvalue
```

That's essentially why the post-inc/dec cant be chained (after 
the first you cant load anymore).


More information about the Digitalmars-d mailing list