Data Types Conversions


C++ automatically performs certain conversions. These are categorised and explained in this section.

Integral Promotions

A character, a short integer, an enumerated type identifier or a bit field are automatically promoted to integer type as required. If a signed integer can be used to represent the value being promoted, the original value is converted to integer. If an unsigned integer is required to represent the original object, the promotion is to unsigned integer.

Integral Conversions

When an integer is converted from signed to unsigned type, if no truncation occurs, the resultant value is the unsigned integer equivalent of the original signed value. If truncation occurs (going from int to unsigned short int say), the resultant value is the original value modulo 2<<n, where n is the number of bits used to represent the unsigned quantity. See the section titled numeric representation, (subsection: Two's Complement Arithmetic) for a pictorial explanation of this topic.

When an integer is converted from unsigned to signed type, if the target of the conversion is capable of holding the bit pattern, no change to the pattern takes place - the result is merely reinterpreted as a signed value. If the target is unable to hold the new value, the result is implementation dependent.

Numbers

Single precision floating point (float) values may be converted to double precision floating point values (double) without changing the numeric value during the conversion. Conversion from a lower to a higher precision floating point representation without change to the numeric value is always possible. When converting from a more precise to less precise representation (such as from double to float), if the number is exactly representable then no change takes place. If not, the next higher or lower representation of the number is used (in the lower precision system). If the result of the conversion is out of range in the lower precision system, the effect is undefined.

Numbers to Integral

The conversion of a floating point quantity to an integral value causes the decimal portion of the floating point number to be discarded. The direction of the truncation of a negative number is machine dependent.

The precision of the conversion of integers to floating point also varies according to the machine architecture. Integers are exactly representable on modern Intel platforms.

Arithmetic Conversions

Conversions that are made during the course of arithmetic expression evaluation are performed in accordance with the rules stated below. The rules are applied in the order of precedence of their listing and operands refers to the operands of a binary arithmetic operator.

  1. If either of the operands is of the type long double, the other operand is converted to long double.

  2. If either of the operands is of the type double, the other is converted to double.

  3. If either of the operands is of type float, the other is converted to float.

  4. Integral promotions are performed on the operands of an arithmetic operator.

  5. If one operand is of type long int and the other is of type unsigned int then if the value of the unsigned integer is always representable as a long integer then it is converted to long integer.

  6. Otherwise, both operands are converted to unsigned long int.

  7. If either of the operands is of type long, the other is converted to long.

  8. If either of the operands is of type unsigned, the other is converted to unsigned.

  9. If none of the above applies, both operands are of the type int.

Pointer Conversions

The following standard conversions apply to pointers.

Reference Conversions

Reference conversion may take place when references are:

A reference to an instance of a class T may be converted to a reference to an instance of an accessible, unambiguous, base class of T. The resultant reference is to the sub-object contained in the derived class.

Pointers to Members

The following standard conversions apply to pointers to members.