是"通常的算术转换"和"整型的提升和QUOT;一样的东西? [英] Are the "usual arithmetic conversions" and the "integer promotions" the same thing?

查看:140
本文介绍了是"通常的算术转换"和"整型的提升和QUOT;一样的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是通常的算术转换和整型的提升是一回事吗?我已阅读了通常的算术转换是用来做一个前pression相同类型的操作数,而整型的提升用于促进比 INT INT ,但在的 MSDN 中这两个概念都放置在通常的算术转换而已。

Are the "usual arithmetic conversions" and the "integer promotions" the same thing? I have read that the "usual arithmetic conversions" are used to make the operands of an expression the same type, while "integer promotions" are used to promote the types smaller than int to int, but in MSDN both of these concepts are placed under "usual arithmetic conversions" only.

推荐答案

通常的算术转换的涉及在某些情况下积分的推广,但是这是两个独立的机制:

The usual arithmetic conversions involve integral promotion under certain circumstances, but these are two separate mechanisms:

[C ++ 14:5/10]:许多二元运算符意想不到的算术或枚举类型转换的原因和产量结果类型的操作数以类似的方式。目的是得到一个常见的​​类型,这也是结果的类型。 这种模式被称为的通常的算术转换的,它的定义如下:

[C++14: 5/10]: 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:


      
  • 如果一个操作数的范围枚举类型(7.2)的,不会进行任何转换;如果另一个操作数没有相同的类型,前pression是病态的。

  •   
  • 如果一个操作数的类型长双,其他应转换为长双
      
  • 否则,如果操作数是双击,其他应转换为双击

  •   
  • 否则,如果操作数是浮动,其他应转换为浮动

  •   
  • ,否则,积分促销活动(4.5),应执行在两个操作数 61 那么下面的规则应适用于推广操作数:
      

        
    • 如果两个操作数具有相同的类型,没有进一步的转换是必要的。

    •   
    • 否则,如果两个操作数有符号整数类型或两者都有的无符号整数类型,具有较小的整数转换等级类型的操作数应转换为操作数的类型有更大的等级。

    •   
    • 否则,如果已经无符号整型操作数的秩大于或等于另一个操作数的类型的秩,用符号整型操作数应被转换为与无符号整型操作数的类型。

    •   
    • 否则,如果有符号整数类型的操作数的类型可以重新present所有无符号整型操作数的类型的值,用无符号整型操作数将被转换成的类型操作有符号整数类型。

    •   
    • 否则,两个操作数应转换为对应于与符号整型的操作数的类型的无符号整型。

    •   
    • If either operand is of scoped enumeration type (7.2), no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.
    • If either operand is of type long double, the other shall be converted to long double.
    • Otherwise, if either operand is double, the other shall be converted to double.
    • Otherwise, if either operand is float, the other shall be converted to float.
    • 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.

      该措辞在很多C11一样的。

      The wording is much the same in C11.

      请注意,积分促销活动也可能在什么都没有做与通常的算术转换的情况下进行,例如有点移位的LHS操作数;最终,这一切都是为什么两个机制有自己独特的名字!

      Note that the integral promotions may also be performed under circumstances that have nothing to do with the usual arithmetic conversions, e.g. the LHS operand of a bit-shift; ultimately, all this is why the two mechanisms have their own distinct names!

      这篇关于是"通常的算术转换"和"整型的提升和QUOT;一样的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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