In C++,
i = ++++j;
works fine in the code but when I use,
i = j++++;
I receive the following error:
Post-increment requires that the operand should be a modifiable lvalue but the result of post-increment is a prvalue("pure" rvalue) which is not modifiable, this diagram shows what is going on:
i = (j++)++ ;
^ ^
| |
| Result is a prvalue, not a valid operand for subsequent post-increment
Modifiable lvalue
Understanding lvalues and rvalues in C and C++ is a good place to start if you need to understand the difference between lvalues and rvalues.
From the draft C++ standard section 5.2.6
Increment and decrement [expr.post.incr] paragraph 1 says(emphasis is mine in this an subsequent quotes):
The value of a postfix ++ expression is the value of its operand. [ Note: the value obtained is a copy of the original value —end note ] The operand shall be a modifiable lvalue. [..] The result is a prvalue.
Update
I reworked my language on undefined behavior since there is a difference here with respect to C++03 and C++11.
Although the first expression shown:
i = ++++j ;
does not generate an error but if this is C++03 and j
is a fundamental type this is undefined behavior since modifying it's value more than once within a sequence point is undefined. The relevant section in an older draft standard would be section 5
Expressions paragraph 4 which says:
[...]Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.
and it gives some examples, one of which is as follows:
i = ++i + 1; // the behavior is undefined
In C++11 the language changes to the side-effect on the same scalar object is unsequenced relative to the another side effect on the same object then the behavior is undefined. So this is actually well defined in C++11, in section 1.9
Program execution paragraph 15 says:
Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. [...] If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.
Using post- and pre- increment in this way does not lead to readable(maintainable) code in both cases using j +=2
either before or after the assignment statement would have sufficed