Thread: user defined template question

  1. #1
    Registered User
    Join Date
    Sep 2004

    user defined template question

    I wrot e a template which successfully works for ints, floats, doubles etc. It includes a method which does a swap between two members of the class.
    For example:
    the class decl.
    template< class T1, class T2 > 
    class Pair {
                     Pair(const T1 &x, const T2 &y) : first(x), second(y) {}
                     void setPairX(const T1 &x);
                     void setPairY(const T2 &y);
                     T1 getPairX() const { return first; }  //inline def
                     T2 getPairY() const { return second; } // inline def
                     void swap();
                      T1 first;
                      T2 second;
    the void swap definition looks like this:\
    template< class T1, class T2 >
    void Pair< T1, T2 >::swap()
         union { T1 tmpX; T2 tmpY; };
         if (typeid(first) == typeid(T1)) {
            tmpX = first;
            first = static_cast<T1>(second);
            second = static_cast<T2>(tmpX);
         else {
             tmpY = first;
             first = static_cast<T2>(second);
             second = static_cast<T1>(tmpY);
    My question is: why this doesn't work for "strings"? I get compiler errors, complaining about the anonymous union I used in swap method. Why is that? These two do not work together?
    I'm kinda new to C++, trying hard to memorize all the rules...

  2. #2
    Registered User
    Join Date
    Jun 2005
    C++ classes (with constructors, destructors, member functions, etc) cannot be a member of a union. The C++ string type is a C++ class.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    Uh, you didn't give the union a name. it should be something like
     union { T1 tmpX; T2 tempY; } TmpTypes;
    Also, a union only sets aside enough memory to hold it's largest data member. I need an example:
     union {
       double dollar;
       int yen;
    } currency;
    I can only use dollars or yen, there isn't enough space to keep track of both of them. You might want to use a class instead.
    Last edited by whiteflags; 04-25-2006 at 12:51 AM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Thanks, Grumpy, I did not know that.

    @citizen, this is an "anonymous union", it's working fine for built-in types. And as far as I know, it is legal in C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template overloading question
    By plutino in forum C++ Programming
    Replies: 14
    Last Post: 02-27-2009, 02:10 AM
  2. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  3. Array Help Please (user defined size)
    By Planetx33 in forum C++ Programming
    Replies: 9
    Last Post: 04-07-2007, 04:36 PM
  4. class template user defined obj
    By terracota in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2004, 08:14 AM
  5. Template question
    By grscot in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2003, 03:12 PM