See Brian's answer for simpler terms.
This is legal because the C ++ standard says so, the emphasis is mine. We are following the C ++ 14 project
5.3.2 Increment and decrement [expr.pre.incr]
The ++ prefix operand is modified by adding 1 or true if it is a bool (this usage is deprecated). An operand may be a modifiable naming convention. The type of operand must be arithmetic or a pointer to a fully defined type of object. The result is an updated operand; it is an lvalue , and it is a bit field, if the operand is a bit field. If x is not of type bool, the expression ++ x is equivalent to x + = 1
So this is completely legal
#include <iostream> using namespace std; int main(){ int x =8; int y = ++ ++ ++ ++ ++ ++ ++ ++ x; cout << x << " " << y; }
Exit
16 16
1.9 Program Execution [intro.execution]
15 ... If the side effect of a scalar object is independent of another side effect on the same scalar object or calculating a value using the value of the same scalar object , the behavior is undefined ....
And he attached an example:
void f(int, int); void g(int i, int* v) { i = v[i++]; // the behavior is undefined i = 7, i++, i++; // i becomes 9 i = i++ + 1; // the behavior is undefined i = i + 1; // the value of i is incremented f(i = -1, i = -1); // the behavior is undefined }
Whizti
source share