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

claptrap clap at trap.com
Sun Mar 8 03:30:55 UTC 2026


On Sunday, 8 March 2026 at 03:00:58 UTC, Timon Gehr wrote:
> On 3/8/26 03:28, claptrap wrote:
>> ```
>> module test;
>> import std.stdio;
>> 
>> void foo(int pos)
>> {
>>      writeln("inside foo : ",pos);
>> }
>> 
>> void main()
>> {
>>      writeln("++");
>>      int p = 0;
>>      foo(p++);
>>      writeln("after foo : ",p);
>> 
>>      writeln("+=1");
>>      p = 0;
>>      foo(p+=1);
>>      writeln("after foo : ",p);
>> }
>> ```
>> 
>> prints:
>> 
>> ++
>> inside foo : 0
>> after foo : 1
>> +=1
>> inside foo : 1
>> after foo : 1
>> 
>> seems inconsistent to me, probably stuck with it now though I 
>> guess?
>> ...
>
>
> `++p` and `p++` are different. If you want to match `p+=1` you 
> should go with `++p`.

I wasn't using pre increment, I was using post, I just mistakenly 
thought (or forgot) p+=2 would be post increment like p++.

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.





More information about the Digitalmars-d mailing list