template<typename T>
Variant(const T &v) { assign(v); }
Variant(const Variant &v) { copy_from(v); }
- ~Variant() { if(funcs) funcs->destroy(storage); }
+ ~Variant() { clear(); }
template<typename T>
Variant &operator=(const T &v) { assign(v); return *this; }
Variant &operator=(const Variant &v) { if(&v!=this) copy_from(v); return *this; }
+ void clear();
+
private:
template<typename T>
void assign(const T &);
};
-template<typename T>
-inline void Variant::assign(const T &v)
+inline void Variant::clear()
{
if(funcs)
funcs->destroy(storage);
+ funcs = nullptr;
+}
+template<typename T>
+inline void Variant::assign(const T &v)
+{
+ clear();
funcs = get_functions<typename std::remove_cv<T>::type>();
create(storage, v);
}
inline void Variant::copy_from(const Variant &v)
{
- if(funcs)
- funcs->destroy(storage);
-
- funcs = v.funcs;
- if(funcs)
+ clear();
+ if((funcs = v.funcs))
funcs->clone(storage, v.storage);
}