https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95514
Bug ID: 95514 Summary: constexpr dynamic memory allocation compile error with inheritance and the this pointer Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: pkeir at outlook dot com Target Milestone: --- Created attachment 48671 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48671&action=edit The code described in the bug report. I believe the C++20 code below should compile, but using `g++ -std=c++2a` instead produces an error; indicating that there was deallocation of storage that was not previously allocated. If the call to the `point_to_me` method is replaced with the assignment on the previous line, there is no error. Functionally the two forms seem the same to me. Removing inheritance from the situation removes the error, which makes me suspect the `static_cast`, but the this pointer usage also catches my eye. Using an `std::allocator` instead of the `new` expression changes nothing (included in the attached file as test2). struct FooBase { constexpr void point_to_me(FooBase*& p) { p = this; } FooBase* m_p = nullptr; }; struct Foo : FooBase {}; constexpr bool test1() { FooBase fb; Foo* pf = new Foo; // fb.m_p = pf; // this works pf->point_to_me(fb.m_p); // this doesn't delete static_cast<Foo*>(fb.m_p); return true; }