Namespaces
Variants
Views
Actions

Implicit conversions

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function declaration
inline specifier
Exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous
 
 

Implicit conversions are performed whenever an expression of some type T1 is used in context that does not accept that type, but accepts some other type T2; in particular:

  • when the expression is used as the argument when calling a function that is declared with T2 as parameter;
  • when the expression is used as an operand with an operator that expects T2;
  • when initializing a new object of type T2, including return statement in a function returning T2;
  • when the expression is used in a switch statement (T2 is integral type);
  • when the expression is used in an if statement or a loop (T2 is bool).

The program is well-formed (compiles) only if there exists one unambiguous implicit conversion sequence from T1 to T2.

If there are multiple overloads of the function or operator being called, after the implicit conversion sequence is built from T1 to each available T2, overload resolution rules decide which overload is compiled.

Note: in arithmetic expressions, the destination type for the implicit conversions on the operands to binary operators is determined by a separate set of rules, usual arithmetic conversions

Contents

[edit] Order of the conversions

Implicit conversion sequence consists of the following, in this order:

1) zero or one standard conversion sequence;
2) zero or one user-defined conversion;
3) zero or one standard conversion sequence.

When considering the argument to a constructor or to a user-defined conversion function, only one standard conversion sequence is allowed (otherwise user-defined conversions could be effectively chained). When converting from one built-in type to another built-in type, only one standard conversion sequence is allowed.

A standard conversion sequence consists of the following, in this order:

1) zero or one lvalue transformation;
2) zero or one numeric promotion or numeric conversion;
3) zero or one function pointer conversion;
(since C++17)
4) zero or one qualification adjustment.

A user-defined conversion consists of zero or one non-explicit single-argument constructor or non-explicit conversion function call

An expression e is said to be implicitly convertible to T2 if and only if T2 can be copy-initialized from e, that is the declaration T2 t = e; is well-formed (can be compiled), for some invented temporary t. Note that this is different from direct initialization (T2 t(e)), where explicit constructors and conversion functions would additionally be considered.

[edit] Contextual conversions

In the following contexts, the type bool is expected and the implicit conversion is performed if the declaration bool t(e); is well-formed (that is, an explicit conversion function such as explicit T::operator bool() const; is considered). Such expression e is said to be contextually converted to bool.

  • the controlling expression of if, while, for;
  • the operands of the built-in logical operators !, && and ||;
  • the first operand of the conditional operator ?:;
  • the predicate in a static_assert declaration;
  • the expression in a noexcept specifier;
(since C++20)
(since C++11)

In the following contexts, a context-specific type T is expected, and the expression e of class type E is only allowed if E has a single non-explicit user-defined conversion function to an allowable type (until C++14)there is exactly one type T among the allowable types such that E has non-explicit conversion functions whose return types are (possibly cv-qualified) T or reference to (possibly cv-qualified) T, and e is implicitly convertible to T (since C++14). Such expression e is said to be contextually implicitly converted to the specified type T. Note that explicit conversion functions are not considered, even though they are considered in contextual conversions to bool. (since C++11)

  • the argument of the delete-expression (T is any object pointer type);
  • integral constant expression, where a literal class is used (T is any integral or unscoped enumeration type, the selected user-defined conversion function must be constexpr);
  • the controlling expression of the switch statement (T is any integral or enumeration type).
#include <cassert>
 
template<typename T>
class zero_init
{
    T val;
public:
    zero_init() : val(static_cast<T>(0)) { }
    zero_init(T val) : val(val) { }
    operator T&() { return val; }
    operator T() const { return val; }
};
 
int main()
{
    zero_init<int> i; assert(i == 0);
    i = 7; assert(i == 7);
    switch(i) { }     // error until C++14 (more than one conversion function)
                      // OK since C++14 (both functions convert to the same type int)
    switch(i + 0) { } // always okay (implicit conversion)
}

[edit] Value transformations

Value transformations are conversions that change the value category of an expression. They take place whenever an expression appears as an operand of an operator that expects an expression of a different value category.

[edit] Lvalue to rvalue conversion

A glvalue of any non-function, non-array type T can be implicitly converted to a prvalue of the same type. If T is a non-class type, this conversion also removes cv-qualifiers. If the glvalue has the type std::nullptr_t, the resulting prvalue is the null pointer constant nullptr.

Unless encountered in unevaluated context (in an operand of sizeof, typeid, noexcept, or decltype), this conversion effectively copy-constructs a temporary object of type T using the original glvalue as the constructor argument, and that temporary object is returned as a prvalue.

This conversion models the act of reading a value from a memory location into a CPU register.

If the object to which the glvalue refers contains an indeterminate value (such as obtained by default initializing a non-class automatic variable), the behavior is undefined

except if the indeterminate value is of possibly cv-qualified unsigned character type which was not cached in a CPU register, or, formally:

  • its storage duration was static or thread-local;
  • or a pointer to it was constructed;
  • or it was bound to a reference.

The behavior is also implementation-defined (rather than undefined) if the glvalue contains a pointer value that was invalidated by delete.

(since C++11)

[edit] Array to pointer conversion

An lvalue or rvalue of type "array of N T" or "array of unknown bound of T" can be implicitly converted to a prvalue of type "pointer to T". If the array is a prvalue, temporary materialization occurs. (since C++17) The resulting pointer refers to the first element of the array (see array to pointer decay for details)

Temporary materialization

A prvalue of any complete type T can be converted to an xvalue of the same type T. This conversion initializes a temporary object of type T from the prvalue by evaluating the prvalue with the temporary object as its result object, and produces an xvalue denoting the temporary object. If T is a class or array of class type, it must have an accessible and non-deleted destructor.

struct S { int m; };
int i = S().m; // member access expects glvalue as of C++17;
               // S() prvalue is converted to xvalue

Temporary materialization occurs in the following situations:

  • when binding a reference to a prvalue;
  • when performing a member access on a class prvalue;
  • when performing an array-to-pointer conversion (see above) or subscripting on an array prvalue;
  • when initializing an object of type std::initializer_list<T> from a braced-init-list;
  • when typeid is applied to a prvalue (this is part of an unevaluated expression);
  • when sizeof is applied to a prvalue (this is part of an unevaluated expression);
  • when a prvalue appears as a discarded-value expression.
(since C++17)

[edit] Function to pointer

An lvalue of function type T can be implicitly converted to a prvalue pointer to that function. This does not apply to non-static member functions because lvalues that refer to non-static member functions do not exist.

[edit] Numeric promotions

[edit] Integral promotion

prvalues of small integral types (such as char) may be converted to prvalues of larger integral types (such as int). In particular, arithmetic operators do not accept types smaller than int as arguments, and integral promotions are automatically applied after lvalue-to-rvalue conversion, if applicable. This conversion always preserves the value.

The following implicit conversions are classified as integral promotions:

  • signed char or signed short can be converted to int;
  • unsigned char or unsigned short can be converted to int if it can hold its entire value range, and unsigned int otherwise;
  • char can be converted to int or unsigned int depending on the underlying type: signed char or unsigned char (see above);
  • wchar_t, char16_t, and char32_t can be converted to the first type from the following list able to hold their entire value range: int, unsigned int, long, unsigned long, long long, unsigned long long;
  • an unscoped enumeration type whose underlying type is not fixed can be converted 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, extended integer types (in size order, signed given preference over unsigned) (since C++11). If the value range is greater, no integral promotions apply;
  • an unscoped enumeration type whose underlying type is fixed can be converted to its underlying type, and, if the underlying type is also subject to integral promotion, to the promoted underlying type. Conversion to the unpromoted underlying type is better for the purposes of overload resolution;
(since C++11)
  • a bit field type can be converted to int if it can represent entire value range of the bit field, otherwise to unsigned int if it can represent entire value range of the bit field, otherwise no integral promotions apply;
  • the type bool can be converted to int with the value false becoming 0 and true becoming 1.

Note that all other conversions are not promotions; for example, overload resolution chooses char -> int (promotion) over char -> short (conversion).

[edit] Floating-point promotion

A prvalue of type float can be converted to a prvalue of type double. The value does not change.

[edit] Numeric conversions

Unlike the promotions, numeric conversions may change the values, with potential loss of precision.

[edit] Integral conversions

A prvalue of an integer type or of an unscoped enumeration type can be converted to any other integer type. If the conversion is listed under integral promotions, it is a promotion and not a conversion.

  • If the destination type is unsigned, the resulting value is the smallest unsigned value equal to the source value modulo 2n
    where n is the number of bits used to represent the destination type.
That is, depending on whether the destination type is wider or narrower, signed integers are sign-extended[footnote 1] or truncated and unsigned integers are zero-extended or truncated respectively.
  • 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. (Note that this is different from signed integer arithmetic overflow, which is undefined).
  • If the source type is bool, the value false is converted to zero and the value true is converted to the value one of the destination type (note that if the destination type is int, this is an integer promotion, not an integer conversion).
  • If the destination type is bool, this is a boolean conversion (see below).

[edit] Floating-point conversions

A prvalue of a floating-point type can be converted to a prvalue of any other floating-point type. If the conversion is listed under floating-point promotions, it is a promotion and not a conversion.

  • If the source value can be represented exactly in the destination type, it does not change.
  • If the source value is between two representable values of the destination type, the result is one of those two values (it is implementation-defined which one, although if IEEE arithmetic is supported, rounding defaults to nearest).
  • Otherwise, the behavior is undefined.

[edit] Floating–integral conversions

  • A prvalue of floating-point type can be converted to a prvalue of any integer type. The fractional part is truncated, that is, the fractional part is discarded. If the value cannot fit into the destination type, the behavior is undefined (even when the destination type is unsigned, modulo arithmetic does not apply). If the destination type is bool, this is a boolean conversion (see below).
  • A prvalue of integer or unscoped enumeration type can be converted to a prvalue of any floating-point type. If the value cannot be represented correctly, it is implementation defined whether the closest higher or the closest lower representable value will be selected, although if IEEE arithmetic is supported, rounding defaults to nearest. If the value cannot fit into the destination type, the behavior is undefined. If the source type is bool, the value false is converted to zero, and the value true is converted to one.

[edit] Pointer conversions

  • A null pointer constant (see NULL), can be converted to any pointer type, and the result is the null pointer value of that type. Such conversion (known as null pointer conversion) is allowed to convert to a cv-qualified type as a single conversion, that is, it's not considered a combination of numeric and qualifying conversions.
  • A prvalue pointer to any (optionally cv-qualified) object type T can be converted to a prvalue pointer to (identically cv-qualified) void. The resulting pointer represents the same location in memory as the original pointer value. If the original pointer is a null pointer value, the result is a null pointer value of the destination type.
  • A prvalue pointer to a (optionally cv-qualified) derived class type can be converted to a prvalue pointer to its (identically cv-qualified) base class. If the base class is inaccessible or ambiguous, the conversion is ill-formed (won't compile). The result of the conversion is a pointer to the base class subobject within the pointed-to object. The null pointer value is converted to the null pointer value of the destination type.

[edit] Pointer-to-member conversions

  • A null pointer constant (see NULL) can be converted to any pointer-to-member type, and the result is the null member pointer value of that type. Such conversion (known as null member pointer conversion) is allowed to convert to a cv-qualified type as a single conversion, that is, it's not considered a combination of numeric and qualifying conversions.
  • A prvalue pointer to member of some type T in a base class B can be converted to a prvalue pointer to member of the same type T in its derived class D. If B is inaccessible, ambiguous, or virtual base of D or is a base of some intermediate virtual base of D, the conversion is ill-formed (won't compile). The resulting pointer can be dereferenced with a D object, and it will access the member within the B base subobject of that D object. The null pointer value is converted to the null pointer value of the destination type.

[edit] Boolean conversions

A prvalue of integral, floating-point, unscoped enumeration, pointer, and pointer-to-member types can be converted to a prvalue of type bool.

The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become false. All other values become true.

A prvalue of type std::nullptr_t, including nullptr, can be converted to a prvalue of type bool in context of direct-initialization. The resulting value is false. (since C++11)

[edit] Qualification conversions

  • A prvalue of type pointer to cv-qualified type T can be converted to a prvalue pointer to a more cv-qualified same type T (in other words, constness and volatility can be added).
  • A prvalue of type pointer to member of cv-qualified type T in class X can be converted to a prvalue pointer to member of more cv-qualified type T in class X.

"More" cv-qualified means that

  • a pointer to unqualified type can be converted to a pointer to const;
  • a pointer to unqualified type can be converted to a pointer to volatile;
  • a pointer to unqualified type can be converted to a pointer to const volatile;
  • a pointer to const type can be converted to a pointer to const volatile;
  • a pointer to volatile type can be converted to a pointer to const volatile.

For multi-level pointers, the following restrictions apply: a multilevel pointer P1 which is cv1
0
-qualified pointer to cv1
1
-qualified pointer to ... cv1
n-1
-qualified pointer to cv1
n
-qualified T is convertible to a multilevel pointer P2 which is cv2
0
-qualified pointer to cv2
1
-qualified pointer to ... cv2
n-1
-qualified pointer to cv2
n
-qualified T only if

  • the number of levels n is the same for both pointers;
  • if there is a const in the cv1
    k
    qualification at some level (other than level zero) of P1, there is a const in the same level cv2
    k
    of P2;
  • if there is a volatile in the cv1
    k
    qualification at some level (other than level zero) of P1, there is a volatile in the same cv2
    k
    level of P2;
  • if at some level k the P2 is more cv-qualified than P1, then there must be a const at every single level (other than level zero) of P2 up until k: cv2
    1
    , cv2
    2
    ... cv2
    k
    .
  • same rules apply to multi-level pointers to members and multi-level mixed pointers to objects and pointers to members;
  • same rules apply to multi-level pointers that include pointers to array of known or unknown bound at any level (arrays of cv-qualified elements are considered to be identically cv-qualified themselves);
(since C++14)
  • level zero is addressed by the rules for non-multilevel qualification conversions.
char** p = 0;
const char** p1 = p; // error: level 2 more cv-qualified but level 1 is not const
const char* const * p2 = p; // OK: level 2 more cv-qualified and const added at level 1
volatile char * const * p3 = p; // OK: level 2 more cv-qual and const added at level 1
volatile const char* const* p4 = p2; // OK: 2 more cv-qual and const was already at 1
 
double *a[2][3];
double const * const (*ap)[3] = a; // OK since C++14

Note that in the C programming language, const/volatile can be added to the first level only:

char** p = 0;
char * const* p1 = p; // OK in C and C++
const char* const * p2 = p; // error in C, OK in C++

Function pointer conversions

  • A prvalue of type pointer to non-throwing function can be converted to a prvalue pointer to potentially-throwing function.
  • A prvalue of type pointer to non-throwing member function can be converted to a prvalue pointer to potentially-throwing member function.
void (*p)();
void (**pp)() noexcept = &p; // error: cannot convert to pointer to noexcept function
 
struct S
{
    typedef void (*p)();
    operator p();
};
void (*q)() noexcept = S(); // error: cannot convert to pointer to noexcept function
(since C++17)

[edit] The safe bool problem

Until the introduction of explicit conversion functions in C++11, designing a class that should be usable in boolean contexts (e.g. if(obj) { ... }) presented a problem: given a user-defined conversion function, such as T::operator bool() const;, the implicit conversion sequence allowed one additional standard conversion sequence after that function call, which means the resultant bool could be converted to int, allowing such code as obj << 1; or int i = obj;.

One early solution for this can be seen in std::basic_ios, which defines operator! and operator void*(until C++11), so that the code such as if(std::cin) {...} compiles because void* is convertible to bool, but int n = std::cout; does not compile because void* is not convertible to int. This still allows nonsense code such as delete std::cout; to compile, and many pre-C++11 third party libraries were designed with a more elaborate solution, known as the Safe Bool idiom.

The explicit bool conversion can also be used to resolve the safe bool problem

explicit operator bool() const { ... }
(since C++11)

[edit] Footnotes

  1. This only applies if the arithmetic is two's complement which is only required for the exact-width integer types. Note, however, that at the moment all platforms with a C++ compiler use two's complement arithmetic

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 616 C++11 lvalue to rvalue conversion of any uninitialized object was always UB indeterminate unsigned char is allowed
CWG 1423 C++11 std::nullptr_t is convertible to bool in both direct- and copy-initialization direct-initialization only
CWG 330 C++14 conversion from double * const (*p)[3] to double const * const (*p)[3] invalid conversion valid

[edit] See also

C documentation for Implicit conversions