[英]Cleaning up after a vector of boost::variant objects
我创建了一个简单的示例来显示我拥有什么样的数据结构:
#include "boost/variant.hpp"
struct Attribute {
boost::variant<vector<double>, vector<std::string>, vector<int64_t> > data;
std::string type;
};
struct Attribute a;
vector<double> vec;
vec.push_back(1);
vec.push_back(2);
a.data = vec;
a.type = "double";
vector<Attribute> attributes;
attributes.push_back(a);
我想知道vector<Attribute> attributes
超出范围后会发生什么。 通常,使用向量时,将在每个元素上调用析构函数,但是如果这些元素的类型未知(如boost::any
或boost::variant
),会发生什么?
类型未知。 它只是变体或动态的 。
析构函数仍在运行。 这是使用这些类的全部要点:它们在变量类型的数据上提供值语义[1] 。
或者,如果需要的话, for boost::variant<T&>
引用包装器语义for boost::variant<T&>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.