How to "OO" in C++
summary
Compares four concrete approaches to polymorphism in C++—inheritance with virtuals, templates/concepts, std::variant with std::visit, and the visitor/overloaded patterns—shows a microbenchmark where removing virtual saves over 10x faster in a specific loop by enabling optimization, demonstrates mutex/lock_guard implemented with templates (no virtuals) to avoid heap and runtime dispatch, and weighs tradeoffs: runtime dispatch flexibility vs compiler optimizability, fixed object layout and verifiability costs of virtuals, and the need to recompile when adding types with std::variant + std::visit while recommending concept-based dispatch for many cases.
AI-written summary · about
chapters (20)
- 0:00Introduction to OO in C++
- 1:25What is object orientation
- 5:43Virtual vs non-virtual methods
- 8:27Performance cost of virtual methods
- 13:48Compiler optimization limits
- 15:57Interoperability and verifiability issues
- 16:47SOLID principles and inheritance
- 17:57Client-side extensibility problem
- 20:04Alternatives to virtual methods
- 21:11Mutex design without virtuals
- 25:23Concept-based approach
- 33:48Choosing between concepts and inheritance
- 34:12Visitor pattern for extensibility
- 38:34Duck typing and variants
- 42:11Using std::variant for polymorphism
- 46:50Overloaded pattern for runtime dispatch
- 49:19Best practices for polymorphism
- 50:56Modern C++ alternatives to static polymorphism
- 51:41Type erasure as a tool
- 52:05Q&A on C++ standards and optimizations
related talks
57mA Tour of Polymorphism Techniques in C++
01h 23mRethink Polymorphism in C++
40mBack to Basics: Virtual Dispatch and its Alternatives
58mOO Considered Harmful
47mDesign Patterns - The Most Common Misconceptions (2 of N)
01h 36mObject-Oriented Program: Best Practices
49mPolymorphism != Virtual: Easy, Flexible Runtime Polymorphism Without Inheritance
22mEffective replacement of dynamic polymorphism with std::variant
