4 Standard conversions |
[conv] |
[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.
4.1 Lvaluetorvalue conversion |
[conv.lval] |
4.2 Arraytopointer conversion |
[conv.array] |
4.3 Functiontopointer conversion |
[conv.func] |
4.4 Qualification conversions |
[conv.qual] |
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,0 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 ntuple of cvqualifiers after the first in a pointer type, e. g., cv1,1 , cvl,2 , . . . , cvl,nin the pointer type T1, is called the cvqualification 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:
- the pointer types are similar.
- for every j > 0, if const is in cv1, jthen const is in cv2, j , and similarly for volatile.
- if the cv1, j and cv2, j are different, then const is in every cv2,kfor 0 < k < j.
[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]
cv0 P0 to cv1 P1 to . . . cvn -1 Pn -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.
T1 is cv1 , 0 P0 to cv1 , 1 P1 to . . . cv1 , n -1 Pn -1 to cv1 , n T
and
T2 is cv2 , 0 P0 to cv2 , 1 P1 to . . . cv2 , n -1 Pn -1 to cv2 , n T
4.5 Integral promotions |
[conv.prom] |
4.6 Floating point promotion |
[conv.fpprom] |
4.7 Integral conversions |
[conv.integral] |
4.8 Floating point conversions |
[conv.double] |
4.9 Floatingintegral conversions |
[conv.fpint] |
4.10 Pointer conversions |
[conv.ptr] |
4.11 Pointer to member conversions |
[conv.mem] |
4.12 Boolean conversions |
[conv.bool] |
49) In C++ class rvalues can have cvqualified types (because they are objects). This differs from ISO C, in which nonlvalues never have cvqualified types.
50) This conversion never applies to nonstatic member functions because an lvalue that refers to a nonstatic member function cannot be obtained.
51) These rules ensure that constsafety is preserved by the conversion.
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*.