C++ Type Erasure Demystified
summary
Explains type erasure by building three concrete implementations: inheritance with virtuals, static-function pointers, and a compiler-like vtable-as-a-static-variable approach, with code for a smart pointer and a function wrapper. Shows local-buffer optimization to avoid allocations, measures deletion and call latency (custom function matches direct call within noise; typical std::function is about twice as slow), and demonstrates how rearranging the call path can make std::function 50% faster. Uses simple benchmarks, points to the zoo library and GCC behavior for real-world tradeoffs.
AI-written summary · about
chapters (8)
- 0:00Understanding Type Erasure
- 10:23Designing with Type Erasure
- 14:41Type Erasure in C
- 20:40Inheritance-Based Type Erasure
- 42:01Static Type Erasure
- 55:02V-Table Based Type Erasure
- 1:14:38Performance Benchmarks
- 1:33:25Conclusion and Q&A
related talks
49mBack to Basics: Type Erasure
01h 07mDynamic Polymorphism With Code Injection and Metaclasses
01h 12mFun With Type Erasure
01h 00mDynamic Polymorphism with Metaclasses and Code Injection
57mIntroduction to C++ Template Metaprogramming
01h 00mType punning in modern C++
06mv.~uint32_t();
50mExpression Templates: Past, Present, Future
