Is (++i)++
undefined behavior? Is it possible that the side effect of prefix increment happens after retrieving the incremented object for postfix increment to operate on? That would seem strange to me.
My gut feeling says this is undefined in C++03 and well-defined in C++11. Am I right?
My gut feeling says this is undefined in C++03 and well-defined in C++0x.
Yes you are right. The behaviour is undefined in C++03 because you are trying to modify i
more than once between two sequence points.
The behaviour is well defined in C++0x because (++i)++
is equivalent to (i += 1)++
. The side effects of the +=
operator are sequenced relative to ++
(post increment) and so the behaviour is well defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With