sizeof(short) == sizeof(int) 实现的整数提升
Integer promotion for implementations where sizeof(short) == sizeof(int)
背景
我正在研究 C++ 中的整数提升规则,发现了以下内容(摘自 n4296):
4.5.1 [pconv.prom]
A prvalue of an integer type other than bool
, char16_t
, char32_t
, or wchar_t
whose integer conversion rank (4.13) is less than the rank of int
can be converted to a prvalue of type int
if int
can represent all the values of the source type;
otherwise, the source prvalue can be converted to a prvalue of type
unsigned int
.
4.13.1.3 [conv.rank]
- 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
.
5.10 [expr]
Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result
types in a similar way. The purpose is to yield a common type, which
is also the type of the result. This pattern is called the usual
arithmetic conversions , which are defined as follows:
[many points omitted]
- Otherwise, the integral promotions (4.5) shall be performed on both operands.
[further omissions]
问题
鉴于short int
的大小和范围可能与int
的大小和范围相等,因此似乎没有必要将short int
提升为int
情况。然而,上面没有提到这种例外。
标准是否要求在这些情况下将 short int
提升为 int
(即使实施优化导致生成相同的可执行文件)?
注意/附加问题
我也注意到措辞使用"can be"而不是"shall be",这是故意的吗?
short
和 int
具有相同的范围和表示形式,这是完全合法的,并且曾经非常普遍;即使在今天,嵌入式系统对两者都使用相同的 16 位表示也并不少见。 C 规范确实包含一些特定于此类实现的语言,因为在此类平台上 unsigned short
提升为 unsigned int
而在平台上提升为 signed int
后一种类型可以表示所有值unsigned short
。它不会从要求将小于 int
或 unsigned int
的类型提升为其中一种类型的规则中豁免此类实现,因为这样的豁免不会带来任何好处。
如果在所有情况下 int
或 unsigned int
的指定提升会产生定义的行为,那么该标准允许实现可以以任何它认为合适的方式执行计算,实现的计算产生相同的行为(as-if 规则)。如果 int
和 short
类型的右值的行为是无法区分的,这意味着一个实现可以在 short
而不是 int
上执行计算,如果它选择这样做的话.无需向标准豁免 int
大小的 short
类型添加规则,因为假设规则已经足够了。
背景
我正在研究 C++ 中的整数提升规则,发现了以下内容(摘自 n4296):
4.5.1 [pconv.prom]
A prvalue of an integer type other than
bool
,char16_t
,char32_t
, orwchar_t
whose integer conversion rank (4.13) is less than the rank ofint
can be converted to a prvalue of typeint
ifint
can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of typeunsigned int
.
4.13.1.3 [conv.rank]
- The rank of
long long int
shall be greater than the rank oflong int
, which shall be greater than the rank ofint
, which shall be greater than the rank ofshort int
, which shall be greater than the rank ofsigned char
.
5.10 [expr]
Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions , which are defined as follows:
[many points omitted]
- Otherwise, the integral promotions (4.5) shall be performed on both operands.
[further omissions]
问题
鉴于short int
的大小和范围可能与int
的大小和范围相等,因此似乎没有必要将short int
提升为int
情况。然而,上面没有提到这种例外。
标准是否要求在这些情况下将 short int
提升为 int
(即使实施优化导致生成相同的可执行文件)?
注意/附加问题
我也注意到措辞使用"can be"而不是"shall be",这是故意的吗?
short
和 int
具有相同的范围和表示形式,这是完全合法的,并且曾经非常普遍;即使在今天,嵌入式系统对两者都使用相同的 16 位表示也并不少见。 C 规范确实包含一些特定于此类实现的语言,因为在此类平台上 unsigned short
提升为 unsigned int
而在平台上提升为 signed int
后一种类型可以表示所有值unsigned short
。它不会从要求将小于 int
或 unsigned int
的类型提升为其中一种类型的规则中豁免此类实现,因为这样的豁免不会带来任何好处。
如果在所有情况下 int
或 unsigned int
的指定提升会产生定义的行为,那么该标准允许实现可以以任何它认为合适的方式执行计算,实现的计算产生相同的行为(as-if 规则)。如果 int
和 short
类型的右值的行为是无法区分的,这意味着一个实现可以在 short
而不是 int
上执行计算,如果它选择这样做的话.无需向标准豁免 int
大小的 short
类型添加规则,因为假设规则已经足够了。