Skip to content

CWG2879 [expr.const.cast] p3 does not cover all cases of similar types #232

Description

@xmh0511

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:

const_cast<int>(0);

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<int>(a); // The result does not refer to the original entity

Activity

  1. frederick-vs-ja commented on Mar 20, 2023

    @frederick-vs-ja

    There's implementation divergence on

    • function pointer types,
    • function reference types,
    • pointer-to-member-function types,
    • enumeration types, and
    • nullptr_t.

    MSVC accepts identically converting cases, while GCC and Clang reject them.

    Presumably, the intent is restricting the types to object pointer types (including cv void*), object reference types, and pointer-to-data-member types.

  2. frederick-vs-ja commented on Apr 16, 2024

    @frederick-vs-ja

    Addressed by CWG2879.

  3. changed the title [-][expr.const.cast] p3 does not cover all cases of similar types[/-] [+]CWG2879 [expr.const.cast] p3 does not cover all cases of similar types[/+] on Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions