Skip to content

constexpr dynamic_cast failure #68199

@gbMattN

Description

@gbMattN

As of c++20, dynamic_cast expressions can appear in constexpr expressions, but the following code

struct Base {
    virtual int foo() {
        return 1;
    }
};
struct Derived : public Base {};

Derived d;

int test_it (void) {
    constexpr auto const x= (dynamic_cast<void *>(&d)!=0);
    return x;
}

int main(void) {
    return 0;
}

Gives the error

<source>:11:26: error: constexpr variable 'x' must be initialized by a constant expression
   11 |     constexpr auto const x= (dynamic_cast<void *>(&d)!=0);
      |                          ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:11:30: note: dynamic_cast applied to object 'd' whose dynamic type is not constant
   11 |     constexpr auto const x= (dynamic_cast<void *>(&d)!=0);
      |                              ^
1 error generated.

This was built on Ubuntu 22.04 using the main branch of llvm at time of reporting (75f295c) with the -stdc++20 flag.

It builds successfully on MSVC since v19.36 and GCC since 10.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++20clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluationcwg-issueAn issue that was filed to the Core Working Grouprejects-valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions