为什么这个C ++代码段编译(非void函数不返回值) [英] Why does this C++ snippet compile (non-void function does not return a value)

查看:1662
本文介绍了为什么这个C ++代码段编译(非void函数不返回值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天早上在我的一个库中发现了这个:

  static tvec4 Min(const tvec4& a,const tvec4& b,tvec4& out)
{
tvec3 :: Min(a,b,out);
out.w = min(a.w,b.w);
}



我希望有一个编译器错误,因为这个方法不返回任何东西,并且返回类型不是 void



只有两个事情是




  • 在调用此方法的唯一位置,未使用或存储返回值。 (这个方法应该是 void - tvec4 返回类型是一个复制和粘贴错误) p>


  • 正在创建一个默认构造的 tvec4 ,这似乎有点不像C ++中的一切。




我没有找到C ++规范中解决这个问题的部分。



情况,这会在VS2012中生成错误。

解决方案

这是未定义的行为。 pdf> C ++ 11草稿标准部分 6.6.3 返回语句说:


[...]流出函数的末尾相当于没有值的返回;这导致在值返回函数中的未定义行为。 [...]


这意味着编译器没有义务提供错误或警告,因为很难诊断在所有情况下。我们可以从标准草案 1.3.24 中的未定义行为定义中看到这一点:


允许未定义的行为范围从完全以不可预测的结果忽略情况,在翻译或程序执行期间以文件化的环境特征表现(具有或[...]


虽然在这种情况下,我们可以得到 gcc clang 来生成一个使用 -Wall 标志,这会给我类似的警告:


警告:非空函数[-Wreturn-type]


我们可以使用 -Werror = return-type 标志。我也喜欢使用 -Wextra -Wconversion -pedantic 为我自己的个人项目。



正如ComicSansMS在 Visual Studio ,此代码将生成 C4716 ,这是默认情况下的错误,我看到的消息是:


错误C4716:'Min':必须返回值




,如果并非所有代码路径都返回值,那么它将生成 C4715 ,这是一个警告。


I found this in one of my libraries this morning:

static tvec4 Min(const tvec4& a, const tvec4& b, tvec4& out)
{
    tvec3::Min(a,b,out);
    out.w = min(a.w,b.w);
}

I'd expect a compiler error because this method doesn't return anything, and the return type is not void.

The only two things thing that comes to mind are

  • In the only place where this method is called, the return value isn't being used or stored. (This method was supposed to be void - the tvec4 return type is a copy-and-paste error)

  • a default constructed tvec4 is being created, which seems a bit unlike, oh, everything else in C++.

I haven't found the part of the C++ spec that addresses this. References (ha) are appreciated.

Update

In some circumstances, this generates an error in VS2012. I haven't narrowed down specifics, but it's interesting, nonetheless.

解决方案

This is undefined behavior from the C++11 draft standard section 6.6.3 The return statement paragraph 2 which says:

[...] 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. [...]

This means that the compiler is not obligated provide an error nor a warning usually because it can be difficult to diagnose in all cases. We can see this from the definition of undefined behavior in the draft standard in section 1.3.24 which says:

[...]Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).[...]

Although in this case we can get both gcc and clang to generate a wanring using the -Wall flag, which gives me a warning similar to this:

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

We can turn this particular warning into an error using the -Werror=return-type flag. I also like to use -Wextra -Wconversion -pedantic for my own personal projects.

As ComicSansMS mentions in Visual Studio this code would generate C4716 which is an error by default, the message I see is:

error C4716: 'Min' : must return a value

and in the case where not all code paths would return a value then it would generate C4715, which is a warning.

这篇关于为什么这个C ++代码段编译(非void函数不返回值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆