Rethink Polymorphism in C++
summary
Argues that runtime inheritance plus heap pointers is error-prone and slow, and demonstrates alternatives using std::variant, variadic templates and move semantics to get value semantics and heterogeneous containers without new/delete. Benchmarks show copying shared_ptr pays heavy atomic costs (microbenchmarks report up to ~10,000× slowdown in his reproduction), unique_ptr avoids that but disallows copying, and a variant-based picture often gives much faster destruction and downcast times (roughly 5–10× improvement in his tests).
AI-written summary · about
chapters (12)
- 0:00C++ polymorphism intro
- 1:54Runtime polymorphism with virtual
- 6:32Raw pointers in collections problems
- 10:00RAII and smart pointers for safety
- 23:28Shared pointers and threading overhead
- 37:35Atomic reference counting impact
- 43:40Virtual table overhead in runtime poly
- 46:51Compile-time polymorphism with templates
- 51:30Variant-based polymorphism without inheritance
- 1:01:50Performance comparison of poly approaches
- 1:12:33Teaching polymorphism basics
- 1:18:16Q&A on variant visitors
related talks
58mBetter Code: Runtime Polymorphism
49mPolymorphism != Virtual: Easy, Flexible Runtime Polymorphism Without Inheritance
55mHow to "OO" in C++
01h 35mRuntime Polymorphism: Back to the Basics
57mA Tour of Polymorphism Techniques in C++
01h 07mDynamic Polymorphism With Code Injection and Metaclasses
01h 11mI just wanted to point to something!
55mBreaking Dependencies: Type Erasure - The Implementation Details
