1

When me and my friend were preparing for exam, my friend said that x+++; is the same as x+=3;

It is not true but is x++++; same as x+=1; or is (x++)++;? Could I generalize it? I.e. x++++++++++++++; or ((((((x++)++)++)++)++)++)++; is equivalent to x+=7;

Maybe it's completely wrong and it is true for ++++++x; or ++(++(++x)); equivalent to x+=3;

Also it should generalize to --x; and x--;

JRBros
  • 43
  • 6
  • 9
    Why didn't you try this in a compiler before posting this question? Don't listen to your friend, listen to your compiler. – Lundin Apr 21 '22 at 14:03
  • 2
    @Lundin: On the other hand, compilers have been known to accept non-standard code. – Fred Larson Apr 21 '22 at 14:04
  • 2
    ^ Here's [a good site for playing w/compilers](https://godbolt.org/) be sure to select whatever compiler the course uses – 0x263A Apr 21 '22 at 14:04
  • My friend just made fun. But this emerged from my deep thoughts. I currently don't have access to a compiler – JRBros Apr 21 '22 at 14:04
  • 5
    If you need more than `++`, then use `+= value`. – NathanOliver Apr 21 '22 at 14:06
  • 2
    It depends on `x`. For fundamental types like `int` the expression `x++` is an rvalue and you can't increment it further but `++x` is an lvalue and can be stacked. For class types it depends on how they implement the operator. – François Andrieux Apr 21 '22 at 14:07
  • 2
    For `(x++)++;` and similar, recall that the postincrement operator returns the value previously held in `x` prior to incrementing it. So `(x++)` is actually a pure rvalue and you can't call `operator++` on it anyway--it'd be like expecting `5++` to be meaningful. `(++x)` returns an lvalue reference to `x` and can be chained. – Nathan Pierson Apr 21 '22 at 14:07
  • 1
    The post-increment operator returns a **prvalue** copy of the original operand, and prvalues cannot be used as input to that operator; so, stacking **post**-increment is not possible. However, the **pre**-increment operator returns a reference to the (now modified) operand, so those can be stacked. Thus, `++++++x;` is equivalent to `x += 3;`. – Adrian Mole Apr 21 '22 at 14:08
  • 1
    related/dupe: https://stackoverflow.com/questions/28859460/chain-increment-operators – NathanOliver Apr 21 '22 at 14:08
  • @NathanOliver: Related, I suppose, but that's about C#. – Fred Larson Apr 21 '22 at 14:09
  • Try this: `int ZZ, Z2 = ZZ = 22, SS = - - ZZ -- - - -- -- Z2;` can you predict what `std::cout << SS << std::endl;` will show, after that? – Adrian Mole Apr 21 '22 at 14:11
  • 1
    @JRBros If you have access to the internet, you have access to a compiler. Godbolt.org for example. – Lundin Apr 21 '22 at 14:24
  • No my friend just didnt understand it well. – JRBros Apr 21 '22 at 14:27
  • Goldbolt.org is not good in small screens – JRBros Apr 21 '22 at 14:27
  • 1
    I would be more interested in this case: `int i = 4; cout>>++i++>>endl;`, is it 4 or 5? :-) – Dominique Apr 21 '22 at 14:30
  • I think it probably won't work work since postfix don't go well in stacking – JRBros Apr 21 '22 at 14:31
  • @FredLarson Lol. Didn't even notice that. google lied to me. – NathanOliver Apr 21 '22 at 14:41

1 Answers1

2

The behavior of your program can be understood using the following rules from the standard.

From lex.pptoken#3.3:

Otherwise, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token, even if that would cause further lexical analysis to fail, except that a header-name is only formed within a #include directive.

And from lex.pptoken#5:

[ Example: The program fragment x+++++y is parsed as x ++ ++ + y, which, if x and y have integral types, violates a constraint on increment operators, even though the parse x ++ + ++ y might yield a correct expression.  — end example ]


is x++++; same as x+=1;

Using the statement quoted above, x++++ will be parsed as x++ ++.

But note that from increment/decrement operator's documentation:

The operand expr of a built-in postfix increment or decrement operator must be a modifiable (non-const) lvalue of non-boolean (since C++17) arithmetic type or pointer to completely-defined object type. The result is prvalue copy of the original value of the operand.

That means the result of x++ will a prvalue. Thus the next postfix increment ++ cannot be applied on that prvalue since it requires an lvalue. Hence, x++++ will not compile.

Similarly, you can use the above quoted statements to understand the behavior of other examples in your snippet.

Jason
  • 36,170
  • 5
  • 26
  • 60