[variant] how to in-place construct a variant
I can't figure out how to in-place construct the contents of a boost::variant. the error message using GCC is merely 516 kb in size. from the boost.variant tutoral:
By default, a variant default-constructs its first bounded type, so v initially contains int(0). If this is not desired, or if the first bounded type is not default-constructible, a variant can be constructed directly from any value convertible to one of its bounded types.
from the synopsis:
template<typename T> variant(const T &); template<typename T> variant & operator=(const T &);
so why does the following fail: struct A : noncopyable{ A(int); A &operator=(int); }; struct B{}; int main(){ A a(5); //ok a=5; //ok variant var(5); //error var=5; //error } both error messages complain about noncopyable being - noncopyable. compiler is gcc 4.3.2 thank you
Stefan Strasser wrote:
I can't figure out how to in-place construct the contents of a boost::variant.
Why do you think that is supported? I see no such thing in the documentation.
so why does the following fail:
struct A : noncopyable{ A(int); A &operator=(int); }; struct B{};
int main(){ A a(5); //ok a=5; //ok variant var(5); //error var=5; //error }
Because variant requires all types it can contain to be CopyConstructible, in particular because it has no support for in-place construction or move semantics.
participants (2)
-
Mathias Gaunard
-
Stefan Strasser