[conv] (original) (raw)

7 Expressions [expr]

7.3 Standard conversions [conv]

7.3.1 General [conv.general]

Standard conversions are implicit conversions with built-in meaning.

[conv] enumerates the full set of such conversions.

Astandard conversion sequence is a sequence of standard conversions in the following order:

[Note 1:

A standard conversion sequence can be empty, i.e., it can consist of no conversions.

— _end note_]

A standard conversion sequence will be applied to an expression if necessary to convert it to an expression having a required destination type and value category.

[Note 2:

Expressions with a given type will be implicitly converted to other types in several contexts:

— _end note_]

An expression E can beimplicitly converted to a type T if and only if the declaration T t=E; is well-formed, for some invented temporary variable t ([dcl.init]).

Certain language constructs require that an expression be converted to a Boolean value.

An expression E appearing in such a context is said to becontextually converted to bool and is well-formed if and only if the declaration bool t(E); is well-formed, for some invented temporary variable t ([dcl.init]).

Certain language constructs require conversion to a value having one of a specified set of types appropriate to the construct.

An expression E of class type C appearing in such a context is said to becontextually implicitly converted to a specified type T and is well-formed if and only if E can be implicitly converted to a type Tthat is determined as follows:C is searched for non-explicit conversion functions whose return type is cv T or reference to cv T such that T is allowed by the context.

There shall be exactly one such T.

The effect of any implicit conversion is the same as performing the corresponding declaration and initialization and then using the temporary variable as the result of the conversion.

The result is an lvalue if T is an lvalue reference type or an rvalue reference to function type ([dcl.ref]), an xvalue if T is an rvalue reference to object type, and a prvalue otherwise.

The expression Eis used as a glvalue if and only if the initialization uses it as a glvalue.

[Note 3:

For class types, user-defined conversions are considered as well; see [class.conv].

In general, an implicit conversion sequence ([over.best.ics]) consists of a standard conversion sequence followed by a user-defined conversion followed by another standard conversion sequence.

— _end note_]

[Note 4:

There are some contexts where certain conversions are suppressed.

For example, the lvalue-to-rvalue conversion is not done on the operand of the unary & operator.

Specific exceptions are given in the descriptions of those operators and contexts.

— _end note_]

7.3.2 Lvalue-to-rvalue conversion [conv.lval]

A glvalue of a non-function, non-array type Tcan be converted to a prvalue.44

If T is an incomplete type, a program that necessitates this conversion is ill-formed.

If Tis a non-class type, the type of the prvalue is the cv-unqualified version of T.

Otherwise, the type of the prvalue is T.45

When an lvalue-to-rvalue conversion is applied to an expression E, and either

the value contained in the referenced object is not accessed.

[Example 1: struct S { int n; };auto f() {S x { 1 };constexpr S y { 2 };return [&](bool b) { return (b ? y : x).n; };} auto g = f();int m = g(false); int n = g(true); — _end example_]

The result of the conversion is determined according to the following rules:

7.3.3 Array-to-pointer conversion [conv.array]

An 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 temporary materialization conversion ([conv.rval]) is applied.

The result is a pointer to the first element of the array.

7.3.4 Function-to-pointer conversion [conv.func]

An lvalue of function type T can be converted to a prvalue of type “pointer to T”.

The result is a pointer to the function.46

7.3.5 Temporary materialization conversion [conv.rval]

A prvalue of type T can be converted to an xvalue of type T.

This conversion initializes a temporary object ([class.temporary]) 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.

T shall be a complete type.

[Note 1:

If T is a class type (or array thereof), it must have an accessible and non-deleted destructor; see [class.dtor].

— _end note_]

[Example 1: struct X { int n; };int k = X().n; — _end example_]

7.3.6 Qualification conversions [conv.qual]

A qualification-decomposition of a type Tis a sequence of and such that T is“ ⋯ U” for n ≥ 0,where each is a set of cv-qualifiers ([basic.type.qualifier]), and each is “pointer to” ([dcl.ptr]), “pointer to member of class of type” ([dcl.mptr]), “array of ”, or “array of unknown bound of” ([dcl.array]).

If designates an array, the cv-qualifiers on the element type are also taken as the cv-qualifiers of the array.

[Example 1:

The type denoted by the type-id const int **has three qualification-decompositions, taking Uas “int”, as “pointer to const int”, and as “pointer to pointer to const int”.

— _end example_]

The n-tuple of cv-qualifiers after the first one in the longest qualification-decomposition of T, that is,, is called thecv-qualification signature of T.

Two types T1 and T2 are similar if they have qualification-decompositions with the same nsuch that corresponding components are either the same or one is “array of ” and the other is “array of unknown bound of”, and the types denoted by U are the same.

The qualification-combined type of two types T1 and T2is the type T3similar to T1 whose qualification-decomposition is such that:

where and are the components of the qualification-decomposition of Tj.

A prvalue of type T1can be converted to type T2if the qualification-combined type of T1 and T2 is T2.

[Note 1:

If a program could assign a pointer of type T** to a pointer of type const T** (that is, if line #1 below were allowed), a program could inadvertently modify a const object (as it is done on line #2).

For example,int main() { const char c = 'c';char* pc;const char** pcc = &pc; *pcc = &c;*pc = 'C'; }

— _end note_]

[Note 2:

Given similar types T1 and T2, this construction ensures that both can be converted to the qualification-combined type of T1 and T2.

— _end note_]

[Note 3:

A prvalue of type “pointer to cv1 T” can be converted to a prvalue of type “pointer to cv2 T” if “cv2 T” is more cv-qualified than “cv1 T”.

A prvalue of type “pointer to member of X of type cv1 T” can be converted to a prvalue of type “pointer to member of X of type cv2 T” if “cv2 T” is more cv-qualified than “cv1 T”.

— _end note_]

[Note 4:

Function types (including those used in pointer-to-member-function types) are never cv-qualified ([dcl.fct]).

— _end note_]

7.3.7 Integral promotions [conv.prom]

For the purposes of [conv.prom], a converted bit-field is a prvalue that is the result of an lvalue-to-rvalue conversion ([conv.lval]) applied to a bit-field ([class.bit]).

A prvalue that is not a converted bit-field and has an integer type other thanbool, char8_t, char16_t,char32_t, or wchar_t whose integer conversion rank ([conv.rank]) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.

A prvalue of an unscoped enumeration type whose underlying type is not fixed can be converted to a prvalue of the first of the following types that can represent all the values of the enumeration ([dcl.enum]): int,unsigned int, long int, unsigned long int,long long int, or unsigned long long int.

If none of the types in that list can represent all the values of the enumeration, a prvalue of an unscoped enumeration type can be converted to a prvalue of the extended integer type with lowest integer conversion rank ([conv.rank]) greater than the rank of long longin which all the values of the enumeration can be represented.

If there are two such extended types, the signed one is chosen.

A prvalue of an unscoped enumeration type whose underlying type is fixed ([dcl.enum]) can be converted to a prvalue of its underlying type.

Moreover, if integral promotion can be applied to its underlying type, a prvalue of an unscoped enumeration type whose underlying type is fixed can also be converted to a prvalue of the promoted underlying type.

[Note 1:

A converted bit-field of enumeration type is treated as any other value of that type for promotion purposes.

— _end note_]

A converted bit-field of integral type can be converted to a prvalue of type int if int can represent all the values of the bit-field; otherwise, it can be converted tounsigned int if unsigned int can represent all the values of the bit-field.

A prvalue of typechar8_t, char16_t, char32_t, orwchar_t ([basic.fundamental]) (including a converted bit-field that was not already promoted to int or unsigned intaccording to the rules above) can be converted to a prvalue of the first of the following types that can represent all the values of its underlying type:int,unsigned int,long int,unsigned long int,long long int,unsigned long long int, or its underlying type.

A prvalue of type bool can be converted to a prvalue of typeint, with false becoming zero and true becoming one.

These conversions are called integral promotions.

7.3.8 Floating-point promotion [conv.fpprom]

A prvalue of type float can be converted to a prvalue of typedouble.

The value is unchanged.

This conversion is called floating-point promotion.

7.3.9 Integral conversions [conv.integral]

A prvalue of an integer type can be converted to a prvalue of another integer type.

A prvalue of an unscoped enumeration type can be converted to a prvalue of an integer type.

If the destination type is bool, see [conv.bool].

If the source type is bool, the value false is converted to zero and the value true is converted to one.

Otherwise, the result is the unique value of the destination type that is congruent to the source integer modulo , where N is the width of the destination type.

The conversions allowed as integral promotions are excluded from the set of integral conversions.

7.3.10 Floating-point conversions [conv.double]

A prvalue of floating-point type can be converted to a prvalue of another floating-point type with a greater or equal conversion rank ([conv.rank]).

A prvalue of standard floating-point type can be converted to a prvalue of another standard floating-point type.

If the source value can be exactly represented in the destination type, the result of the conversion is that exact representation.

If the source value is between two adjacent destination values, the result of the conversion is animplementation-defined choice of either of those values.

Otherwise, the behavior is undefined.

The conversions allowed as floating-point promotions are excluded from the set of floating-point conversions.

7.3.11 Floating-integral conversions [conv.fpint]

A prvalue of a floating-point type can be converted to a prvalue of an integer type.

The conversion truncates; that is, the fractional part is discarded.

The behavior is undefined if the truncated value cannot be represented in the destination type.

[Note 1:

If the destination type is bool, see [conv.bool].

— _end note_]

A prvalue of an integer type or of an unscoped enumeration type can be converted to a prvalue of a floating-point type.

The result is exact if possible.

If the value being converted is in the range of values that can be represented but the value cannot be represented exactly, it is an implementation-defined choice of either the next lower or higher representable value.

[Note 2:

Loss of precision occurs if the integral value cannot be represented exactly as a value of the floating-point type.

— _end note_]

If the value being converted is outside the range of values that can be represented, the behavior is undefined.

If the source type is bool, the value false is converted to zero and the valuetrue is converted to one.

7.3.12 Pointer conversions [conv.ptr]

A null pointer constant is an integer literal ([lex.icon]) with value zero or a prvalue of type std​::​nullptr_t.

A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type ([basic.compound]) and is distinguishable from every other value of object pointer or function pointer type.

Such a conversion is called a null pointer conversion.

The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion ([conv.qual]).

A null pointer constant of integral type can be converted to a prvalue of type std​::​nullptr_t.

[Note 1:

The resulting prvalue is not a null pointer value.

— _end note_]

A prvalue of type “pointer to cv T”, where Tis an object type, can be converted to a prvalue of type “pointer tocv void”.

The pointer value ([basic.compound]) is unchanged by this conversion.

A prvalue v of type “pointer to cv D”, where Dis a complete class type, can be converted to a prvalue of type “pointer tocv B”, where B is a base class ([class.derived]) of D.

If B is an inaccessible ([class.access]) or ambiguous ([class.member.lookup]) base class of D, a program that necessitates this conversion is ill-formed.

If v is a null pointer value, the result is a null pointer value.

Otherwise, if B is a virtual base class of D andv does not point to an object whose type is similar ([conv.qual]) to D and that is within its lifetime or within its period of construction or destruction ([class.cdtor]), the behavior is undefined.

Otherwise, the result is a pointer to the base class subobject of the derived class object.

7.3.13 Pointer-to-member conversions [conv.mem]

A null pointer constant can be converted to a pointer-to-member type; the result is the null member pointer valueof that type and is distinguishable from any pointer to member not created from a null pointer constant.

Such a conversion is called a null member pointer conversion.

The conversion of a null pointer constant to a pointer to member of cv-qualified type is a single conversion, and not the sequence of a pointer-to-member conversion followed by a qualification conversion ([conv.qual]).

A prvalue of type “pointer to member of B of type cv T”, where B is a class type, can be converted to a prvalue of type “pointer to member of D of type cv T”, where D is a complete class derived ([class.derived]) from B.

If B is an inaccessible ([class.access]), ambiguous ([class.member.lookup]), or virtual ([class.mi]) base class of D, or a base class of a virtual base class ofD, a program that necessitates this conversion is ill-formed.

If class D does not contain the original member and is not a base class of the class containing the original member, the behavior is undefined.

Otherwise, the result of the conversion refers to the same member as the pointer to member before the conversion took place, but it refers to the base class member as if it were a member of the derived class.

The result refers to the member in D's instance of B.

Since the result has type “pointer to member of D of type cv T”, indirection through it with a D object is valid.

The result is the same as if indirecting through the pointer to member of B with theB subobject of D.

The null member pointer value is converted to the null member pointer value of the destination type.47

7.3.14 Function pointer conversions [conv.fctptr]

A prvalue of type “pointer to noexcept function” can be converted to a prvalue of type “pointer to function”.

The result is a pointer to the function.

A prvalue of type “pointer to member of type noexcept function” can be converted to a prvalue of type “pointer to member of type function”.

The result designates the member function.

[Example 1: void (*p)();void (**pp)() noexcept = &p; struct S { typedef void (*p)(); operator p(); };void (*q)() noexcept = S(); — _end example_]

7.3.15 Boolean conversions [conv.bool]

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer-to-member type can be converted to a prvalue of type bool.

A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.