Are the “usual arithmetic conversions” and the “integer promotions” the same thing?

不羁的心 提交于 2019-11-26 17:23:38

问题


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.


回答1:


No.

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

[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:

  • 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.

The wording is much the same in C11.

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!



来源:https://stackoverflow.com/questions/28142902/are-the-usual-arithmetic-conversions-and-the-integer-promotions-the-same-thi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!