Conversion of one data types to another data types which do not fall under numeric promotion category comes into this category.

There are broadly five types of numeric conversions:

  1. Integral to integral types
  2. Floating point to floating point types
  3. Integral to floating point types
  4. Floating point to integral types
  5. Integral and floating point to boolean type.

Safe and Unsafe Conversions

There also exist unsafe conversions. When the destination data type does not have enough size to represent source type and the conversion is made, does unsafe conversion. For example, converting double to int as values after decimal will be lost.

Based on this safe and unsafe nature, numerical conversions is again divided into three categories:

  1. Value preserving conversion: Conversion does not result in loss of data and destination and source values are same.
long value {5}; // int -> long. Value 5 will be same for both
  1. Reinterpretive conversion: Conversion that does not result in loss of data but the destination and source values may be different. unsigned/signed conversions are mostly fall into this category.
// covnersion int -> usigned int -> int. value is changed from source
// but no data loss.
std::cout << static_cast<int>(static_cast<unsigned int>(-5)) << "\n";
  1. Lossy conversion: Unsafe conversion where data maybe lost during conversion.
// conversion double -> int -> double: loss of data. 0.12 data loss.
std::cout << static_cast<double>(static_cast<int>(3.12)) << "\n";

References

  1. https://www.learncpp.com/cpp-tutorial/numeric-conversions/