Skip to content

static_cast involving pointers to members is too strict during constant evaluation #154033

@Halalaluyafail3

Description

@Halalaluyafail3

The following program demonstrates this issue:

consteval int foo(){
    struct A{char x;};
    struct B{char y;};
    struct C:A,B{};
    char C::*p=&A::x;
    char B::*q=static_cast<char B::*>(p);
    C c{};
    return((B&)c).*q;
}
int main(){
    return foo();
}

Clang rejects this code complaining about the static_cast, while GCC accepts it. Also, MSVC will crash with this code. This code should be fine because https://eel.is/c++draft/expr.static.cast#11.sentence-5 says "If class B contains the original member, or is a base class of the class containing the original member, the resulting pointer to member points to the original member." B here does not contain the member A::x, but it is a base class of C which contains the member.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constevalC++20 consteval

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions