Implicit conversions
From Cppreference
Implicit conversions simplify processing of built-in types. These 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 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 |
[edit] Order of the conversions
Implicit casts 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
[edit] Generic conversions
[edit] 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 std::nullptr_t, the resulting prvalue is the null pointer constant nullptr.
[edit] 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.
[edit] 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.
[edit] Numeric conversions
[edit] 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 or signed short may be casted to int.
- unsigned char or unsigned short may be casted to unsigned int.
- char may be casted to int or unsigned int depending on the underlying type - signed char or unsigned char (see above)
- wchar_t or char16_t or char32_t may be casted to the first type from the following list able to hold their entire value range: int, unsigned int, long, unsigned long.
- 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: int, unsigned int, long, unsigned long, long long, or unsigned long long. 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 int or unsigned int. (since C++11)
- A bitfield type can be converted to int if it can represent entire value range of the bitfield, otherwise to unsigned int if it can represent entire value range of the bitfield, otherwise no conversions apply.
[edit] 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 unsigned, the resulting value is the value modulo 2n
- 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. true then becomes 1 and false - zero.
[edit] Floating point promotion
A value of float type may be converted to double. This does not change the value of the variable.
[edit] 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.
[edit] Pointer conversions
- Null pointer constant nullptr can be casted to a pointer which then holds null pointer (since C++11)
- A pointer to any type can be casted to a pointer holding void. 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.
[edit] Boolean conversions
Integer, floating point and enumeration types can be converted to a bool type. A Value of zero becomes false and any nonzero value becomes true.
A nullptr can be casted to a bool type. The resulting value is false. (since C++11)
[edit] 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 to const volatile
- volatile type can be converted to const volatile