According to cppreference, variant is not allowed to allocate dynamic memory. This suggests that variant should not have dynamically-allocated containers as template, like vectors and maps. Yet, some say that it is possible to have a vector as a variant template. Could it be that the variant stores the vector pointer or reference, rather than the actual structure itself?
I would like to have a variant storing a vector and a map. I thought of 2 possibilities:
std::variant<std::vector<int>, std::map<int, int> > x; //stores within the variant itself ??
std::variant <std::uniqur_ptr<std::vector<int> >, std::unique_ptr<std::map<int, int> > > y; //stores only a pointer. The container is allocated elsewhere.
I would prefer the first option for its simplicity. Let me know what you think!
variant<vector>
does not allocate dynamic memory. Thestd::vector
it contains does.