Implicit Conversion (Coercion)

Implicit conversion (aka. type coercion) happens when C++ expects a value of one type, and you give a value of different type:

  • If the value can be converted implicit conversion occurs.
  • If the value cannot be converted compile error occurs.

Note that type conversion produces a new value of the desired type and DOES NOT change the value or type of the object being converted (learncpp.com).

Numeric promotions

Numeric promotion converts a smaller data type to a larger data type (e.g. float to double), but the rule is only applied to certain data types. (For other data types that are not covered by numeric promotion, see numeric conversion below.)

Numeric promotions are guaranteed to preserve the value before and after promotion.

Integral promotions

  • signed char and signed short can be converted to int
  • unsigned char and unsigned short can be converted to int OR unsigned int (depending on whether the architecture's int can hold the entire range of the original type)
  • bool can be converted to int (false becomes 0, true becomes 1)

Floating-point promotions

  • float can be converted to double

Numeric conversions

Numeric conversion works like numeric promotion, but applies to a wider range of data types. (The distinction between numeric promotion and conversion is "mostly academic," but matters in function overload resolution, learncpp.com.)

  • an integer type can be converted to any other integer types (e.g. int to short, or int to long)
  • a floating-point type can be converted to any other floating-point types (e.g. double to float, or double to long double)
  • an integer type can be converted to any floating-point types (e.g. int to double)
  • a floating-point type can be converted to any integer types (e.g. double to int)
  • an integer type or a floating-point type can be converted to bool (non-zero value becomes true, zero value becomes false)

Narrowing conversions

Note that conversions from "larger" or "more precise" data types to "smaller" or "less precise" data types are called narrowing conversions.

You may lose data/precision in this case. For example, when you store a double value 3.123 to an int, you will get 3.

Arithmetic conversions

Operations with most binary operators (e.g. +, -, <, >, &&) normally require that the operands' types to be the same.

If the types are not the same, then the operand with the "lower priority" is converted to match the type of the operand with the "higher priority" (e.g. operating an int with a double will produce a double value):

  • long double (highest)
  • double
  • float
  • unsigned long long
  • long long
  • unsigned long
  • long
  • unsigned int
  • int (lowest)

Pointer conversions

  • pointer to a subclass can be converted as pointer to its superclass ("upcasting")
  • pointer to any type can be converted as pointer with type void*
  • pointer with type void* can be converted as pointer to any type (with explicit cast)
  • zero integer expression can be converted as the null pointer

Reference conversions

  • reference to a subclass — can be converted as reference to its superclass ("upcasting")

References