The Basics
Explore how std::any from C++17 allows you to store any type safely with type checking and error handling. Learn its main functions, usage scenarios, and advantages over unsafe void pointers to manage diverse data types effectively in your programs.
We'll cover the following...
We'll cover the following...
In C++14 there weren’t many options for holding variable types in a variable. You could use void*,
of course, but this wasn’t safe. void* is just a raw pointer, and you have to manage the whole object
lifetime and protect it from casting to a different type.
Potentially, void* could be wrapped in a class with some type discriminator.
Making Type-Safe
As you see, we have some basic form of the type, but there’s a bit of coding required to make sure MyAny is type-safe. ...