[英]When a function missing the return value, the compiler generates a warning but not an error?
Here is a c++ function: 这是一个c ++函数:
int FuncWithoutReturn()
{
int var = 10;
++var;
// No return value here !!!
}
In MSVC, compiler generates error: 在MSVC中,编译器生成错误:
error C4716: 'FuncWithoutReturn' : must return a value.
错误C4716:'FuncWithoutReturn':必须返回一个值。
But in XCode 5, the compiler just spits a warning: 但在XCode 5中,编译器只是发出警告:
Control reaches end of non-void function
控制到达非空函数的结束
In runtime if I am lucky, the app crashes. 在运行时,如果我很幸运,应用程序崩溃。 I know it is a stupid error but it would be good that the compiler yields an error in first place.
我知道这是一个愚蠢的错误,但编译器首先产生错误会很好。
Just wondering anyone knows WHY XCode think it is a warning instead of an error. 只是想知道为什么XCode认为这是一个警告而不是错误。
You can use -Werror=return-type
to make that warning and error, in my original comment I forgot that. 您可以使用
-Werror=return-type
来发出警告和错误,在我原来的评论中我忘记了。 You can see it live . 你可以看到现场 。
This is both an option in clang and gcc , as far as I understand XCode
can use either one. 这是clang和gcc中的一个选项,据我所知,
XCode
可以使用其中任何一个。
Falling off the end of value returning function is undefined behavior , we can see this by going to the draft C++ standard section 6.6.3
The return statement paragraph 2 which says: 退出值返回函数的末尾是未定义的行为 ,我们可以通过转到草案C ++标准部分
6.6.3
看到返回语句第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.
这会导致值返回函数中的未定义行为。
Undefined Behavior does not require a diagnostic( warning or error ), although in many cases compilers will provide one. 未定义的行为不需要诊断( 警告或错误 ),但在许多情况下编译器将提供一个。
You can enable it using -Werror=return-type
您可以使用
-Werror=return-type
启用它
Just wondering anyone knows WHY XCode think it is a warning instead of an error.
只是想知道为什么XCode认为这是一个警告而不是错误。
Check your project's/target's/xcconfig's settings for "Mismatched Return Type" (aka GCC_WARN_ABOUT_RETURN_TYPE
). 检查项目的/目标的/ xcconfig的“不匹配的返回类型”(又名
GCC_WARN_ABOUT_RETURN_TYPE
)的设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.