CWG2879 [expr.const.cast] p3 does not cover all cases of similar types (original) (raw)

Full name of submitter (unless configured in github; will be published with the issue): Jim X

[expr.const.cast] p3 says:

For two similar types T1 and T2, a prvalue of type T1 may be explicitly converted to the type T2 using a const_­cast if, considering the qualification-decompositions of both types, each P1i is the same as P2i for all i. The result of a const_­cast refers to the original entity.

The first case is:

All mainstream implementations emit the error:

invalid use of 'const_cast' with type 'int', which is not a pointer, reference, nor a pointer-to-data-member type

However, we do not have this limitation in subclause [expr.const.cast] p3. Moreover, each P1i is the same as P2i for all i can just be vacuous true if Pi is empty.

[expr.const.cast] p2 also implies the case

Subject to the restrictions in this subclause, an expression can be cast to its own type using a const_­cast operator.

The relevant issue is https://stackoverflow.com/questions/61003855/const-cast-to-non-pointer-non-reference-type


The second case is

The result of a const_­cast refers to the original entity

This only applies to the case where the destination type is pointer or reference type.

int const a = 0; const_cast(a); // The result does not refer to the original entity