4 Standard conversions [conv]

4.4 Qualification conversions [conv.qual]

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

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

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

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:

  • the pointer types are similar.

  • for every j > 0, if const is in cv1,j then const is in cv2,j, and similarly for volatile.

  • if the cv1,j and cv2,j are different, then const is in every cv2,k for 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 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;       // #1: not allowed
  *pcc = &c;
  *pc = 'C';                    // #2: modifies a const object
}

 — end note ]

A multi-level pointer to member type, or a multi-level mixed pointer and pointer to member type has the form:

cv0 P0 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.

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

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.

These rules ensure that const-safety is preserved by the conversion.