Now that we have the boilerplate for a heterogeneous container of dynamic size, let's see how we can use it like a regular std::vector:
- Construct a heterogeneous container of variants: Here we construct a std::vector with different types, note that the initializer list contains different types:
using VariantType = std::variant<int, std::string, bool>;
auto c = std::vector<VariantType> { 42, std::string{"needle"}, true };
- Print the content by iterating with a regular for loop: To iterate the container with a regular for-loop we utilize std::visit() and a polymorphic lambda. The global function std::visit() takes care of the type conversion. The example prints each value to std::cout, independent ...