Type erasure as a design pattern
We have already seen how type erasure manifests itself in a program: the code expects certain semantics behavior, but, instead of dealing with specific types that provide it, we use an abstraction and “erase” the properties of those types that are not relevant to the task at hand (starting with the name of the type).
In this way, type erasure has attributes of several other design patterns, but it’s not equivalent to any of them. It could reasonably be considered a design pattern in its own right. So, what does type erasure offer as a design pattern?
In type erasure, we find an abstract expression of certain behavior, such as a function call, that can be used to separate the interface from the implementation. So far, this sounds very similar to inheritance. Recall now how, at the end of the last section, we made a std::function
object invoke several completely different callables: a function, a lambda expression, and a member...