对于非 void 函数没有 return 语句,其中控制永远不会结束,这是未定义的行为吗?
我正在使用此处列出的书籍学习 C++。特别是,我读到从非 void 函数末尾流出是未定义的行为。然后我看了这个答案,上面写着:
在 C++ 中,仅从值返回函数的末尾流出始终是未定义的行为(无论调用代码是否使用函数的结果)。在 C 中,仅当调用代码尝试使用返回值时,这才会导致未定义的行为。
但在这个答案中我读到:
在 C/C++ 下,不从声称返回某些内容的函数中返回是合法的。
正如您在第一个引用的答案中看到的,用户说在 C++ 中它始终是 UB,但第二个引用的答案说它是合法的。他们似乎互相矛盾。
上面引用的 C++ 答案中哪一个是正确的?
另外,我在 C++ 中还有以下示例:
int func(int a, int b)
{
if(a > b)
{
return a;
}
else if(a < b)
{
return b;
}
}
int main()
{
int x =0, y =0;
std::cin>> x >> y;
if(x!=y)
{
func(x,y); //Question 1: IS THIS UB?
std::cout<<"max is: "<<func(x,y); //Question 2: IS THIS UB?
}
else
{
std::cout<<"both are equal"<<std::endl;
}
return 0;
}
我在上面给定的代码片段中有两个问题,我在上面代码的注释中提到过。
从代码中可以看出,控制永远不能流过函数func
的末尾,因为a
内部永远不会等于b
函数,因为我已经在 main
中单独检查了该条件。
I am learning C++ using the books listed here. In particular, I read that flowing off the end of a non-void function is undefined behavior. Then I looked at this answer that says:
In C++ just flowing off the end of a value returning function is always undefined behavior (regardless of whether the function's result is used by the calling code). In C this causes undefined behavior only if the calling code tries to use the returned value.
But in this answer I read:
It is legal under C/C++ to not return from a function that claims to return something.
As you can see in the first quoted answer, the user says that in C++ it is always UB but the second quoted answer says that it is legal. They seem to be contradicting each other.
Which of the above quoted answer is correct in C++?
Also I have the following example in C++:
int func(int a, int b)
{
if(a > b)
{
return a;
}
else if(a < b)
{
return b;
}
}
int main()
{
int x =0, y =0;
std::cin>> x >> y;
if(x!=y)
{
func(x,y); //Question 1: IS THIS UB?
std::cout<<"max is: "<<func(x,y); //Question 2: IS THIS UB?
}
else
{
std::cout<<"both are equal"<<std::endl;
}
return 0;
}
I have 2 question from the above given code snippet which I have mentioned in the comments of the code above.
As can be seen from the code, the control can never flow over the end of the function func
because a
will never be equal to b
inside the function since I have checked that condition in main
separately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这两种说法并不矛盾。
第一个语句是关于当控制流退出非
void
函数而不执行return
语句时会发生什么。第二个陈述是关于当控制流根本不退出函数时会发生什么。对exit
或std::terminate
等函数的调用永远不会使控制流继续超过调用这些函数时的点。但这与返回值的性质无关。
当非
void
函数没有明确的return
语句(或throw
)时程序的行为。或者co_return
这些天)由 [stmt.return]/2:The two statements are in no way contradictory.
The first statement is about what happens when control flow exits a non-
void
function without executing areturn
statement. The second statement is about what happens when control flow does not exit the function at all. Calls to functions likeexit
orstd::terminate
do not ever have control flow proceed past the point when those functions are called.But that has nothing to do with the nature of the return value.
The behavior of the program when a non-
void
function runs out of stuff to do without an explicitreturn
statement (orthrow
. Orco_return
these days) is governed by [stmt.return]/2: