Is (++i)++ undefined behavior?

寵の児 提交于 2020-01-01 09:42:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!