gpt4 book ai didi

c++ - 当返回对象的函数在没有返回语句的情况下结束时会发生什么

转载 作者:IT老高 更新时间:2023-10-28 22:16:39 25 4
gpt4 key购买 nike

在 C++ 中,当一个应该返回对象的函数在没有返回语句的情况下结束时会发生什么?返回什么?

例如

std::string func() {}

最佳答案

What gets returned?

我们不知道。根据标准,行为是未定义的。

§6.6.3/2 The return statement[stmt.return] :

(强调我的)

Flowing off the end of a constructor, a destructor, or a function with a cv void return type is equivalent to a return with no operand. Otherwise, flowing off the end of a function other than main (basic.start.main) results in undefined behavior.

事实上大多数编译器都会给出警告,比如 Clang :

warning: control reaches end of non-void function [-Wreturn-type]

关于c++ - 当返回对象的函数在没有返回语句的情况下结束时会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39118324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com