逗号运算符和 void 表达式
我遇到了这个代码片段 1
int return_printChar1()
{
// code
// oops! no return statement
}
int return_printChar2()
{
// code
return printf("Return");
}
int main()
{
int i;
// some more code
i = return_printChar2();
if((return_printChar1(),i))
{
printf ("Gotcha");
}
}
1: This is not a real life example.
我的问题是“代码片段的行为在 C 中定义良好吗? C++?”
我的看法:
在 C 中,行为定义良好,因为 6.5.17
说
逗号运算符的左操作数被计算为void表达式;评估后有一个序列点
该行为已明确定义,因为 5.18
说
一对由逗号分隔的表达式从左到右进行计算,左侧表达式的值被丢弃。
然而 C++03(在 6.6.3
节中)也说
从函数末尾流出相当于没有值的返回;这会导致返回值函数中出现未定义的行为。
同样在C中
如果控制到达非 void 函数(
main()
除外)的末尾 (}
),则行为未定义。
因此,考虑到所有这些点,我无法判断实际行为。大家觉得怎么样?
PS:如果您认为这个问题毫无用处并且您有更好的事情要做,请帮助自己:D。
I came across this code snippet 1
int return_printChar1()
{
// code
// oops! no return statement
}
int return_printChar2()
{
// code
return printf("Return");
}
int main()
{
int i;
// some more code
i = return_printChar2();
if((return_printChar1(),i))
{
printf ("Gotcha");
}
}
1: This is not a real life example.
My question is "Is the behaviour of the code snippet well defined in C and C++?"
My take:
In C the behaviour is well defined because 6.5.17
says
The left operand of a comma operator is evaluated as a void expression; there is a sequence point after its evaluation
In C++03 the behaviour is well defined because 5.18
says
A pair of expressions separated by a comma is evaluated left-to-right and the value of the left expression is discarded.
However C++03 (in section 6.6.3
) also says that
Flowing off the end of a function is equivalent to a returnwith no value; this results in undefined behavior in a value-returning function.
Similarly in C
If control reaches end (
}
) of non-void function (exceptmain()
) the behaviour is undefined.
So taking all these points into consideration I can't judge the actual behaviour. What do you people think?
P.S: If you think the question is useless and you have got better things to do, help yourself: D.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的 C 规范 (C99 TC3) 说
被称为“计算为空表达式”的表达式的值将被丢弃。所以在 C 的情况下,不存在未定义的行为。在旧的 C 中可能有所不同(如果我没记错的话,有些细节是不同的)。
C++ 的情况与 C 略有不同,因为 C++ 支持将构造函数和析构函数作为返回值的类对象,并且不能保证它们在未初始化的内存上运行良好。也许这导致了 C++ 的不同规则。
The C spec I have (C99 TC3) says
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.
显然是未定义的。 C99 §6.3.2.2 说:“(对 void 表达式求值其副作用。)”因此,函数被求值并且确实从末尾流出。没有出狱卡。
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.