September 2017
Beginner to intermediate
384 pages
9h 18m
English
std::any occupies a position in between the compile-time polymorphism of std::variant<A, B, C> and the runtime polymorphism of polymorphic inheritance hierarchies and dynamic_cast. You might wonder whether std::any interacts with the machinery of dynamic_cast at all. The answer is "no, it does not"--nor is there any standard way to get that behavior. std::any is one hundred percent statically type-safe: there is no way to break into it and get a "pointer to the data" (for example, a void *) without knowing the exact static type of that data:
struct Animal { virtual ~Animal() = default; }; struct Cat : Animal {}; void test() { std::any a = Cat{}; // The held object is a "Cat"... assert(a.type() == typeid(Cat)); ...Read now
Unlock full access