Comma operator and void expression

安稳与你 提交于 2019-12-05 03:08:47

The C spec I have (C99 TC3) says

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

The value of an expression that's said to be "evaluated as a void expression" is discarded. So in the C case, there is no undefined behavior. It may have been different in old C (some details are, if I remember correctly).

The situation for C++ is slightly different than for C, because C++ supports class objects with constructor and destructors as return values and having them operate on uninitialized memory can't be guaranteed to work well. Perhaps this contributed to the different rules for C++.

It's undefined behavior.

The evaluation of the left expression results in flowing off the end of a value-returning function with no return. Just because the value is discarded doesn't mean the evaluation never happened.

It's clearly undefined. C99 §6.3.2.2 says, "(A void expression is evaluated for its side effects.)" So the function is evaluated and does flow off the end. There's no get out of jail free card.

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