问题
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?
回答1:
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.
回答2:
This is an undefined behaviour since i
is being modified more than once between two sequence points.
来源:https://stackoverflow.com/questions/4347010/is-i-undefined-behavior