...

/

Dynamically Sized Heterogenous Collections

Dynamically Sized Heterogenous Collections

Get introduced to std::variant, exception safety, and visiting variants for heterogeneous collections.

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:

Press + to interact
auto container = std::vector<std::any>{42, "hi", true};

It has some drawbacks, though. First, every time a value in it is accessed, the type must be tested for at runtime. In other words, we completely lose the type information of the stored value at compile time. Rather, we have to rely on runtime type checks for the information. Secondly, it allocates the object on the heap rather than the stack, which can have significant performance implications.

If we want to iterate our container, we need to explicitly say this to every std::any object: “if you are an int, do this, and if you are a char pointer, do that.” This is not desirable as it requires repeated source code and is also less efficient than other alternatives, which we will cover later in this chapter.

The following example compiles; the type is explicitly tested for and casted upon:

Press + to interact
std::vector<std::any> container = {42, "hello", true};
for (const auto& a : container) {
if (a.type() == typeid(int)) {
const auto& value = std::any_cast<int>(a);
std::cout << value<<" ";
}
else if (a.type() == typeid(const char*)) {
const auto& value = std::any_cast<const char*>(a);
std::cout << value<<" ";
}
else if (a.type() == typeid(bool)) {
const auto& value = std::any_cast<bool>(a);
std::cout << value<<" ";
}
}

We simply cannot print it with a regular stream operator since the std::any object has no idea of how to ...