Dynamically Sized Heterogenous Collections
Get introduced to std::variant, exception safety, and visiting variants for heterogeneous collections.
We'll cover the following
Overview
We started this chapter by noting that the dynamically sized containers offered by C++ are homogenous, meaning that we can only store elements of one type. But sometimes, we need to keep track of a collection that’s dynamic in size that contains elements of different types. To be able to do that, we will use containers containing elements of type std::any
or std::variant
.
The simplest solution is to use std::any
as the base type. The std::any
object can store any value in it:
Get hands-on with 1200+ tech skills courses.