【问题标题】:Cleaning up after a vector of boost::variant objects在 boost::variant 对象的向量之后进行清理
【发布时间】:2014-04-03 01:27:10
【问题描述】:

我创建了一个简单的示例来展示我拥有什么样的数据结构:

#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&lt;Attribute&gt; attributes 超出范围后会发生什么。通常使用向量,每个元素都会调用析构函数,但如果这些元素的类型未知(如boost::anyboost::variant)会发生什么?

【问题讨论】:

    标签: c++ boost memory-leaks stdvector boost-variant


    【解决方案1】:

    类型未知。它只是 variantdynamic

    析构函数仍在运行。这是使用这些类的全部要点:它们为变量类型的数据提供值语义[1]


    或者引用包装语义,如果你愿意,for boost::variant&lt;T&amp;&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多