Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is (++i)++ undefined behavior?

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?

like image 788
fredoverflow Avatar asked Dec 03 '10 15:12

fredoverflow


1 Answers

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.

like image 105
Prasoon Saurav Avatar answered Sep 22 '22 06:09

Prasoon Saurav