i++ question

May I know why i is not 2 but 1? Thank you.
1
2
3
i=1;
i=i++;
cout<<i<<endl;
Assuming that i is a scalar like int

In C++17, after i = i++ ; the value of i would be 1.
(The value computation and side effect of the right hand side of the assignment is sequenced before the value computation and side effect of the left hand side of the assignment)

Prior to C++17, i = i++ ; would engender undefined behaviour.
(Side effects of the right hand side of the assignment and the left hand side of the assignment are unsequenced.)

More information: http://en.cppreference.com/w/cpp/language/eval_order
Registered users can post here. Sign in or register to post.