Performance & Memory Considerations

Let's have a quick look at the performance and memory conisderations of std::variant.

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:

Get hands-on with 1200+ tech skills courses.