省略 C++ 中的 return 语句

发布于 2024-09-12 08:54:20 字数 582 浏览 10 评论 0原文

我刚刚使用 Strawberry Perl 获得的 Windows 版 g++ 出现了一些奇怪的行为。它允许我省略返回语句。

我有一个成员函数,它返回一个由两个指针组成的结构,称为boundTag:

struct boundTag Box::getBound(int side) {
    struct boundTag retBoundTag;
    retBoundTag.box = this;
    switch (side)
    {
        // set retBoundTag.bound based on value of "side"
    }
}

这个函数给了我一些错误的输出,我发现它没有 return 语句。我本想返回 retBoundTag ,但忘记实际编写返回语句。一旦我添加了 return retBoundTag; 一切都很好。

但我已经测试了这个函数并从中获得了正确的 boundTag 输出。即使现在,当我删除 return 语句时,g++ 也会在没有警告的情况下编译它。搞什么?它是否猜测返回retBoundTag

I just had some weird behavior from a version of g++ for Windows that I got with Strawberry Perl. It allowed me to omit a return statement.

I have a member function that returns a structure consisting of two pointers, called a boundTag:

struct boundTag Box::getBound(int side) {
    struct boundTag retBoundTag;
    retBoundTag.box = this;
    switch (side)
    {
        // set retBoundTag.bound based on value of "side"
    }
}

This function gave me some bad output, and I discovered that it had no return statement. I had meant to return retBoundTag but forgot to actually write the return statement. Once I added return retBoundTag; everything was fine.

But I had tested this function and gotten correct boundTag output from it. Even now, when I remove the return statement, g++ compiles it without warning. WTF? Does it guess to return retBoundTag?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

无声无音无过去 2024-09-19 08:54:20

省略 non-void 函数中的 return 语句 [除 main()] 并在代码中使用返回值会调用 未定义行为

ISO C++-98[第 6.6.3/2 节]

可以使用带表达式的 return 语句
仅在返回值的函数中;表达式的值为
返回给函数的调用者。如果需要,表达式
隐式转换为函数的返回类型
出现。 return 语句可以涉及构造和复制
临时对象(class.temporary)。 从a的末端流出
函数相当于没有值的返回;这导致
返回值函数中的未定义行为

例如

int func()
{
    int a=10;
    //do something with 'a'
    //oops no return statement
}


int main()
{
     int p=func();
     //using p is dangerous now
     //return statement is optional here 
}

,通常 g++ 会给出警告:控制到达非 void 函数的末尾。尝试使用 -Wall 选项进行编译。

Omitting the return statement in a non-void function [Except main()] and using the returned value in your code invokes Undefined Behaviour.

ISO C++-98[Section 6.6.3/2]

A return statement with an expression can be used
only in functions returning a value; the value of the expression is
returned to the caller of the function. If required, the expression
is implicitly converted to the return type of the function in which it
appears. A return statement can involve the construction and copy of
a temporary object (class.temporary). 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
.

For example

int func()
{
    int a=10;
    //do something with 'a'
    //oops no return statement
}


int main()
{
     int p=func();
     //using p is dangerous now
     //return statement is optional here 
}

Generally g++ gives a warning: control reaches end of non-void function. Try compiling with -Wall option.

野心澎湃 2024-09-19 08:54:20

C 和 C++ 不要求您有 return 语句。
可能没有必要有一个,因为函数进入无限循环,或者因为它抛出异常。

Prasoon 已经引用了标准的相关部分:

[第 6.6.3/2 节]

带有表达式的 return 语句只能在返回值的函数中使用;表达式的值返回给函数的调用者。如果需要,表达式会隐式转换为其所在函数的返回类型。 return 语句可以涉及临时对象 (class.temporary) 的构造和复制。从函数末尾流出相当于没有值的返回;这会导致返回值函数中出现未定义的行为。

这意味着没有 return 语句是可以的。但是到达函数末尾而不返回是未定义的行为

编译器并不总是能检测到这些情况,因此不一定是编译错误(它必须解决暂停问题以确定执行是否实际上到达函数的末尾)。如果发生这种情况会发生什么,这只是未定义。它可能看起来有效(因为调用函数只会查看返回值所在位置中的任何垃圾值),它可能会崩溃,或者让恶魔从你的鼻子里飞出来。

C and C++ don't require you to have a return statement.
It might not be necessary to have one, because the function enters an infinite loop, or because it throws an exception.

Prasoon already quoted the relevant part of the standard:

[Section 6.6.3/2]

A return statement with an expression can be used only in functions returning a value; the value of the expression is returned to the caller of the function. If required, the expression is implicitly converted to the return type of the function in which it appears. A return statement can involve the construction and copy of a temporary object (class.temporary). 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.

What it means is that not having a return statement is ok. But reaching the end of the function without returning is undefined behavior.

The compiler can't always detect these cases, so it's not required to be a compile error (it'd have to solve the halting problem to determine whether execution ever actually reaches the end of the function). It is simply undefined what should happen if this occurs. It might appear to work (because the calling function will just look at whatever garbage value is in the location where the return value is supposed to be), it might crash, or make demons fly out of your nose.

像你 2024-09-19 08:54:20

尽管 C++ 编译器不能总是检测到函数何时无法执行 return 语句,但它通常可以。

从好的方面来说,至少 g++ 可以通过命令行编译器选项“-Wreturn-type”轻松检测到这一点。您只需要记住启用它即可。 (如果您使用“-Wall”,它也会被启用。)

Even though aC++ compiler can't always detect when a function can't execute a return statement, it usually can.

On the bright side, at least g++ makes this easy to detect with the command-line compiler option "-Wreturn-type". You just need to remember to enable it. (It also gets enabled if you use "-Wall".)

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