0

Default constructor of boost variant initializes the first bounded type. How do I default initialize any member other than the first? One option is to simply assign a default constructed object of the right type. However, that does not work with some types like arrays. Without initialization boost::get throws boost::bad_get exception. So I can't do std::copy on the array.

boost::variant<std:string, int[5]> v; // default initializes the string. 

I need something like

initialize<int[5]>(v);

I also had compilation issues when using an array as the first bounded type. Clang reports error as follows:

object expression of non-scalar type 'int [5]' cannot be used in a pseudo-destructor expression 
  operand.~T();

Edit: I've the same question regarding boost.optional. Is there an API to default initialize an optional? I mean other than an assignment from a default initialized T.

Sumant
  • 4,286
  • 1
  • 23
  • 31
  • It seems variant requires each bounded type to be CopyConstructible or MoveConstructible, which arrays are not (in C++03 at least). – Sumant Jul 08 '13 at 21:40
  • Using the InPlaceFactory and TypedInPlaceFactory for boost.optional I can do default initialization. Pretty cool! Boost variant, however, does not seem to support the InPlaceFactory idiom. – Sumant Jul 09 '13 at 23:55

0 Answers0