Tip 87 of #TuesdayCodingTips - Emplace into a vector of variants
`emplace_*` is an alternative to methods like `insert` or `push_*` in standard C++ containers. It allows you to construct the inserted object directly inside the container, passing all required parameters down to it, eliminating a needless copy/move along the way.
But what if you have a `std::vector<std::variant<Ts>>`? The interface is not flexible enough to control how to construct the variant object. There is however a hidden workaround.
Suppose your variant can be constructed from a cheap-to-construct type (trivial type, `std::monostate`, or similar). In that case, you can use that to initialize the new element and then invoke `std::variant::emplace` to override the element's memory with another type, presumably one that is expensive to copy or move.
Compiler Explorer link: https://godbolt.org/z/Yec1hvKzP