GotW #85

Home Blog Talks Books & Articles Training & Consulting

On the
blog
RSS feed November 4: Other Concurrency Sessions at PDC
November 3
: PDC'09: Tutorial & Panel
October 26: Hoare on Testing
October 23
: Deprecating export Considered for ISO C++0x

This is the original GotW problem and solution substantially as posted to Usenet. See the book Exceptional C++ Style (Addison-Wesley, 2004) for the most current solution to this GotW issue. The solutions in the book have been revised and expanded since their initial appearance in GotW. The book versions also incorporate corrections, new material, and conformance to the final ANSI/ISO C++ standard (1998) and its Technical Corrigendum (2003).

Style Case Study #3: Construction Unions
Difficulty: 4 / 10

No, this issue isn't about organizing carpenters and bricklayers. Rather, it's about deciding between what's cool and what's uncool, good motivations gone astray, and the consequences of subversive activities carried on under the covers. It's about getting around the C++ rule of using constructed objects as members of unions.

Problem

JG Questions

1. What are unions, and what purpose do they serve?

2. What kinds of types cannot be used as members of unions? Why do these limitations exist? Explain.

 

Guru Questions

3. The article in [1] cites the motivating case of writing a scripting language: Say that you want your language to support a single type for variables that at various times can hold an integer, a string, or a list. Creating a union { int i; list<int> l; string s; } doesn't work for the reasons given above. The following code presents a workaround that attempts to support allowing any type to participate in a union. For a more detailed explanation, see the original article.

Critique this code and identify:

a) Mechanical errors, such as invalid syntax or nonportable conventions.

b) Stylistic improvements that would improve code clarity, reusability, and maintainability.

#include <list>
#include <string>
#include <iostream>
using namespace std;

#define max(a,b) (a)>(b)?(a):(b)

typedef list<int> LIST;
typedef string STRING;

struct MYUNION {
  MYUNION() : currtype( NONE ) {}
  ~MYUNION() {cleanup();}

  enum uniontype {NONE,_INT,_LIST,_STRING};
  uniontype currtype;

  inline int& getint();
  inline LIST& getlist();
  inline STRING& getstring();

protected:
  union {
    int i;
    unsigned char buff[max(sizeof(LIST),sizeof(STRING))];
  } U;

  void cleanup();
};

inline int& MYUNION::getint()
{
  if( currtype==_INT ) {
    return U.i;
  } else {
    cleanup();
    currtype=_INT;
    return U.i;
  } // else
}

inline LIST& MYUNION::getlist()
{
  if( currtype==_LIST ) {
    return *(reinterpret_cast<LIST*>(U.buff));
  } else {
    cleanup();
    LIST* ptype = new(U.buff) LIST();
    currtype=_LIST;
    return *ptype;
  } // else
}

inline STRING& MYUNION::getstring()
{
  if( currtype==_STRING) {
    return *(reinterpret_cast<STRING*>(U.buff));
  } else {
    cleanup();
    STRING* ptype = new(U.buff) STRING();
    currtype=_STRING;
    return *ptype;
  } // else
}

void MYUNION::cleanup()
{
  switch( currtype ) {
    case _LIST: {
      LIST& ptype = getlist();
      ptype.~LIST();
      break;
    } // case
    case _STRING: {
      STRING& ptype = getstring();
      ptype.~STRING();
      break;
    } // case
    default: break;
  } // switch
  currtype=NONE;
}

(For an idea of the kinds of things I'm looking for, see also Style Case Study #1 and Style Case Study #2.)

4. Show a better way to achieve a generalized variant type, and comment on any tradeoffs you encounter.

 

Solution

Unions Redux

1. What are unions, and what purpose do they serve?

Unions allow more than one object, of either class or builtin type, to occupy the same space in memory. For example:

// Example 1
//
union U
{
  int i;
  float f;
};

U u;

u.i = 42;    // ok, now i is active
std::cout << u.i << std::endl;

u.f = 3.14f; // ok, now f is active
std::cout << 2 * u.f << std::endl;

But only one of the types can be "active" at a time -- after all, the storage can after all only hold one value at a time. Also, unions only support some kinds of types, which leads us into the next question:

 

2. What kinds of types cannot be used as members of unions? Why do these limitations exist? Explain.

From the C++ standard:

An object of a class with a non-trivial constructor, a non-trivial copy constructor, a non-trivial destructor, or a non-trivial copy assignment operator cannot be a member of a union, nor can an array of such objects.

In brief, for a class type to be usable in a union, it must meet all of the following criteria:

bullet

The only constructors, destructors, and copy assignment operators are the compiler-generated ones.

bullet

There are no virtual functions or virtual base classes.

bullet

Ditto for all of its base classes and nonstatic members (or arrays thereof).