Let's look briefly at how std::any might be implemented by the standard library. The core idea is called "type erasure," and the way we achieve it is to identify the salient or relevant operations that we want to support for all types T, and then "erase" every other idiosyncratic operation that might be supported by any specific type T.
For std::any, the salient operations are as follows:
- Constructing a copy of the contained object
- Constructing a copy of the contained object "by move"
- Getting typeid of the contained object
Construction and destruction are also required, but those two operations are concerned with the lifetime management of the contained object itself, not "what you can do with it," so at least in this case we don't need to consider them.
So we invent a polymorphic class type (call it...