Object Lifetime in C++: From Start to Finish
summary
Covers concrete rules: object lifetime starts when storage is obtained and non‑vacuous initialization completes, ends when destructor runs or storage is released; temporaries die at the end of the full expression except when bound to a reference (lifetime extension). Demonstrates RAII patterns, shows return value optimization and named RVO (clang often elides, GCC sometimes emits moves), warns about dangling references from returning member refs, reproduces behavior on Godbolt and uses C++ Insights to show range‑for desugaring; range‑for UB is fixed in C++23.
AI-written summary · about
chapters (11)
- 0:00Introduction to object lifetime
- 2:53Storage duration basics
- 8:09Constructors destructors examples
- 15:16RAII and resource management
- 18:51Tricky parts of object lifetime
- 21:00Return value optimization
- 27:44Dangling references explained
- 34:06Lifetime of temporaries
- 41:42Lifetime extension rules
- 48:13Range-based for loop pitfalls
- 53:31Summary and closing
related talks
01h 11mC++ Object Lifetimes - Objects and Lifetimes in C++ Explained
12mWorkshop Interview - Understanding Object Lifetime For Efficient and Safer C++
39mTemporal Dynamics - Exploring the Intricacies of Object Lifetimes in Modern C++
39mTemporal Dynamics - Exploring the Intricacies of Object Lifetimes in Modern C++
01h 04m“Sane and Safe C++ Classes”
01h 02mBack to Basics: RAII and the Rule of Zero
01h 20mWhat Classes We Design and How
36mInterfaces Matter
