The Object Upside Down
summary
Creates an instance of a non-default-constructible type by placing two layout-compatible wrapper structs into a union, activating one, reading the other through their common initial sequence, reinterpret_casting that member to the wrapper type and then to the target base to yield a T without calling any constructors. Relies on standard-layout and an inactive union read* hack that works under current rules but is unsafe; C++20 makes stateless lambdas default-constructible so the workaround becomes unnecessary.
AI-written summary · about
related talks
55mBreaking Dependencies: Type Erasure - The Implementation Details
27mHow to Hold a T
57mFun with Type Erasure: Implementing a Value Wrapper for Polymorphic Types
01h 05mType Punning in C++17: Avoiding Pun-defined Behavior
01h 22mThese 10 tricks that only library implementors know!
01h 02mBinary Object Serialization With Structure Traversal & Reconstruction
50mType safe C++? LOL!
12mExtreme Type Safety with Opaque Typedefs
