Implicit conversions
Implicit conversions simplify processing of built-in types. These are intuitive casts can help increase clarity by avoiding excess code.
Implicit casts can happen whenever a type mismatch occurs, such as when calling a function or assigning a variable with slightly incompatible type. If the an implicit cast exists that can convert the source type to the destination type, that cast is performed and no error occurs. If more than one different implicit cast can be performed to match the types, the program is ill-formed because of the ambiguity. If types already match, implicit conversions are never performed.
Contents |
Order of the conversions
Implicit cast consist of a series of simpler sub-conversions. The order in which they can be applied to form the final cast is as follows:
- zero or more of generic conversions
- zero or more of numeric conversions
- zero or more of qualification conversions
Generic conversions
lvalue to rvalue
A glvalue of any non-function, non-array type T
can be implicitly converted to prvalue of the same type. If T
is a non-class type, this conversion also removes cv-qualifiers. Unless encountered in unevaluated context (in an operand of sizeof, typeid, noexcept, or decltype), this conversion copy-constructs a temporary object of type T
using the original glvalue as the constructor argument, and the temporary object is returned as a prvalue. If the glvalue has the type Template:cpp, the resulting prvalue is the null pointer constant nullptr
.
Array to pointer
A lvalue or rvalue of type "array of N
T
" or "array of unknown bound of T
" can be converted to a prvalue of type "pointer to T
". The resulting pointer refers to the first element of the array.
Function to pointer
An lvalue of function type T
can be converted to a prvalue pointer to that function. This does not apply to non-static member functions.
Numeric conversions
Integer promotion
Integer types may be converted to larger types in order to do calculations. This does not change the actual contents of the variable.
The following conversions may apply:
signed char
orsigned short
may be casted to Template:cpp.
unsigned char
orunsigned short
may be casted to Template:cpp.
char
may be casted to Template:cpp or Template:cpp depending on the underlying type - Template:cpp or Template:cpp (see above)
wchar_t
orchar16_t
orchar32_t
may be casted to the first type from the following list able to hold their entire value range: Template:cpp, Template:cpp, Template:cpp, Template:cpp.
- An enumeration type whose underlying type is not fixed may be casted to the first type from the following list able to hold their entire value range: Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, or Template:cpp. If the value range is greater, no conversions apply.
- An enumeration type whose underlying type is fixed can be converted to its underlying type. Additionally, the resulting value can be promoted to Template:cpp or Template:cpp. (since C++11)
- A bitfield type can be converted to Template:cpp if it can represent entire value range of the bitfield, otherwise to Template:cpp if it can represent entire value range of the bitfield, otherwise no conversions apply.
Integral conversions
If no integer promotion rules apply, an integral type may be converted to another integral type, potentially involving changes to the value of the variable. The destination type may have smaller range. The conversion is done according to the following rules.
- If the destination type is unsigned, the resulting value is the value modulo 2n where n is the number of bits used to represent the destination type.
- If the destination type is signed, the value does not change if the source integer can be represented in the destination type. Otherwise the result is implementation-defined.
-
bool
may be casted to an integer value. Template:cpp then becomes Template:cpp and Template:cpp - zero.
-
Floating point promotion
A value of float
type may be converted to Template:cpp. This does not change the value of the variable.
Floating point - integer conversions
- A floating point number may be converted to an integer number. The fractional part is truncated. If the value can not fit into the destination type, the behavior is undefined.
- Integer or enumeration types may be converted to floating point types. If the value can not be represented correctly, it is implementation defined whether the higher or lower representable value will be selected. If the value can not fit into the destination type, the behavior is undefined.
Pointer conversions
- Null pointer constant
nullptr
can be casted to a pointer which then holds null pointer (since C++11)
- Null pointer constant
- A pointer to any type can be casted to a pointer holding Template:cpp. The constness of the pointed type does not change.
- A pointer to a class type can be converted to a pointer to its base class. If the destination base class is ambiguous or inaccessible the program is ill formed. The constness of the pointed type does not change.
Boolean conversions
Integer, floating point and enumeration types can be converted to a Template:cpp type. A Value of zero becomes Template:cpp and any nonzero value becomes Template:cpp.
A nullptr
can be casted to a Template:cpp type. The resulting value is Template:cpp. (since C++11)
Qualification conversions
For more information about cv-qualification see cv qualifiers
The following conversions are allowed:
- unqualified type can be converted to
const
- unqualified type can be converted to
volatile
- unqualified type can be converted to
const volatile
-
const
type can be converted toconst volatile
-
volatile
type can be converted toconst volatile
- unqualified type can be converted to