C ++标准是否指定在某些情况下编译应因错误而失败? [英] Does the C++ standard specify that for some cases the compiling should fail with an error?

查看:77
本文介绍了C ++标准是否指定在某些情况下编译应因错误而失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查有关缩小转换的标准,我认为对于缩小转换,应该触发错误.因为标准表示:

I'm checking the standard about narrowing conversion, and I think for a narrowing conversion an error should be triggered. Because the standard says:

[注意:如上所述,在列表初始化中,不允许在顶层进行此类转换. —尾注]

[ Note: As indicated above, such conversions are not allowed at the top level in list-initializations. — end note ]

我认为对不允许"的描述意味着编译应该失败.

I think the description of "not allowed" means the compiling should fail.

但是有人告诉我此处只是说程序格式错误",并且标准不会要求编译必须失败.

But someone told me that here just says "the program is ill-formed", and the standard won't require that compilation must fail.

如果需要缩小转换(请参阅下文)以转换 元素为T时,程序格式不正确.

if a narrowing conversion (see below) is required to convert the element to T, the program is ill-formed.

所以我的问题是:标准是否指定应生成错误还是警告?还是在某些情况下编译应该失败?从编译器的角度来看,可以编译程序并给出一些警告吗?

So my question is: Does the standard specify whether an error or warning should be generated? Or for some cases the compiling should fail? From the aspect of a compiler, is it OK to make the program compile and just give some warnings?

BTW: Clang 4.0.0

BTW: Clang 4.0.0 and Gcc 7.0.0 behave differently.

float a {1.e39}; // Error for both Clang and GCC
double d;
float a3{d};     // Error for Clang, warning for GCC

推荐答案

该标准不使用术语错误"和警告",仅讨论编译器必须发出诊断信息"的情况.

The standard doesn't use the terms "error" and "warning", it only talks about cases where the compiler must "issue a diagnostic".

在您的示例中,如果程序格式错误",则要求编译器以某种方式告诉您-发出诊断信息.

In your example, if the program is "ill-formed", the compiler is required to tell you that somehow - issue a diagnostic.

之后,它可以做任何喜欢的事情-包括仍然编译和运行程序.该标准仅指定符合标准的代码会发生什么,其他所有内容均未定义.然后,据我们所知,任何事情都会发生.

After that, it can do anything it likes - including compiling and running the program anyway. The standard only specifies what happens for conforming code, everything else is undefined. And then, as we know, anything can happen.

这篇关于C ++标准是否指定在某些情况下编译应因错误而失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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