简体   繁体   English

用大括号从double初始化float

[英]float initialization from double with braces

Why does the compiler (clang,gcc) not warn about narrowing conversions when doing this 为什么编译器(clang,gcc)在执行此操作时没有警告缩小转换

float a{3.1231231241234123512354123512341235123541235};
float a = {double(3.1231231241234123512354123512341235123541235)}

I expected a warning because I do explicit value-initialization with braces. 我期待一个警告,因为我使用大括号进行显式值初始化。 Following this answer Link it should spit out an error. 按照这个答案链接它应该吐出一个错误。

Compilation here 汇编在这里

[dcl.init.list]/§7 (standard draft) [dcl.init.list] /§7 (标准草案)

A narrowing conversion is an implicit conversion 缩小转换是隐式转换

... ...

  • from long double to double or float, or from double to float, except where the source is a constant expression and the actual value after conversion is within the range of values that can be represented (even if it cannot be represented exactly) , or 从long double到double或float,或从double到float, 除非source是常量表达式,转换后的实际值在可以表示的值范围内(即使它不能精确表示) ,或者

... ...

Both expressions 3.14159 and double(3.141) are constant expressions and the value is within the range of values representable by float . 表达式3.14159double(3.141)都是常量表达式,值在float表示的值范围内。 Therefore the conversion isn't narrowing as defined by the standard and there is no requirement to warn about the conversion. 因此,转换不会像标准所定义的那样缩小 ,并且不需要警告转换。


but it does not give a warning also for longer inputs 但是对于更长的输入,它也不会发出警告

Sure it does , as long as the value is outside of the range of values representable by float . 确实如此 ,只要该值超出float表示的值范围。

Because the source is a constant expression and overflow does not occur for these cases, then narrowing conversion error won't be triggered. 因为源是常量表达式并且在这些情况下不会发生溢出,所以不会触发缩小转换错误。

(emphasis mine) (强调我的)

conversion from a long double to double or to float and conversion from double to float, except where the source is a constant expression and overflow does not occur 从long double转换为double或者float并从double转换为float, 除非source是常量表达式并且不会发生溢出

If you use it with a double variable(ie a non-constant-expression) or a constant with big value which causes overlow, the diagnostic message will be generated. 如果将它与double变量(即非常量表达式)或具有较大值的常量一起使用而导致过低,则将生成诊断消息。 eg 例如

double d = 3.14159;
float a {d}; // non-constant-expression cannot be narrowed from type 'double' to 'float' in initializer list

EDIT (for longer input) 编辑 (更长输入)

Because even if the value cannot be represented exactly by float , overflow still doesn't occur, then it's allowed. 因为即使该值不能由float精确表示,溢出仍然不会发生,那么它是允许的。

$8.6.4/7.2 List-initialization (emphasie mine) $ 8.6.4 / 7.2列表初始化 (强调我的)

from long double to double or float, or from double to float, except where the source is a constant expression and the actual value after conversion is within the range of values that can be represented ( even if it cannot be represented exactly ), or 从long double到double或float,或从double到float,除非source是常量表达式,转换后的实际值在可以表示的值范围内( 即使它不能精确表示 ),或者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM