From the course: C++ Development: Advanced Concepts, Lambda Expressions, and Best Practices

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

The any class

The any class

- The any class was introduced with C plus plus 17 to provide a type safe polymorphic container for a single object of any type. This is any.cpp from chapter five of the exercise files. You see we're including the any header. We have a string header and a vector header we're going to be using those as well. We have this function which takes a object of type any, and it prints out its contents and so it first checks if it has a value, and otherwise it checks its type. So the any class has this method type which returns a typeid object and we compare it with the typeid of an int and if it matches then we have a value of type int and we use any cast to retrieve that value. And we put it in the X variable here and we print it out. Otherwise we compare its type to the typeid of a standard string object and we handle that. Otherwise we test for a vector of int and we handle that. And otherwise we…

Contents