§ 8.5.4/7 中的标准解释了缩小转换是什么:

然后它在某些列表初始化上下文中禁止这种转换,给出
例子:

int x = 999;        // x is not a constant expression
const int y = 999;
const int z = 99;
char c1 = x;    // OK, though it might narrow (in this case, it does narrow)
char c2{x};     // error: might narrow
char c3{y};     // error: narrows (assuming char is 8 bits)
char c4{z};     // OK: no narrowing needed
unsigned char uc1 = {5};    // OK: no narrowing needed
unsigned char uc2 = {-1};   // error: narrows
unsigned int ui1 = {-1};    // error: narrows
signed int si1 =
{ (unsigned int)-1 };   // error: narrows
int ii = {2.0};         // error: narrows
float f1 { x };         // error: might narrow
float f2 { 7 };         // OK: 7 can be exactly represented as a float
int f(int);
int a[] =
{ 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level

示例中说明的所有 7 个错误均由
带有 -std=c++11 的 clang 3.2/3.3 ,例如
error: non-constant-expression cannot be narrowed from type 'int' to 'char' in initializer list [-Wc++11-narrowing]
gcc 4.7.2/4.8.1 没有将它们报告为错误,但在每种情况下
给出了类似的警告,例如
warning: narrowing conversion of ‘x’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]
(所以 gcc 似乎知道合规需要什么,但选择容忍不合规
默认。)
我不明白的是这个例子如何:
unsigned int ui1 = {-1};    // error: narrows
有资格作为例子。 (同样的对称 si1 示例。)显然
唯一可以作为例子的词是第四个词
和上面给出的缩小转换定义中的最后一项;但
如果是,那么为什么这个例子没有通过限定条件逃脱
除非源是一个常量表达式并且之后的实际值
转换将适合目标类型,并在转换回目标类型时产生原始值
原始类型?当然 -1 有一个整数常量,如果转换为 unsigned 并返回,
仍然产生 int -1
我错过了什么?

最佳答案



这是错误的。如果将 -1 转换为 unsigned ,则会得到 UINT_MAX 。这很好,因为总是定义转换为无符号类型。但是,UINT_MAX 不适合 int 并且只有当值适合目标类型时,标准才定义到有符号类型的转换。

关于c++ - 为什么 "unsigned int ui = {-1};"是缩小转换错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17551583/

10-11 22:51
查看更多