Implicit type conversions are done by compiler where it is possible. This is also called as automatic type conversion or coercion.
C++ language core provides some rules known as standard conversions to specify how some fundamental data types (and other such as pointers, references etc.) are converted to another type.
There are 14 different standard conversions which are broadly divided into 5 groups.
- Numeric promotions: converting
floattodouble,chartoint,booltointetc. - Numeric conversions: converting
floattointetc. - Qualification conversions:
<tobe added after understanding> - Value conversions:
<tobe added after understanding> - Pointer conversions: Conversions from
std::nullptrto other point types.
If compiler can’t do a conversion, it raises compilation error. For example, converting a string to int as shown below.
int value {"12"};Brace initialization also raises error for narrow conversions, such as float to int as shown below.
int value {3.0};