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

            Bug ID: 110714
           Summary: constexpr lifetime error: base class this pointer
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pkeir at outlook dot com
  Target Milestone: ---

Compiling the C++20 MRE code below fails:

struct Base
{
  constexpr virtual ~Base() {}
  constexpr Base* get_this() { return this; }
  int x;
};

struct Derived : public Base {};

constexpr bool test()
{
  Derived* pf = new Derived;
  delete pf->get_this();
  return true;
}

constexpr bool b = test();

...with the following error message:

2$ /opt/gcc-latest/bin/g++ -std=c++20 -c ce_base_alloc2.cpp 
ce_base_alloc2.cpp:17:24:   in ‘constexpr’ expansion of ‘test()’
ce_base_alloc2.cpp:13:23:   in ‘constexpr’ expansion of
‘pf->Derived::<anonymous>.Base::get_this()->Base::~Base()’
ce_base_alloc2.cpp:8:8: error: deallocation of storage that was not previously
allocated
    8 | struct Derived : public Base {};
      |        ^~~~~~~

I have tried with GCC trunk (14.0.0) and also version 12.2.0.

I suspect that the this pointer in the base class is not tracking the constexpr
dynamic allocation. Clang and MSVC both compile successfully. Clang requires
the virtual destructor.

Reply via email to