4 - Standard conversions [conv]

-1- Standard conversions are implicit conversions defined for built-in types. Clause conv enumerates the full set of such conversions. A standard conversion sequence is a sequence of standard conversions in the following order:

[Note: a standard conversion sequence can be empty, i.e., it can consist of no conversions. ] A standard conversion sequence will be applied to an expression if necessary to convert it to a required destination type.

-2- [Note: expressions with a given type will be implicitly converted to other types in several contexts:


--- end note]

-3- An expression e can be implicitly 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). The effect of the implicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion. The result is an lvalue if T is a reference type (dcl.ref), and an rvalue otherwise. The expression e is used as an lvalue if and only if the initialization uses it as an lvalue.

-4- [Note: For user-defined 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.

-5- 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. ]

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

-1- An lvalue (basic.lval) of a non-function, non-array type T can be converted to an rvalue. If T is an incomplete type, a program that necessitates this conversion is ill-formed. If the object to which the lvalue refers is not an object of type T and is not an object of a type derived from T, or if the object is uninitialized, a program that necessitates this conversion has undefined behavior. If T is a non-class type, the type of the rvalue is the cv-unqualified version of T. Otherwise, the type of the rvalue is T. *

[Footnote: In C++ class rvalues can have cv-qualified types (because they are objects). This differs from ISO C, in which non-lvalues never have cv-qualified types. --- end foonote]

-2- The value contained in the object indicated by the lvalue is the rvalue result. When an lvalue-to-rvalue conversion occurs within the operand of sizeof (expr.sizeof) the value contained in the referenced object is not accessed, since that operator does not evaluate its operand.

-3- [Note: See also basic.lval. ]

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

-1- An lvalue or rvalue of type ``array of N T'' or ``array of unknown bound of T'' can be converted to an rvalue of type ``pointer to T.'' The result is a pointer to the first element of the array.

-2- A string literal (lex.string) that is not a wide string literal can be converted to an rvalue of type ``pointer to char''; a wide string literal can be converted to an rvalue of type ``pointer to wchar_t''. In either case, the result is a pointer to the first element of the array. This conversion is considered only when there is an explicit appropriate pointer target type, and not when there is a general need to convert from an lvalue to an rvalue. [Note: this conversion is deprecated. See Annex depr. ] For the purpose of ranking in overload resolution (over.ics.scs), this conversion is considered an array-to-pointer conversion followed by a qualification conversion (conv.qual). [Example: "abc" is converted to ``pointer to const char'' as an array-to-pointer conversion, and then to ``pointer to char'' as a qualification conversion. ]

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

-1- An lvalue of function type T can be converted to an rvalue of type ``pointer to T.'' The result is a pointer to the function.*

[Footnote: This conversion never applies to nonstatic member functions because an lvalue that refers to a nonstatic member function cannot be obtained. --- end foonote]

-2- [Note: See over.over for additional rules for the case where the function is overloaded. ]

4.4 - Qualification conversions [conv.qual]

-1- An rvalue of type ``pointer to cv1 T'' can be converted to an rvalue of type ``pointer to cv2 T'' if ``cv2 T'' is more cv-qualified than ``cv1 T.''

-2- An rvalue of type ``pointer to member of X of type cv1 T'' can be converted to an rvalue of type ``pointer to member of X of type cv2 T'' if ``cv2 T'' is more cv-qualified than ``cv1 T.''

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

-4- A conversion can add cv-qualifiers at levels other than the first in multi-level pointers, subject to the following rules:*

[Footnote: These rules ensure that const-safety is preserved by the conversion. --- end foonote]
Two pointer types T1 and T2 are similar if there exists a type T and integer n > 0 such that:
T1~roman is~cv sub 1,0~roman "pointer to"~cv sub 1,1~roman "pointer to"~...~cv sub 1,n-1~roman "pointer to"~cv sub 1,n~T
and
T2~roman is~cv sub 2,0~roman "pointer to"~cv sub 2,1~roman "pointer to"~...~cv sub 2,n-1~roman "pointer to"~cv sub 2,n~T
where each cvi,j is const, volatile, const volatile, or nothing. The n-tuple of cv-qualifiers after the first in a pointer type, e.g., cv1,1, cv1,2, ~...~, cv1,n in the pointer type T1, is called the cv-qualification signature of the pointer type. An expression of type T1 can be converted to type T2 if and only if the following conditions are satisfied:
[Note: if a program could assign a pointer of type T** to a pointer of type const T** (that is, if line //1 below was 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;         //1: not allowed
	*pcc = &c;
	*pc = 'C';              //2: modifies a  const  object
}

--- end note]

-5-

A multi-level pointer to member type, or a multi-level mixed pointer and pointer to member type has the form:
cv sub 0 P sub 0~roman "to"~cv sub 1 P sub 1~roman "to"~...~cv sub n-1 P sub n-1~roman "to"~cv sub n~T
where Pi is either a pointer or pointer to member and where T is not a pointer type or pointer to member type.

-6- Two multi-level pointer to member types or two multi-level mixed pointer and pointer to member types T1 and T2 are similar if there exists a type T and integer n > 0 such that:

T1~roman is~cv sub 1,0 P sub 0~roman "to"~cv sub 1,1 P sub 1~roman "to"~...~cv sub 1,n-1 P sub n-1~roman "to"~cv sub 1,n~T
and
T2~roman is~cv sub 2,0 P sub 0~roman "to"~cv sub 2,1 P sub 1~roman "to"~...~cv sub 2,n-1 P sub n-1~roman "to"~cv sub 2,n~T

-7- For similar multi-level pointer to member types and similar multi-level mixed pointer and pointer to member types, the rules for adding cv-qualifiers are the same as those used for similar pointer types.

4.5 - Integral promotions [conv.prom]

-1- An rvalue of type char, signed char, unsigned char, short int, or unsigned short int can be converted to an rvalue of type int if int can represent all the values of the source type; otherwise, the source rvalue can be converted to an rvalue of type unsigned int.

-2- An rvalue of type wchar_t (basic.fundamental) or an enumeration type (dcl.enum) can be converted to an rvalue of the first of the following types that can represent all the values of its underlying type: int, unsigned int, long, or unsigned long.

-3- An rvalue for an integral bit-field (class.bit) can be converted to an rvalue of type int if int can represent all the values of the bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field. If the bit-field is larger yet, no integral promotion applies to it. If the bit-field has an enumerated type, it is treated as any other value of that type for promotion purposes.

-4- An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one.

-5- These conversions are called integral promotions.

4.6 - Floating point promotion [conv.fpprom]

-1- An rvalue of type float can be converted to an rvalue of type double. The value is unchanged.

-2- This conversion is called floating point promotion.

4.7 - Integral conversions [conv.integral]

-1- An rvalue of an integer type can be converted to an rvalue of another integer type. An rvalue of an enumeration type can be converted to an rvalue of an integer type.

-2- If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type). [Note: In a two's complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). ]

-3- If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.

-4- 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.

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

4.8 - Floating point conversions [conv.double]

-1- An rvalue of floating point type can be converted to an rvalue of another 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 an implementation-defined choice of either of those values. Otherwise, the behavior is undefined.

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

4.9 - Floating-integral conversions [conv.fpint]

-1- An rvalue of a floating point type can be converted to an rvalue 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: If the destination type is bool, see conv.bool. ]

-2- An rvalue of an integer type or of an enumeration type can be converted to an rvalue of a floating point type. The result is exact if possible. Otherwise, it is an implementation-defined choice of either the next lower or higher representable value. [Note: loss of precision occurs if the integral value cannot be represented exactly as a value of the floating type. ] If the source type is bool, the value false is converted to zero and the value true is converted to one.

4.10 - Pointer conversions [conv.ptr]

-1- A null pointer constant is an integral constant expression (expr.const) rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Two null pointer values of the same type shall compare equal. 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).

-2- An rvalue of type ``pointer to cv T,'' where T is an object type, can be converted to an rvalue of type ``pointer to cv void.'' The result of converting a ``pointer to cv T'' to a ``pointer to cv void'' points to the start of the storage location where the object of type T resides, as if the object is a most derived object (intro.object) of type T (that is, not a base class subobject).

-3- An rvalue of type ``pointer to cv D,'' where D is a class type, can be converted to an rvalue of type ``pointer to cv B,'' where B is a base class (clause class.derived) of D. If B is an inaccessible (clause class.access) or ambiguous (class.member.lookup) base class of D, a program that necessitates this conversion is ill-formed. The result of the conversion is a pointer to the base class sub-object of the derived class object. The null pointer value is converted to the null pointer value of the destination type.

4.11 - Pointer to member conversions [conv.mem]

-1- A null pointer constant (conv.ptr) can be converted to a pointer to member type; the result is the null member pointer value of that type and is distinguishable from any pointer to member not created from a null pointer constant. Two null member pointer values of the same type shall compare equal. 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).

-2- An rvalue of type ``pointer to member of B of type cv T,'' where B is a class type, can be converted to an rvalue of type ``pointer to member of D of type cv T,'' where D is a derived class (clause class.derived) of B. If B is an inaccessible (clause class.access), ambiguous (class.member.lookup) or virtual (class.mi) base class of D, a program that necessitates this conversion is ill-formed. 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,'' it can be dereferenced with a D object. The result is the same as if the pointer to member of B were dereferenced with the B sub-object of D. The null member pointer value is converted to the null member pointer value of the destination type.*

[Footnote: The rule for conversion of pointers to members (from pointer to member of base to pointer to member of derived) appears inverted compared to the rule for pointers to objects (from pointer to derived to pointer to base) (conv.ptr, clause class.derived). This inversion is necessary to ensure type safety. Note that a pointer to member is not a pointer to object or a pointer to function and the rules for conversions of such pointers do not apply to pointers to members. In particular, a pointer to member cannot be converted to a void*. --- end foonote]

4.12 - Boolean conversions [conv.bool]

-1- An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue 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.