缩小C ++ 0x中的转换。仅仅是我,还是听起来像是一个巨大的变化? [英] Narrowing conversions in C++0x. Is it just me, or does this sound like a breaking change?

查看:137
本文介绍了缩小C ++ 0x中的转换。仅仅是我,还是听起来像是一个巨大的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 0x将使以下代码和类似代码格式错误,因为它需要 double narrowing conversion $ c>到 int

C++0x is going to make the following code and similar code ill-formed, because it requires a so-called narrowing conversion of a double to a int.

int a[] = { 1.0 };

我想知道这种初始化在现实世界的代码中是否被大量使用。此更改将破坏多少代码?如果您的代码受到影响,是否需要花费很多精力在代码中进行修复?

I'm wondering whether this kind of initialization is used much in real world code. How many code will be broken by this change? Is it much effort to fix this in your code, if your code is affected at all?

作为参考,请参见8.5。 n3225的.4 / 6

For reference, see 8.5.4/6 of n3225


缩小转换是隐式转换

A narrowing conversion is an implicit conversion


  • 从浮点类型到整数类型,或者

  • 从long double到double或float,或者从double到float,除非源是a常量表达式和转换后的实际值在可以表示的值的范围内(即使不能准确表示),或者

  • 从整数类型或无范围枚举类型转换为a浮点类型,除非源是一个常量表达式,并且转换后的实际值将适合目标类型,并且转换回原始类型时将产生原始值,或者

  • 从整数类型或无作用域枚举类型到不能表示t的所有值的整数类型$ li
  • from a floating-point type to an integer type, or
  • from long double to double or float, or from double to float, except where the source is a constant expression and the actual value after conversion is within the range of values that can be represented (even if it cannot be represented exactly), or
  • from an integer type or unscoped enumeration type to a floating-point type, except where the source is a constant expression and the actual value after conversion will fit into the target type and will produce the original value when converted back to the original type, or
  • from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type, except where the source is a constant expression and the actual value after conversion will fit into the target type and will produce the original value when converted back to the original type.


推荐答案

使用GCC时遇到了这一重大变化。编译器为以下代码显示错误:

I ran into this breaking change when I used GCC. The compiler printed an error for code like this:

void foo(const unsigned long long &i)
{
    unsigned int a[2] = {i & 0xFFFFFFFF, i >> 32};
}




在函数 void foo(const long long unsigned int&)

错误:缩小(((long long unsigned int )i)& 4294967295ull)从{code> long long unsigned int unsigned int 中的{ }

error: narrowing conversion of (((long long unsigned int)i) & 4294967295ull) from long long unsigned int to unsigned int inside { }

错误:缩小((((long long unsigned int)i)>> 32)的转换从{}

error: narrowing conversion of (((long long unsigned int)i) >> 32) from long long unsigned int to unsigned int inside { }

$中的 long long unsigned int unsigned int b
$ b

幸运的是,错误消息很直接而且修复很简单:

Fortunately, the error messages were straightforward and the fix was simple:

void foo(const unsigned long long &i)
{
    unsigned int a[2] = {static_cast<unsigned int>(i & 0xFFFFFFFF),
            static_cast<unsigned int>(i >> 32)};
}

该代码位于外部库中,一个文件中只有两次。我认为重大更改不会影响太多代码。新手可能获取 困惑,

The code was in an external library, with only two occurrences in one file. I don't think the breaking change will affect much code. Novices might get confused, though.

这篇关于缩小C ++ 0x中的转换。仅仅是我,还是听起来像是一个巨大的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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