www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - "++" vs "+=" on function parameter

reply claptrap <clap trap.com> writes:
```
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?

probably bad form to use "+=" on a parameter, might be a 
candidate for depreciation?
Mar 07
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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`.
Mar 07
parent reply claptrap <clap trap.com> writes:
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.
Mar 07
parent reply user1234 <user12134 12.de> writes:
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 2. that would be inconsistent with the C version 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".
Mar 08
parent reply claptrap <clap trap.com> writes:
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"
Mar 08
next sibling parent user1234 <user1234 12.de> writes:
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).
Mar 08
prev sibling parent reply Nick Treleaven <nick geany.org> writes:
On Sunday, 8 March 2026 at 10:26:54 UTC, claptrap wrote:
 Doesnt that mean "p++" is inconsistent? Arn't "p++" and "p+=1" 
 essentially shorthand for "p=p+1"
From https://dlang.org/spec/expression.html#order-increment: ```d Expression Equivalent ++expr ((expr) += 1) expr++ (ref x){auto t = x; ++x; return t;}(expr) ```
Mar 08
parent reply Sergey <kornburn yandex.ru> writes:
On Sunday, 8 March 2026 at 12:42:19 UTC, Nick Treleaven wrote:
 On Sunday, 8 March 2026 at 10:26:54 UTC, claptrap wrote:
 Doesnt that mean "p++" is inconsistent? Arn't "p++" and "p+=1" 
 essentially shorthand for "p=p+1"
From https://dlang.org/spec/expression.html#order-increment: ```d Expression Equivalent ++expr ((expr) += 1) expr++ (ref x){auto t = x; ++x; return t;}(expr) ```
I'm amazed how we have the whole discussion about obvious thing that every high school student who ever learned any C-like language knows :)
Mar 08
parent user1234 <user1234 12.de> writes:
On Sunday, 8 March 2026 at 12:50:35 UTC, Sergey wrote:
 On Sunday, 8 March 2026 at 12:42:19 UTC, Nick Treleaven wrote:
 On Sunday, 8 March 2026 at 10:26:54 UTC, claptrap wrote:
 Doesnt that mean "p++" is inconsistent? Arn't "p++" and 
 "p+=1" essentially shorthand for "p=p+1"
From https://dlang.org/spec/expression.html#order-increment: ```d Expression Equivalent ++expr ((expr) += 1) expr++ (ref x){auto t = x; ++x; return t;}(expr) ```
I'm amazed how we have the whole discussion about obvious thing that every high school student who ever learned any C-like language knows :)
that's not necessarily a high schooler thing. The point is "do you need the rvalue before the side effect". There is an interesting discussion here https://softwareengineering.stackexchange.com/questions/43067/why-do-we-have-postfix-increment So apparently the postfix version matches to an instruction in the PDP arch ? cool. However even with the common x86 arch you're pretty sure that the result will stand in a register, which opens doors to optimz. But seriously ```d (ref x){auto t = x; ++x; return t;}(expr) ``` does not express well that, i.e the "value category" (boring topic)
Mar 09
prev sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Sunday, 8 March 2026 at 02:28:31 UTC, claptrap wrote:
 seems inconsistent to me, probably stuck with it now though I 
 guess?
The inconsistency is with left handed assignment; your doing all this math with the right hand of the ='s then you swap back the the left hand to commit it
Mar 09