2012-05-11 CPlusPlus11

Unrestricted Unions

Unions is somewhat of a white area on the C++ map for me. I can't remember that I have ever used a union in a C++ program. Nevertheless C++11 have introduced a way to improve unions.

What it is all about is that before C++11 unions could not have members that had non-trival constructors. So if you had a union like this

   union U
   {
      int i;
      Class c;
   };

and class Class had a non-trivial constructor, it would be illegal. In C++11 this restriction is lifted and the code is legal.