Why is this a narrowing conversion?
Because the definition includes (with my emphasis):
C++11 8.5.4/7 A narrowing conversion is an implicit conversion
[…] from an integer type […] to a floating-point type, except where the source is a constant expression and the actual value after conversion will fit into the target type and will produce the original value when converted back to the original type.
u
is not a constant expression, so it’s a narrowing conversion whether or not all possible values of the source type might be representable in the target type.
Isn’t every
unsigned
perfectly representable as adouble
?
That’s implementation defined. In the common case of 32-bit unsigned
and double
with a 52-bit mantissa, that is the case; but some implementations have larger unsigned
and/or smaller double
representations, so code that depends on that assumption is not portable.