문제

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