扩展无符号整数类型的转换等级

Conversion rank of extended unsigned integer type

转换排名在6.3.1.1/1中定义:

Every integer type has an integer conversion rank defined as follows:

— No two signed integer types shall have the same rank, even if they have the same representation.

— The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision.

— The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.

— The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.

— The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width.

— The rank of char shall equal the rank of signed char and unsigned char .

— The rank of _Bool shall be less than the rank of all other standard integer types.

— The rank of any enumerated type shall equal the rank of the compatible integer type (see 6.7.2.2).

— The rank of any extended signed integer type relative to another extended signed integer type with the same precision is implementation-defined, but still subject to the other rules for determining the integer conversion rank.

— For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 has greater rank than T3.

有一个关于有符号整数类型的规则:

The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision.

问题:具有更高精度的扩展无符号整数类型是否可以具有更小的整数转换等级?

考虑 size_tunsigned int。第一个是扩展整数类型,如果 size_t 的整数转换级别低于 unsigned int,则 整数提升 应用于 size_t,这可能导致精度损失。

首先,size_t 通常 不是 扩展整数类型,而是 typedef 现有的无符号整数类型。即使这样,它的等级也可以低于或高于 unsigned int。其次,标准在 6.3.1.1p3 中说

  1. The integer promotions preserve value including sign.

即因此,值位数多于 unsigned int 的无符号整数类型的转换等级不能小于 unsigned int,否则整数提升的子句将无效。

当然,正如 Kamil Cuk 指出的那样,6.2.5p8 nails it:

8 For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange of the values of the other type.


我相信您将 usual arithmetic conversions 与整数促销混淆了。通常的算术转换会丢失符号并修改负符号整数的值。