Style Case Studies: Construction Unions
(Page 1 of 9 )
A union allows more than one object to occupy the same space in memory. Find out how construction unions are used in C++, and what adjustments have had to be made in adopting unions from C. This book excerpt is from
Exceptional C++ Style by Herb Sutter, ISBN 0-201-76042-8, copyright 2005. All rights reserved. It is reprinted with permission from Addison-Wesley Professional.
Editor's Note: This book presents puzzles or problems. A question is posed by JG, a term for new junior-grade military officer, and an answer from a Guru.
Item 36. Construction Unions
No, this Item 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.
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 [Manley02] 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 covered in Questions 1 and 2. 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;
}
4. Show a better way to achieve a generalized variant type, and comment on any tradeoffs you encounter.
This chapter is from Exceptional C++ Style, by Herb Sutter (ISBN 0201760428, copyright 2005. All rights reserved. It is reprinted with permission from Addison-Wesley Professional). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Solution >>
More Code Examples Articles
More By Addison-Wesley/Prentice Hall PTR