GenericVariables through Unions

I'm trying to implement a GenericVariable (which should allow people to just use that type to do practically anything) by using Unions. I want people to use: bool, long int, long double and std::string.

This is my code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <bitset>
struct GenericVar
{     union GenericVal {bool bo;/* std::string st; */;long int li;long double ld;} Value;
      std::bitset<2> State;
                     // 00 : bool
                     // 01 : std::string
                     // 10 : long int
                     // 11 : long double
      bool& operator=(bool b)                   {State = 0; Value.bo=b; return Value.bo;}
      /* std::string */
      long int& operator=(long int i)           {State = 2; Value.li=i; return Value.li;}
         long int& operator=(int i)             {State = 2; Value.li=i; return Value.li;}
         long int& operator=(unsigned i)        {State = 2; Value.li=i; return Value.li;}
      long double& operator=(long double d)     {State = 3; Value.ld=d; return Value.ld;}
         long double& operator=(double d)       {State = 3; Value.ld=d; return Value.ld;}
         long double& operator=(float d)        {State = 3; Value.ld=d; return Value.ld;}
      
      friend std::ostream& operator<<(std::ostream&,const GenericVar&);};

std::ostream& operator<<(std::ostream& os,const GenericVar& gv)
{     if (gv.State == 0) os << gv.Value.bo;
      // else if (State == 1) os << gv.Value.st;
      else if (gv.State == 2) os << gv.Value.li;
      else os << gv.Value.ld;}

Everything seems to work fine, except for the std::string.. How would I go around adding that?
Last edited on
You can't put a user defined type in a union. You can, however, use a pointer. So you can use a pointer to a string.
The errors I get with my current compiler (which, at that, is pretty old) are:
member 'std::string GenericVar::GenericVal::st' with constructor not allowed in union
member 'std::string GenericVar::GenericVal::st' with destructor not allowed in union
member 'std::string GenericVar::GenericVal::st' with copy assignment operator not allowed in union

Using pointers is an alternative, but I really need to "embed" them in some way, not just refer to them.
Last edited on
A union member cannot have a copy constructor.

Edit:: too slow

Last edited on
So.. how would I go around on doing this without using pointers (storing the value of the string inside the GenericVal union)?
You can't.

You're constructing something akin to Microsoft's VARIANT type. The union holds the built in types and pointers to more complicated bytes. An enclosing struct has a tag, that tracks what type the union is at the moment. You can put a constructor on this enclosing struct/class. But the union pretty much the same primitive device as it is in C.
Have you seen Boost Any?
http://www.boost.org/doc/libs/1_45_0/doc/html/any.html

You appear to be looking to do the first kind of variant type they list.
Hm, both look promising and the variant is indeed more like a union.. but any provides the full functionality I've been looking for, so I think I'll go with that.

The reason I need this functionality is because of the programming language I'm working on, I want it to be very easy to use.
Topic archived. No new replies allowed.