Performance & Memory Considerations
Explore the performance and memory considerations of std::variant in C++17, understanding its memory footprint, alignment rules, and how it compares to boost::variant, enabling better informed decisions for efficient code.
We'll cover the following...
We'll cover the following...
std::variant uses the memory in a similar way to union: so it will take the max size of the underlying types. But since we need something that will know what the currently active alternative is, then we need to use some more space. Plus everything needs to honour the alignment rules.
Here are some basic sizes:
On GCC 8.1, 32 bit:
sizeof string: 32
sizeof variant<int, string>: 40
sizeof variant<int, ...