質問

I've created a simple example to show what kind of data structures I have:

#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);

I want to know what happens to my vector<Attribute> attributes after it goes out of scope. Normally with vectors the destructor is called on each element, but what happens if the type of these elements is unknown (as with boost::any or boost::variant)?

役に立ちましたか?

解決

The type is not unknown. It's just variant or dynamic.

The destructor is still run. This is the entire point of using these classes: they provide value semantics[1] on variable typed data.


Or reference-wrapper semantics, if you will, for boost::variant<T&>

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top