Implicit conversions
From Cppreference
Implicit conversions simplify processing of built-in types. These are intuitive casts that help to write less code preserving clarity of it.
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 |
[edit] Order of the conversions
Implicit cast consists from 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
When doing arithmetic operations, all small integer types are automatically converted to int or unsigned int. This is called integer promotion. 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. (C++11 feature)
- 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.
- bool can be casted to int. true then becomes 1 and false - zero.
[edit] Floating point promotion
When doing floating point operations, all float objects are converted to double objects unless explicitly stated otherwise.
[edit] Floating point - integer conversions
- Floating point numbers may be converted to 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 (C++11 feature)
- 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
A integer, floating point and enumeration type can be converted to a bool type. Value of zero becomes false, any nonzero value becomes true.
A nullptr can be casted to a bool type. The resulting value is false. (C++11 feature)
[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