Surprises in Object Lifetime
summary
Concrete surprises: implicit wrappers like reference_wrapper and string_view produce dangling references with no compiler warnings, returning or binding to temporaries sometimes extends lifetime but not recursively, and initializer_list creates hidden const arrays that can cause five allocations when building a vector. Structured bindings block moves, and C++17 if-init bindings leak into else blocks. Examples run on Compiler Explorer with clang/gcc; static tools miss many cases while sanitizers and constexpr checks reliably catch the undefined behaviors.
AI-written summary · about
chapters (19)
- 0:00Introduction and welcome
- 2:52What is object lifetime
- 8:13Basic object lifetime examples
- 13:14Reference wrappers surprises
- 17:14Strings lifetime surprises
- 19:26Moved-from objects destruction
- 22:59Temporary lifetime extension
- 27:08Initializer list allocations
- 32:54Standard array efficiency
- 38:38Range-based for loops
- 41:48C++20 for-init statements
- 43:34If-init statement scope
- 44:39RVO and NRVO
- 47:32Structured bindings moves
- 51:11Constructor exceptions
- 53:59Delegating constructors
- 55:05Avoiding lifetime surprises
- 58:56Constexpr and sanitizers
- 1:00:01Q&A
related talks
15mC++ WAT
56mCute C++ Tricks (and what you can learn from them): Part 2 of N
37mThe C++ rvalue Lifetime Disaster
47mThe C++ rvalue Lifetime Disaster
52mNon-conforming C++: the Secrets the Committee Is Hiding From You
58mAn (In-)Complete Guide to C++ Object Lifetimes
54mTypical C++, But Why?
05mReducing immediate dangling?
