Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardC++object lifetime and std::launderSingle-choice MCQ

After placement-new constructs a new object of the same type into storage previously holding another object that contained a `const` or reference member, why is `std::launder` required to access the new object through the old pointer?

struct S { const int x; }; alignas(S) unsigned char buf[sizeof(S)]; S* p = new (buf) S{1}; p->~S(); new (buf) S{2}; // reading p->x without launder?