简体   繁体   English

当函数缺少返回值时,编译器会生成警告但不会出错?

[英]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. 这是clanggcc中的一个选项,据我所知, 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.

相关问题 C++:当 function 返回值未使用时如何触发编译器错误? - C++: How to trigger a compiler error when function return value is unused? 使用 typedef 返回类型声明友元函数时出现编译器错误 - Compiler error when declaring a friend function with a typedef'ed return type 对未定义的内容使用#if 时的编译器警告/错误 - Compiler warning/error when using #if on something that is undefined 以一种在使用临时调用时生成编译器错误的方式重载方法 - Overload a method in a way that generates a compiler error when called with a temporary 当没有从函数显式返回值时,如何强制编译器错误 - How to force compiler error when no value is explicitly returned from function operator []编译器错误和警告 - operator[] compiler error and warning FileCodeModel :: AddFunction在传递vsCMFunctionConstructor时生成一个带返回类型的函数 - FileCodeModel::AddFunction generates a function with return type when passed vsCMFunctionConstructor 将未初始化的局部变量传递给函数时的C ++编译器警告(?) - C++ compiler warning(?) when passing uninitialized local variable to function 为什么没有返回语句时没有编译器错误? - Why there is no compiler error when return statement is not present? C ++:&#39;cout &lt;&lt; pointer &lt;&lt; ++ pointer&#39;生成编译器警告 - C++: 'cout << pointer << ++pointer' generates a compiler warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM