dcthxd
dcthxd

Reputation: 51

How to understand expr.const.cast clause 8 in C++ standard N3337?

In C++ draft standard N3337 section [expr.const.cast]/8:

The following rules define the process known as casting away constness. In these rules Tn and Xn represent types. For two pointer types:

X1 is T1 cv1,1 * ⋯ cv1,N * where T1 is not a pointer type

X2 is T2 cv2,1 * ⋯ cv2,M * where T2 is not a pointer type

K is min (N,M)

casting from X1 to X2 casts away constness if, for a non-pointer type T there does not exist an implicit conversion (Clause conv) from:

T cv1,(N-K+1) * cv1,(N-K+2) *⋯ cv1,N *

to

T cv2,(M-K+1) * cv2,(M-K+2) *⋯ cv2,M *

I can't understand the clause, so I looked up the corresponding clause in C++ draft standard N4659, [expr.const.cast]/7:

A conversion from a type T1 to a type T2 casts away constness if T1 and T2 are different, there is a cv-decomposition of T1 yielding n such that T2 has a cv-decomposition of the form

cv02P02 cv12P12cvn-12Pn-12 cvn2 U2

and there is no qualification conversion that converts T1 to

cv02P01 cv12P11cvn-12Pn-11 cvn2 U1.

But I still can't understand the relationship between T1,T2 and T. Help me, who can interpret the clause?

Upvotes: 1

Views: 147

Answers (1)

Davis Herring
Davis Herring

Reputation: 39838

In the older version, a (mostly) arbitrary T is introduced just to dispense with the issue that

const int *p=0;
auto q=reinterpret_cast<char*>(p);  // error: casts away constness

is changing from int to char. It gets effectively rewritten as

auto __q=reinterpret_cast<const char*>(p);  // ok
char *q=__q;  // error

The newer version simply explicitly reuses U1 rather than inventing T.

Upvotes: 1

Related Questions