简体   繁体   English

算术转换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

This is a piece of code on C++ Primer. 这是C ++ Primer上的一段代码。 I know sval+cval generates an int type according to 我知道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. 如果该类型的所有可能值都适合int,则将bool,char,signed char,unsigned char,short和unsigned short类型升级为int。

But for the last one I couldn't understand why it uses "converted". 但是对于最后一个,我不明白为什么它使用“转换”。 Why is cval not promoted to int first and then the int converted (or maybe promoted I'm not sure whether promoted can be used from int to long because I only see definition of promotion on smaller type to int ) to long . 为什么不cval提升为int ,然后再将int转换为int (或者可能是提升的,所以我不确定是否可以从intlong使用提升的,因为我只看到将提升的定义从较小的类型转换为int )到long I didn't see any explanation or examples on char straightly to long in that part of the book. 我没有看到任何解释或实例char直以long书中的一部分。
Is there any thing wrong with my understanding? 我的理解有什么问题吗?
I'm quite new at C++, someone please enlighten me! 我是C ++的新手,请给我一个启发! Many thanks in advance! 提前谢谢了!

The additive operators perform what is called the usual arithmetic conversion on their operands which can include integral promotions and then after that we can have further conversions. 加法运算符在其操作数上执行所谓的常规算术转换 ,其中可以包括整数提升,然后我们可以进行进一步的转换。 The purpose is to yield a common type and if the promotions do not accomplish that then a further conversion is required. 目的是产生一个普通类型,如果促销没有完成,则需要进一步转换。

This is covered in section 5 [expr] of the draft C++ standard which says ( emphasis mine ): 这在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 这种模式称为通常的算术转换 ,其定义如下

and includes the following bullet: 并包括以下项目符号:

  • Otherwise, the integral promotions (4.5) shall be performed on both operands.61 Then the following rules shall be applied to the promoted operands: 否则,应在两个操作数上执行整数提升(4.5) 。61然后,以下规则应应用于提升的操作数:

which has the following bullets: 其中包含以下项目符号:

  • 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 . 否则,如果两个操作数都具有符号整数类型或都具有无符号整数类型,则整数转换等级较小的操作数应转换为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. 否则,两个操作数均应转换为与带符号整数类型的操作数类型相对应的无符号整数类型。

So in the first case after promotions they both have the same type( int ) so no further conversion is needed. 因此,在促销后的第一种情况下,它们都具有相同的type( int ),因此不需要进一步的转换。

In the second case after promotions they do not( int and long ) so a further conversion is required. 在升级后的第二种情况下,它们不( int和long ),因此需要进一步转换。

From the C++11 Standard: 从C ++ 11标准:

4 Standard conversions 4标准转换

1 Standard conversions are implicit conversions with built-in meaning. 1标准转换是具有内置含义的隐式转换。 Clause 4 enumerates the full set of such conversions. 第4章列举了此类转换的全部内容。 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. —零或一次资格转换。

In the expression, 在表达式中

cval + lval;

since cval is not of type long , it has to be converted to long . 由于cval的类型不是long类型,因此必须将其转换为long However, in the process of applying the standard conversions, integral promotion comes ahead of conversions . 但是,在应用标准转化的过程中, 积分促销要先于转化 Hence, cval is promoted to an int first before being converted to a long . 因此, cval在转换为long之前先提升为int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM