c++ - "usual arithmetic conversions"和 "integer promotions"是一回事吗?

标签 c++ c integer-promotion

“通常的算术转换”和“整数提升”是一回事吗?我读过“通常的算术转换”用于使表达式的操作数具有相同类型,而“整数提升”用于将小于 int 的类型提升为 int,但在 MSDN这两个概念都只放在“通常的算术转换”下。

最佳答案

没有。

通常的算术转换在某些情况下涉及整数提升,但这是两种独立的机制:

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

C11 中的措辞大致相同。

请注意,整数提升也可以在与通常的算术转换无关的情况下执行,例如移位的 LHS 操作数;最终,这就是为什么这两种机制有自己不同的名称!

关于c++ - "usual arithmetic conversions"和 "integer promotions"是一回事吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28142902/

相关文章:

c - 如何将 python3 c api 用于命令行驱动的应用程序?

C++ 否定和重载决议

c++ - 在 C++ 中说 "promote everything to floating before calculation"的确定性方式

c++ - 多个 tcp 套接字,一个停止

c++ - 是什么导致这种在 try block 展开期间抛出析构函数的奇怪行为?

c++ - 使用调用外部函数的函数加载由 MATLAB Coder 生成的 DLL

c++ - 具有整数提升的位运算

c++ - 二维数组中的指针

c - 空指针的动态数组

c - GCC 不合格?