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

            Bug ID: 129049
           Summary: Converting dynamically created objects of derived
                    class type to reference of its nonempty base class
                    type during constant evaluation is not handled
                    correctly
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Pure-Happiness at outlook dot com
  Target Milestone: ---

The code below should be accepted, but the last static assertion fails when
compiled by GCC, indicating that converting dynamically created objects of
derived class type to reference of its nonempty base class type during constant
evaluation is not handled correctly. This bug can be reproduced in GCC 16.2.0
(https://godbolt.org/z/EqbKezn4P) and the current trunk, while Clang accepts
the code.

struct A {};
struct B {
  int x;
};
struct C : A {};
struct D : B {};

consteval void f0() {
  A *p = new C;
  delete static_cast<C *>(p);
}
consteval void f1() {
  A &p = *new C;
  delete static_cast<C *>(&p);
}
consteval void f2() {
  B *p = new D;
  delete static_cast<D *>(p);
}
consteval void f3() {
  B &p = *new D;
  delete static_cast<D *>(&p);
}

static_assert((f0(), true));
static_assert((f1(), true));
static_assert((f2(), true));
static_assert((f3(), true));

Reply via email to