1 Standard conversions are implicit conversions defined for built-in types. Clause 4 enumerates the full set of such conversions. A standard conversion sequence is a sequence of standard conversions in the following order:
2 [Note: expressions with a given type will be implicitly converted to other types in several contexts:
3 An expression e can be implicitly converted to a type T if and only if the declaration ``T t=e;'' is wellformed, for some invented temporary variable t (8.5). 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 (8.3.2), 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 12.3. In general, an implicit conversion sequence (13.3.3.1) consists of a standard conversion sequence followed by a userdefined 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. ]
1 An lvalue (3.10) 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. 49)
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 (5.3.3) the value contained in the referenced object is not accessed, since that operator does not evaluate its operand.
3 [Note: See also 3.10. ]
49) 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. [back to text]
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 (2.13.4) 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 D. ] For the purpose of ranking in overload resolution (13.3.3.1.1), this conversion is considered an array-to-pointer conversion followed by a qualification conversion (4.4). [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. ]
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.50)
2 [Note: See 13.4 for additional rules for the case where the function is overloaded. ]
50) This conversion never applies to nonstatic member functions because an lvalue that refers to a nonstatic member function cannot be obtained. [back to text]
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 (8.3.5). ]
4 A conversion can add cv-qualifiers at levels other than the first in multi-level pointers, subject to the following rules:51) Two pointer types T1 and T2 are similar if there exists a type T and integer n >0 such that: T1 is cv1 , 0 pointer to cv1 , 1 pointer to . . . cv1 ,n -1 pointer to cv1 ,n T and T2 is cv2 , 0 pointer to cv2 , 1 pointer to . . . cv2 ,n -1 pointer to cv2 ,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:
---end note]int main() { const char c = 'c'; char* pc; const char** pcc = &pc; //1: not allowed *pcc = &c; *pc = 'C'; //2: modifies a const object }
5 A multi-level pointer to member type, or a multi-level mixed pointer and pointer to member type has the form: cv0P0 to cv1P1 to . . . cvn -1Pn -1 to cvn 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 is cv1 , 0P0 to cv1 , 1P1 to . . . cv1 ,n -1Pn -1 to cv1 ,n T and T2 is cv2 , 0P0 to cv2 , 1P1 to . . . cv2 ,n -1Pn -1 to cv2 ,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.
51) These rules ensure that const-safety is preserved by the conversion. [back to text]
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 (3.9.1) or an enumeration type (7.2) 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 (9.6) 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.
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.
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 4.12. 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.
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.
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 4.12. ]
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.
1 A null pointer constant is an integral constant expression (5.19) 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 (4.4).
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 (1.8) 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 10) of D. If B is an inaccessible (clause 11) or ambiguous (10.2) 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.
1 A null pointer constant (4.10) 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 (4.4).
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 10) of B. If B is an inaccessible (clause 11), ambiguous (10.2) or virtual (10.1) 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.52)
52) 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) (4.10, clause 10). 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*. [back to text]
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.