title: C++11 class with union of string and shared_ptr
body: <p>(this is a bit similar to <a href="https://stackoverflow.com/questions/10693913/c11-anonymous-union-with-non-trivial-members">this question</a>, and inspired by the <a href="http://www.stroustrup.com/C++11FAQ.html#unions" rel="nofollow noreferrer">C++11 FAQ on union</a> but not exactly so...)</p>
  
  <p>In the context of coding a Scheme-like interpreter in idiomatic C++11 Let's suppose I want a tagged union of a string, an int, and some closure. So I would probably code:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;new&gt;
  #include &lt;memory&gt;
  #include &lt;functional&gt;
  enum kind { nothing, string, integer, closure };
  
  class Value {
   enum kind k;
   typedef std::string string_t;
    union {
      std::string str;
      int num;
      std::shared_ptr&lt;std::function&lt;Value(std::vector&lt;Value&gt;)&gt;&gt; clos;
    };
  public:
   Value (const Value &amp;v) 
   : k(none) {
     switch (v.k) {
     case none: break;
     case string: new(&amp;str)string_t(v.str); break;
     case integer: num = v.num; break; 
     /// what about closure-s?
     }
     k = v.k;
   };
   Value&amp; operator = (const Value&amp;v) {
     switch (v.k) {
      case none: break;
      case string: new(&amp;str)string_t(v.str); break;
      case integer: num = v.num; break; 
      /// what about closure-s?
     }
    k = v.k;
   }
   /// etc...
  };
  </code></pre>
  
  <p>Now what about the <code>closure</code> case? For the copy constructor and the assignment operator, I am tempted to code:</p>
  
  <pre><code> case closure: clos = v.clos; break;
  </code></pre>
  
  <p>But perhaps should I use a placement <code>new</code> on a <code>shared_ptr</code>?</p>
  
  <p>I don't want to use Boost (or any non standard C++11 library) for that purpose.</p>