对于非 void 函数没有 return 语句,其中控制永远不会结束,这是未定义的行为吗?

发布于 2025-01-14 20:35:50 字数 1258 浏览 3 评论 0原文

我正在使用此处列出的书籍学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2025-01-21 20:35:50

这两种说法并不矛盾。

第一个语句是关于当控制流退出非 void 函数而不执行 return 语句时会发生什么。第二个陈述是关于当控制流根本不退出函数时会发生什么。对 exitstd::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 a return statement. The second statement is about what happens when control flow does not exit the function at all. Calls to functions like exit or std::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 explicit return statement (or throw. Or co_return these days) is governed by [stmt.return]/2:

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文