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
If you use it with a double
variable(i.e. a non-constant-expression) or a constant with big value which causes overlow, the diagnostic message will be generated. e.g.
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.
$8.6.4/7.2 List-initialization
(emphasie mine)
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