Code Examples
  Home arrow Code Examples arrow Style Case Studies: Construction Unions
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Style Case Studies: Construction Unions
By: Addison-Wesley/Prentice Hall PTR
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2004-11-16

    Table of Contents:
  • Style Case Studies: Construction Unions
  • Solution
  • Dissecting Construction Unions
  • Into the Code
  • Comments
  • Adding New Types
  • Underhanded Names
  • Toward a Better Way: boost::any
  • Alexandrescu’s Discriminated Unions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    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.


    SutterItem 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.

    More Code Examples Articles
    More By Addison-Wesley/Prentice Hall PTR


     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks

    Iron Speed




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway