算术转换VS积分提升 [英] Arithmetic conversion VS integral promotion

查看:94
本文介绍了算术转换VS积分提升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

This is a piece of code on C++ Primer. I know sval+cval generates an int type according to

将小整数类型转换为大整数类型.类型 bool,char,signed char,unsigned char,short和unsigned short是 如果该类型的所有可能值都适合int,则提升为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.

但是对于最后一个,我不明白为什么它使用转换".为什么先不将cval升级为int,然后再转换为int(或者可能是升级的,所以我不确定是否可以将int升级为long来使用升级的,因为我只看到较小的升级定义键入int)到long.在本书的那部分中,我没有直接在long上看到关于char的任何解释或示例.
我的理解有什么问题吗?
我是C ++的新手,请有人启发我!提前非常感谢!

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. I didn't see any explanation or examples on char straightly to long in that part of the book.
Is there any thing wrong with my understanding?
I'm quite new at C++, someone please enlighten me! 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.

这在C ++标准草案的5 [expr] 部分中进行了说明,该部分指出(强调我的):

This is covered in section 5 [expr] of the draft C++ standard which says (emphasis mine):

许多期望算术或枚举类型的操作数的二进制运算符会导致转换并产生收益 结果类型以类似的方式. 目的是产生一个通用类型,它也是结果的类型. 这种模式称为通常的算术转换,其定义如下

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

并包括以下项目符号:

  • 否则,必须对两个操作数执行积分提升(4.5).61然后执行以下操作 规则应应用于提升的操作数:
  • 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.

因此,在促销后的第一种情况下,它们都具有相同的类型( int ),因此不需要进一步的转换.

So in the first case after promotions they both have the same type(int) so no further conversion is needed.

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

In the second case after promotions they do not(int and long) so a further conversion is required.

这篇关于算术转换VS积分提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆