List initialization (the {...}
syntax) doesn't allow narrowing conversions. For example, trying to list-initialize an int i
with 3.14f
holds a compilation error, as conversion from floating point values to integers is narrowing:
<source>:11:32: error: narrowing conversion of '3.1400001e+0f' from 'float' to 'int' inside { } [-Wnarrowing]
int i{3.14f};
^
With that said, why is it possible to construct a float f
with 3.14
, which is of type double
? (Conversion from double
to float
is considered a narrowing one.) Doing the following:
float f{3.14};
Holds no compilation errors.
extern double y; struct X { float f; } x{y};
errors out for me. – Vmail