A narrowing conversion is (roughly speaking) a conversion between built-in types that might need to truncate or take a modulus on some values because the destination type is not capable of suitably representing a given source value if known at compile time, or some possible source values if the value is not known at compile time.
So conversion from the known value 2
or 4
to a bool
is a narrowing conversion, since a bool
can't really represent those numbers, only 0
or 1
.
Ever since C++11, it has been illegal for a program to require a narrowing conversion on any value found within {
curly braces }
used for aggregate initialization or list-initialization.
A narrowing conversion is still allowed when the source is not in curly braces, such as your bool a = 2;
.