gpt4 book ai didi

c++ - 为什么 "unsigned int ui = {-1};"是缩小转换错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:37:15 24 4
gpt4 key购买 nike

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

A narrowing conversion is an implicit conversion

— 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 constantexpression 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 sourceis a constant expression and the actual value after conversion will fit into the target type and willproduce 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 thevalues of the original type, except where the source is a constant expression and the actual value afterconversion will fit into the target type and will produce the original value when converted back to theoriginal type.

然后在某些列表初始化上下文中不允许这样的转换,给出示例:

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

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

— end example ]

示例中说明的所有 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

我错过了什么?

最佳答案

Surely -1 there is an integer constant and, if converted to unsigned and back, still yields int -1?

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com