Assigning a value to a type with a greater range usually poses no
problem. If shorter range or different type, maybe there are some
problems.
Assigned to a bool, zero will convert to false, and nonzero will convert
to true.
When conversion, maybe lost precision(double -> float, long long
->float) loss fragment(float -> i) or Undefine (int i = 666, then
char c = i;) Detail example can be seen in my evernote bookmark.
A implicit conversion will happened when you call a function.(Just like you
use assignment operator=). Below they all compile successfully. You can use
=delete keyword to resolve this questions.
In C++, introduce brace initialization {}, It will not allow narrowing
happen. But in g++, it just show a -Wnarrowing message, Anyway, I think
that It’s helpful.
When an operation involves two types, the smaller is converted to the larger.
Adding an int to a float, int is converted to float type. (You have to do it,
because two types have the different inside representations)
In an expression, C++ makes two kinds of automatic conversion. 1) Some
type are automatically converted whenever they occur. 2) Some
type are converted when they are combined with other types in an
expression.
C++ converts bool, char, unsighed char, signed char and short to int.
Because int type is generally chosen to be the computer’s most natural
type. It does calculations faster for that type. It’s called integerpromotions.
unsigned short convert to int if short is shorter than int, if they have the
same size. unsigned short convert to unsigned int. So no data loss in
promoting.