算术转换VS积分提升

Arithmetic conversion VS integral promotion

char cval;
short sval;
long lval;
sval + cval; // sval and cval promoted to int
cval + lval; // cval converted to long

这是C++ Primer上的一段代码。 我知道 sval+cval 根据

生成一个 int 类型

convert the small integral types to a larger integral type. The types bool, char, signed char, unsigned char, short, and unsigned short are promoted to int if all possible values of that type fit in an int.

但是最后一个我不明白为什么要用"converted"。为什么 cval 没有先提升到 int 然后 int 转换(或者可能提升 我不确定提升是否可以从 intlong 因为我只看到较小类型的升级定义 int) 到 long。在本书的那部分,我没有看到任何关于 char 直接到 long 的解释或示例。
我的理解有问题吗?
我是 C++ 的新手,有人请赐教!非常感谢!

来自 C++11 标准:

4 Standard conversions

1 Standard conversions are implicit conversions with built-in meaning. Clause 4 enumerates the full set of such conversions. A standard conversion sequence is a sequence of standard conversions in the following order:

— Zero or one conversion from the following set: lvalue-to-rvalue conversion, array-to-pointer conversion, and function-to-pointer conversion.

— Zero or one conversion from the following set: integral promotions, floating point promotion, integral conversions, floating point conversions, floating-integral conversions, pointer conversions, pointer to member conversions, and boolean conversions.

— Zero or one qualification conversion.

在表达式中,

cval + lval;

由于 cval 不是 long 类型,因此必须将其转换为 long。但是,在应用标准转化的过程中,积分提升 领先于 转化。因此,cval 在转换为 long.

之前首先被提升为 int

加法运算符对其操作数执行所谓的usual arithmetic conversion,其中可以包括整数提升,然后我们可以进行进一步的转换。目的是产生一个通用类型,如果促销没有实现,则需要进一步转换。

这在 C++ 标准草案的 5 [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 follow

并包括以下项目符号:

  • Otherwise, the integral promotions (4.5) shall be performed on both operands.61 Then the following rules shall be applied to the promoted operands:

其中包含以下项目符号:

  • If both operands have the same type, no further conversion is needed

  • Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank shall be converted to the type of the operand with greater rank.

  • Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type.
  • Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, the operand with unsigned integer type shall be converted to the type of the operand with signed integer type.
  • Otherwise, both operands shall be converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

所以在第一种情况下,在提升之后它们都具有相同的类型(int)所以不需要进一步的转换。

在第二种情况下,在促销之后它们没有(int 和 long)因此需要进一步转换。