title: Constructor and copy-constructor for class containing union with non-trivial members
body: <p>I am trying to implement a custom variant type which uses a union to store data of various different types. In the field <code>type_id</code> I plan to store which type the data stored in the union is of. The union contains non-trivial members. Here is my current implementation:</p>
  
  <pre><code>struct MyVariant {
    enum { t_invalid, t_string, t_int, t_double, t_ptr, t_dictionary } type_id;
    union {
      int                             as_int;
      double                          as_double;
      std::string                     as_string;
      std::unique_ptr&lt;int&gt;            as_ptr;
      std::map&lt;int, double&gt;           as_dictionary;
    };
  };
  </code></pre>
  
  <p>I try to create an instance of <code>MyVariant</code> like follows:</p>
  
  <pre><code>MyVariant v;
  </code></pre>
  
  <p>I get the error message: <em>Call to implicitly-deleted default constructor of MyVariant</em>. So, I tried to implement the constructor manually like follows:</p>
  
  <pre><code>MyVariant() : type_id{t_int}, as_int{0} {}
  </code></pre>
  
  <p>That gives me a similar error message: <em>Attempt to use a deleted function</em>.
  Next, I tried to implement the following constructor:</p>
  
  <pre><code>MyVariant(int value) : type_id{t_int}, as_int{value} {}
  </code></pre>
  
  <p>and construct my instance like follows: </p>
  
  <pre><code>MyVariant v{123};
  </code></pre>
  
  <p>=> same error message: <em>Attempt to use a deleted function</em>.</p>
  
  <p>I've also started to implement a copy constructor, it looks like follows. However, of course this doesn't help with the compiler errors.</p>
  
  <pre><code>MyVariant::MyVariant(const MyVariant&amp; other)
  {
      type_id = other.type_id;
      switch (type_id) {
          case t_invalid:
              break;
          case t_string:
              new (&amp;as_string) std::string();
              as_string = other.as_string;
              break;
          case t_int:
              as_int = other.as_int;
              break;
          case t_double:
              as_double = other.as_double;
              break;
          case t_ptr:
              new (&amp;as_ptr) std::unique_ptr&lt;int&gt;(nullptr);
              as_ptr = std::make_unique&lt;int&gt;(*other.as_ptr);
              break;
          case t_dictionary:
              new (&amp;as_dictionary) std::map&lt;int, double&gt;();
              // TODO: copy values from other
              break;
      }
  }
  </code></pre>
  
  <p>I am using Xcode and Apple LLVM 6.1 as compiler.</p>
  
  <p>The main question is: Why do I get the compiler errors which I'm getting and how do I have to modify my code to make it compile?</p>
  
  <p>The additional question is: Am I on the right way with my implementations for the constructor and copy constructor?</p>