[英]What are the rules governing precedence during C++11 type inference?
有关浮点/双精度类型的C ++ 11类型推断中的优先级的规则是什么,例如,从包含多个类型的表达式推断时,如下所示:
auto var = float(1) * double(1);
The result will be a double
. 结果将是
double
。 This is called floating point promotion
. 这称为
floating point promotion
。
From the standard, ISO 14882:2011 , 4.6 Floating point promotion : 从标准, ISO 14882:2011,4.6 浮点促销 :
1 A prvalue of type float can be converted to a prvalue of type double.
1 float类型的prvalue可以转换为double类型的prvalue。 The value is unchanged.
该值保持不变。
2 This conversion is called floating point promotion.2此转换称为浮点促销。
As noted by @sftrabbit, in 5. Expressions , paragraph 9 in the new standard: 正如@sftrabbit所述,在5.表达式中 ,新标准中的第9段:
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;
- 如果任一操作数具有作用域枚举类型(7.2),则不执行任何转换; 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.- 如果任一操作数的类型为long double,则另一个操作数应转换为long double。
— Otherwise, if either operand is double, the other shall be converted to double.- 否则,如果任一操作数为double,则另一个操作数应转换为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.- 否则,应对两个操作数执行整数促销(4.5)。
The type inference doesn't add anything new, the expression to the right of the '=' is evaluated as always, and the type of it is then used for the 'auto'. 类型推断不会添加任何新内容,“=”右侧的表达式将一如既往地进行计算,然后将其类型用于“auto”。
It's slightly more interesting when you look at differences between 'auto var' and 'auto & var' and similar, but that wasn't your question. 当你看到'auto var'和'auto&var'之间的差异时,它会更有趣,但这不是你的问题。
The answer depends on the types in question. 答案取决于有问题的类型。 In your specific example, the standard guarantees that
sizeof(double) >= sizeof(float)
therefore the resulting type of double * float
will always be double
. 在您的具体示例中,标准保证
sizeof(double) >= sizeof(float)
因此得到的double * float
类型将始终为double
。 (this is a rule inherited from the C language, and is generally the same in many other languages which derive from C) (这是从C语言继承的规则,并且在源自C的许多其他语言中通常是相同的)
When initialising a variable with the auto
keyword, the resulting type of the expression of the initialisation is determined - whether its from a function return, a calculation, a decltype, etc. The type depends on implicit and explicit conversions available for the types which you're using. 使用
auto
关键字初始化变量时,将确定初始化表达式的结果类型 - 无论是函数返回,计算还是decltype等。类型取决于您可用于类型的隐式和显式转换'正在使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.