忽略C ++ 0x中缩小的转换的后果是什么 [英] What are the consequences of ignoring narrowing conversions in C++0x

查看:205
本文介绍了忽略C ++ 0x中缩小的转换的后果是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为在g ++中打开C ++ 0x标准,我开始看到缩小转换错误,特别是在从int转换为short时,尽管我理解错误覆盖了更宽的转化次数

Since switching on the C++0x standard in g++, I've started seeing 'narrowing conversion' errors, particularly when converting from an 'int' to a 'short' although I understand the error covers a much broader swath of conversions.

任何人都可以阐明引入这个额外安全级别的理由吗?禁用此错误有什么可能的后果? (除了潜在的精确度损失)。

Can anyone shed some light on the rational for introducing this extra level of safety?, What are the possible consequences of disabling this error? (apart from the potential loss of precision).

谢谢。

推荐答案

p>从作业和复合赋值运算符 [expr.ass]

From Assignment and compound assignment operators [expr.ass]



x = {v}的意思,其中T是表达式x的标量类型,是x = T(v)的标量类型,除了不允许
缩小转换(8.5.4)。 / p>

The meaning of x={v}, where T is the scalar type of the expression x, is that of x=T(v) except that no narrowing conversion (8.5.4) is allowed.

列表初始化 [dcl.ini.list] p>

and from List-initialization [dcl.ini.list]


如果需要缩小转换(见下文)来转换任何参数,程序就会生成错误。

If a narrowing conversion (see below) is required to convert any of the arguments, the program is ill-formed.

所以基本上你不能忽视它,你的程序在缩小转换的情况下是不成熟的。

So basically you can't ignore it, your program is ill-formed in presence of narrowing conversions.

实施合规性


需要实施程序来诊断
使用此类扩展根据本国际标准不合格。但是,这样做后,他们可以编译并执行此类程序。

Implementations are required to diagnose programs that use such extensions that are ill-formed according to this International Standard. Having done so, however, they can compile and execute such programs.

Bjarne Stroustroup说

Bjarne Stroustroup say this:


>防止缩小

问题:C和C ++隐式截断:

The problem: C and C++ implicitly truncates:


$ b b

   int x = 7.3;        // Ouch!
    void f(int);
    f(7.3);         // Ouch!




但是,在C ++ 0x中,{}初始化不缩小:

However, in C++0x, {} initialization doesn't narrow:



int x0 {7.3};   // error: narrowing
int x1 = {7.3}; // error: narrowing
double d = 7;
int x2{d};      // error: narrowing (double to int)
char x3{7};     // ok: even though 7 is an int, this is not narrowing
vector<int> vi = { 1, 2.3, 4, 5.6 };    // error: double to int narrowing




C ++ 0x避免很多不兼容性是通过依赖初始化器的实际值(例如上面的例子中的7),当它可以(而不仅仅是类型),当决定什么是缩小转换。如果一个值可以精确地表示为目标类型,则转换不会变窄。

The way C++0x avoids a lot of incompatibilities is by relying on the actual values of initializers (such as 7 in the example above) when it can (and not just type) when deciding what is a narrowing conversion. If a value can be represented exactly as the target type, the conversion is not narrowing.



char c1{7};      // OK: 7 is an int, but it fits in a char
char c2{77777};  // error: narrowing 




请注意,浮点到整数转换

Note that floating-point to integer conversions are always considered narrowing -- even 7.0 to 7.

因此,在某种程度上,缩小也会提高类型安全性。

So in a way, narrowing also increases type safety.

这篇关于忽略C ++ 0x中缩小的转换的后果是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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