Back to Basics: Type Erasure
summary
Explains how to implement type erasure step-by-step: start with representation and behavior split using void plus function pointers, build a reference form (no ownership), then a value form using heap allocation and lambdas for clone/delete* affordances, and finally show vtable/virtual-base and single-function-with-switch optimizations. Compares std::function, std::any, function_ref, unique_function and inplace_function, shows tradeoffs (heap cost, ABI, speed) and provides working code (Godbolt link) that demonstrates each approach.
AI-written summary · about
chapters (9)
- 0:00Introduction
- 1:00What is type erasure?
- 2:06Motivating type erasure in C++
- 7:00Representation and behavior
- 13:26Building type-erased types
- 23:10Affordances and special members
- 30:41Standard library examples
- 40:20Other applications
- 43:20Conclusion and Q&A
related talks
01h 41mC++ Type Erasure Demystified
37mC++ Zero Overhead Pass by Value Through Invocable C++ Abstractions
01h 29mHigher-order functions and function_ref
59mBack to Basics: Move Semantics
01h 07mDynamic Polymorphism With Code Injection and Metaclasses
01h 00mDynamic Polymorphism with Metaclasses and Code Injection
01h 14mReintroduction to Generic Programming for C++ Engineers
50mExpression Templates: Past, Present, Future
