gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 00:33:37 26 4
gpt4 key购买 nike

“通常的算术转换”和“整数提升”是一回事吗?我读过“通常的算术转换”用于使表达式的操作数具有相同类型,而“整数提升”用于将小于 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com