在C ++ 11类型推理中,管理优先级的规则是什么? [英] What are the rules governing precedence during C++11 type inference?

查看:152
本文介绍了在C ++ 11类型推理中,管理优先级的规则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于float / double类型的C ++ 11类型推理,例如当从包含多个类型的表达式推导时,什么规则优先级如此:

  auto var = float(1)* double(1); 


解决方案

结果将是 double 。这称为浮点促销



从标准 ISO 14882:2011 4.6浮点促销


1 float类型的prvalue可以转换为double类型的prvalue。该值不变。

2此转换称为浮点促销。







如@sftrabbit所述,在 5。表达式,新标准中的第9段:


许多希望算术或枚举类型的操作数导致转换和结果类型以类似的方式。目的是产生一个共同的类型,这也是结果的类型。



这种模式被称为通常的算术转换,定义如下: p>

- 如果任一操作数是范围枚举类型(7.2),则不执行转换;如果另一个操作数不具有相同的类型,则表达式是不成形的。

- 如果任一操作数的类型为long double,则另一个操作数将转换为long double。

- 否则,如果任一操作数是双精度,则另一个将被转换为双精度。

- 否则,如果任一操作数是浮点型,则另一个将被转换为浮点型。

- 否则,将对两个操作数执行整数提升(4.5)。



What are the rules governing precedence in C++11 type inference for float/double types, for example, when inferring from an expression containing multiple types, like so:

auto var = float(1) * double(1);

解决方案

The result will be a double. This is called floating point promotion.

From the standard, ISO 14882:2011, 4.6 Floating point promotion:

1 A prvalue of type float can be converted to a prvalue of type double. The value is unchanged.
2 This conversion is called floating point promotion.


As noted by @sftrabbit, in 5. Expressions, paragraph 9 in the new standard:

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.

这篇关于在C ++ 11类型推理中,管理优先级的规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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