Heterogenous Collections Using Variant
Learn how to use variants for heterogenous collections and access values using the std::get() function.
We'll cover the following...
We'll cover the following...
Variant use in heterogenous collections
Now that we have a variant that can store any provided list, we can expand this to a heterogeneous collection. We do this by simply creating a std::vector of our variant:
We can now push elements of different types to our vector:
The vector will now look like this in memory, where every element in the vector contains the size of the variant, which in this case is sizeof(std::size_t) + sizeof(std::string):
Of course, we can also pop_back() or modify the container in any other way the container allows:
Accessing the values in our variant container
Now that we have the ...