Conversions

Implicit conversion

A conversion refers to a value of one type being produced that corresponds to a value of another type. For example, the int value 3 can be converted to the double value 3.0. Conversions between arithmetic types are usually implicit conversions which means that the conversion happens automatically without any additional code.

The following expressions all perform implicit conversion before the operator is executed:

Expression Implicit conversions
double n = 0 00.0
int n = 3.25 3.253
2 * 4.51 22.0
asin(1) 11.0

For arithmetic operators (such as addition and multiplication), the general rule is that the integral value will be converted to a floating-point value if either operand is a floating-point value; this is part of what's known as the usual arithmetic conversions.

For any assignment or function call where the supplied value is not of the same type as the parameter or variable being assigned to, then an implicit conversion will be performed if the two types are compatible.

An integral value can usually be converted to a floating-point value without losing any information; however, a conversion from a floating-point value to an integral value will result in the fractional part of the value being discarded.

Explicit conversion

Sometimes the behavior of an implicit conversion can be undesirable; if that is the case, then an explicit conversion can be used instead:

Expression Resulting value
2 * int(2.4) 4
double(5) + 1 6.0
int(4 * atan(1)) 3

The explicit conversion notation in the form of T(E), where T is a type and E is an expression, is called functional cast notation: it attempts to produce a new value of the specified type by intepreting the value of the specified expression.

An alternative to the functional cast notation is the C-style cast notation, it's form is (T)E. For example, the expression (int)3.5 results in the value 3.

Both forms of cast notation have the same effect, it is your preference on which to use. When using either cast notation it is said that this type is being "casted to" this other type; therefore "casting" and "converting" are synonomous terms.

Standard conversions

There are many standard conversions in C++, they are considered during implicit conversion; some of them are described in the following table:

Conversion Description
boolchar false'\x00', true'\x01'
boolint false0, true1
booldouble false0.0, true1.0
charbool '\x00'false, true otherwise
charint See the ASCII Chart
chardouble See the ASCII Chart
intbool 0false, true otherwise
intchar See the ASCII Chart
intdouble Same value as if .0 was appended to the decimal literal
doublebool 0.0false, true otherwise
doublechar See the ASCII Chart
doubleint Integral part in resulting value, fractional part gets discarded.

Conversions with strings

Starting with C++11, the following functions were introduced to convert strings to and from arithmetic types:

Expression Description
to_string(v) Returns a string value representing the integral or floating-point value v
stoi(s) Returns an int value representing the string value s
stod(s) Returns a double value representing the string value s