What is type erasure in C++?
Here’s a very simple example of type erasure in action: // Type erasure side of things class TypeErasedHolder { struct TypeKeeperBase { virtual ~TypeKeeperBase() {} }; template <class ErasedType> struct TypeKeeper : TypeKeeperBase { ErasedType storedObject; TypeKeeper(ErasedType&& object) : storedObject(std::move(object)) {} }; std::unique_ptr<TypeKeeperBase> held; public: template <class ErasedType> TypeErasedHolder(ErasedType objectToStore) : held(new TypeKeeper<ErasedType>(std::move(objectToStore))) {} }; … Read more