Type erasure is a technique that allows the decoupling of the bit representation of implementation from the user. This permits the user to be resilient against changes in the implementation and/or to operate on different implementations. The simplest C++ approach to type erasure is through inheritance. Compiler Explorer link: https://lnkd.in/diVs7QE3 #cpp #cplusplus #coding #programming #dailybiteofcpp
One piece of anecdotal evidence regarding type erasure, std::make_unique and auto. Somehow I remember that if you auto ptr = std::make_unique<DerivedType>; and then you try to push ptr someplace where std::unique_ptr<Interface> is expected, MSVC will vomit at you. As this is an anecdotal evidence, it is possible that I forgot some additional precondition which makes my case different from this one.
I might be wrong, but from what I’ve learned so far, the main strenght of type erasure comes from external polymorphism. Complete decoupling. ImplA/B should be compatible with infinite number of Interfaces. They simply need to adhere to the interface requirements. Btw I love your capsules!
I'd say arrays decaying to pointers is the simplest and oldest. But it just erases the size. Half a century and Eric Neibler's brain was needed to bring safety to that kind of type-erasure. Going back to OP, having `std::any_cast<void>` to always return valid `void*` would facilitate implementation of type_erasure much more. I wouldn't need `boost.type_erasure.any`, if that cast on `std::any` were present.