[英]C++: Can std::variant hold vector, map, and other containers?
According to cppreference, variant is not allowed to allocate dynamic memory.根据 cppreference,不允许变体分配动态 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.我想要一个存储向量和 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!
让我知道你的想法!
According to cppreference, variant is not allowed to allocate dynamic memory.
根据 cppreference,不允许变体分配动态 memory。
You are misunderstanding what that means.你误解了这意味着什么。
std::variant
is not allowed to be implemented by dynamically allocating the contained object, but that contained object is allowed to do whatever it normally does.不允许通过动态分配包含的 object 来实现
std::variant
,但允许包含 object 的内容做它通常做的任何事情。
It is the difference between这是之间的区别
class incorrect_variant {
union {
std::vector<int> * vector;
std::map<int, int> * map;
} u;
enum kind {
is_vec,
is_map,
} k;
public:
incorrect_variant(std::vector<int> value) : u(new std::vector<int>(value)), k(is_vec) {}
// etc
}
class correct_variant {
std::aligned_storage<std::max(sizeof(std::vector<int>), sizeof(std::map<int, int>)> storage;
enum kind {
is_vec,
is_map,
} k;
public:
correct_variant(std::vector<int> value) : k(is_vec)
{
new (storage) std::vector<int>(value);
}
// etc
}
std::vector<T>
is a class with a pointer that manages dynamically allocated memory. std::vector<T>
是一个 class,带有一个管理动态分配的 memory 的指针。
std::variant
by not "being allowed to allocate memory" wouldn't be able to store the std::vector<T>
object itself in dynamic memory, but the vector can manage its own dynamic memory just as fine. std::variant
通过不“被允许分配内存”将无法将std::vector<T>
object 本身存储在动态 memory 中,但向量可以管理自己的动态 ZCD69B4957F06CD818D7BF3D6198。
You're getting it wrong.你搞错了。
The variant
class itself allocates not a single byte (otherwise it would probably have an allocator template argument). variant
class 本身不分配一个字节(否则它可能会有一个分配器模板参数)。 Everything is locally in the class.一切都在 class 中。 But the "varianted" types themselves can allocate as much memory as they like.
但是“变体”类型本身可以根据需要分配尽可能多的 memory。 They own their own memory and none of this concerns
std::variant
.他们拥有自己的 memory ,这与
std::variant
无关。 Isn't a bit like an array of std::string
s.有点像
std::string
的数组。 Sure, a C array doesn't allocate anything on its own, but the individual elements (strings) must do some allocation.当然,C 数组不会自行分配任何内容,但各个元素(字符串)必须进行一些分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.