Fun with Type Erasure: Implementing a Value Wrapper for Polymorphic Types
summary
Builds a minimal type‑erased value wrapper that gives polymorphic objects value semantics instead of pointer semantics, implementing a union for 24-byte SSO, a static function-table with destroy/copy/move/assign per concrete type, and an optional pointer-to-interface cache to speed value access. It chooses small vs big storage by size and noexcept moveability, implements placement‑new in‑place construction, copy/move/assign with weak/strong exception guarantees, and compares behavior and sizes against std::any implementations — it works and trades a few bytes for much cheaper runtime access.
AI-written summary · about
chapters (12)
- 0:00Start
- 1:22The idea
- 1:45Pointer semantics
- 4:43Value semantics?
- 10:21Basic solution: drawbacks
- 18:00Minimal implementation
- 26:45Let's save pointer to interface
- 29:12Comparison to std::any
- 34:05Let's move!
- 38:09Differences to std:: any
- 46:08Assignment support
- 53:00Value assignment
related talks
55mBreaking Dependencies: Type Erasure - The Implementation Details
01h 23mRethink Polymorphism in C++
01h 21mMany an `any`
58mRust Traits In C++
01h 13mType safe C++ – LOL! :-)
01h 00m“Enhanced Support for Value Semantics in C++17”
12mExtreme Type Safety with Opaque Typedefs
01h 07mDynamic Polymorphism With Code Injection and Metaclasses
