title: Unions used like Classes/Structs
body: <p>I was trying to learn more about unions and their usefulness, when I was surprised that the following code is perfectly valid and works exactly as expected:</p>
  
  <pre><code>template &lt;class T&gt;
  union Foo
  {
      T a;
      float b;
  
      Foo(const T&amp; value)
          : a(value)
      {
      }
  
      Foo(float f)
          : b(f)
      {
      }
  
      void bar()
      {
      }
  
      ~Foo()
      {
      }
  };
  
  int main(int argc, char* argv[])
  {
      Foo&lt;int&gt; foo1(12.0f);
      Foo&lt;int&gt; foo2((int) 12);
  
      foo1.bar();
      foo2.bar();
  
      int s = sizeof(foo1); // s = 4, correct
  
      return 0;
  }
  </code></pre>
  
  <p>Until now, I had no idea that it is legal to declare unions with templates, constructors, destructor, and even member functions. In case it's relevant, I'm using Visual Studio 2012.</p>
  
  <p>When I searched the internet to find more about using unions in this manner, I found nothing. Is this a new feature of C++, or something specific to MSVC? If not, I'd like to learn more about unions, specifically examples of them used like classes (above). If someone could point me to a more detailed explanation of unions and their usage as data structures, it'd be much appreciated.</p>