An (In-)Complete Guide to C++ Object Lifetimes

A C++ program manipulate objects, but it is undefined behavior if you attempt to manipulate them while they are not alive. So let’s do a deep dive into object lifetime.

When are objects created, and when are they destroyed? How does temporary lifetime extension come into play, and what changed there recently? What happens when you std::malloc memory and just pretend objects are there without creating anything? Or worse: You use mmap() to read shared memory. How do unions interact with constructors, strict aliasing, or the “common initial sequence”? What happens when you explicitly call the destructor and later re-use the same storage? What’s the deal with std::launder, std::bit_cast, and std::start_lifetime_as?

We’ll answer all of those questions and much more. We’ll do that by looking at the C++ standard, old and new proposals, and compiler optimizations.

Errata (ACCU)

  • The use of std::destroy_at + placement new in operator= is problematic when the object is a [[no_unique_address]] member. In that case, the trailing padding bytes the compiler might re-use for a different member are potentially overwritten. You are also not covered by transparent replacement in that case.

Errata (C++Now)

  • I’ve skipped slide 86 in the talk because I thought it was incorrect at the time; it isn’t actually incorrect.

See also