https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107744

            Bug ID: 107744
           Summary: Error in constant evaluation of dynamic_cast
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fchelnokov at gmail dot com
  Target Milestone: ---

The following program

struct A {
    constexpr A();
    virtual void f() {}
    bool x;
};

struct B : A {};

constexpr A::A() { 
    x = dynamic_cast<B*>(this);
}
constexpr bool b = B{}.x;

is accepted by Clang and MSVC, but GCC prints vague error:

<source>:12:24:   in 'constexpr' expansion of 'B()'
<source>:7:8:   in 'constexpr' expansion of '((B*)this)->B::<anonymous>.A::A()'
<source>:12:24: error: '((&<anonymous>.B::<anonymous>) != 0)' is not a constant
expression
   12 | constexpr bool b = B{}.x;

Online demo: https://godbolt.org/z/1xdq68aaM

Reply via email to