省略 C++ 中的 return 语句
我刚刚使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
省略
non-void
函数中的return
语句 [除main()
] 并在代码中使用返回值会调用 未定义行为。ISO C++-98[第 6.6.3/2 节]
例如
,通常 g++ 会给出
警告:控制到达非 void 函数的末尾
。尝试使用-Wall
选项进行编译。Omitting the
return
statement in anon-void
function [Exceptmain()
] and using the returned value in your code invokes Undefined Behaviour.ISO C++-98[Section 6.6.3/2]
For example
Generally g++ gives a
warning: control reaches end of non-void function
. Try compiling with-Wall
option.C 和 C++ 不要求您有
return
语句。可能没有必要有一个,因为函数进入无限循环,或者因为它抛出异常。
Prasoon 已经引用了标准的相关部分:
[第 6.6.3/2 节]
这意味着没有 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]
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.
尽管 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".)