[英]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.