Everyday Efficiency: In-Place Construction (Back to Basics?)
summary
Everyday rules for avoiding needless copies and allocations: string moves often cost as much as copies (bench shows ~20% slower moves for small strings), copy elision/RVO is the primary optimization to rely on, and push_back is fine for rvalues while emplace_back truly avoids temporaries. Associative containers need piecewise_construct for multi-arg mapped types, initializer_list can cause initializer_list undefined behavior and prevents moves, and optional/variant provide in-place constructors that prevent extra construction and moves.
AI-written summary · about
chapters (30)
- 0:00Introduction and talk overview
- 3:02What is in-place construction?
- 5:16Return Value Optimization (RVO) explained
- 8:08Rules and pitfalls of RVO
- 10:19RVO examples and compiler differences
- 14:31Named RVO and compiler behavior
- 15:52Common anti-patterns with RVO
- 17:39Why avoid default construction?
- 18:00Vector: push_back vs emplace_back
- 21:34Vector in-place construction with parameter packs
- 23:29Vector in-place construction with explicit constructors
- 25:17Vector in-place construction pitfalls
- 26:43Pairs and multi-argument constructors
- 28:50Recommendations for vectors
- 29:11Initializer lists and their issues
- 34:14Recommendations for initializer lists
- 34:35Maps: complexity and interfaces
- 36:04Vittorio's foreach-n-args for maps
- 37:50Inserting into existing maps
- 41:47Map in-place construction pitfalls
- 42:48Multi-argument constructors in maps
- 44:38Emplacing function results in maps
- 46:48Map insert_or_assign in C++17
- 47:53Map interface complexity summary
- 49:00C++20 aggregate initialization in maps
- 50:07Try_emplace for maps
- 51:37Optional, variant, and any in-place construction
- 53:27Recommendations for optional and variant
- 55:41Variant narrowing conversions in C++20
- 57:05Summary and best practices
related talks
01h 00mBack to Basics: Rvalues and Move Semantics in C++
53mBack to Basics: Lifetime Management in Cpp
01h 03mModern C++ Development: Limitations and Problems in std::function and Similar Constructs
01h 11mPragmatic Simplicity - Actionable Guidelines To Tame Cpp Complexity
01h 07mMoved-from Objects in C++
51mSimplicity: Not Just for Beginners
01h 02mHow to Hide C++ Implementation Details
56mC++ Code Smells
