tags:

views:

252

answers:

5

Hi, i want to use string inside Union. if i write as below

union U
{
   int i;
   float f;
   string s;
};

Compiler gives error saying U::S has copy constructor.

I read some other post for alternate ways for solving this issue. But i want to know why compiler doesn't allow this in the first place?

EDIT: @KennyTM: In any union, if member is initialized others will have garbage values, if none is initialized all will have garbage values. I think, tagged union just provides some comfort to access valid values from Union. Your question: how do you or the compiler write a copy constructor for the union above without extra information? sizeof(string) gives 4 bytes. Based on this, compiler can compare other members sizes and allocate largest allocation(4bytes in our example). Internal string length doesn't matter because it will be stored in a seperate location. Let the string be of any length. All that Union has to know is invoking string class copy constructor with string parameter. In whichever way compiler finds that copy constructor has to be invoked in normal case, similar method as to be followed even when string is inside Union. So i am thinking compiler could do like, allocate 4 bytes. Then if any string is assigned to s, then string class will take care of allocation and copying of that string using its own allocator. So there is no chance of memory corruption as well.

Is string not existed at the time of Union developement in compiler ? So the answer is not clear to me still. Am a new joinee in this site, if anything wrong, pls excuse me.

+4  A: 

In C++98/03, members of a union can't have constructors, destructors, virtual member functions, or base classes.

So basically, you can only use built-in data types, or PODs

Note that it is changing in C++0x: Unrestricted unions

union {
    int z;
    double w;
    string s;  // Illegal in C++98, legal in C++0x.
};
Samuel_xL
+1 for info about c++0x. Thank you.
bjskishore123
+5  A: 

Think about it. How does the compiler know what type is in the union?

It doesn't. The fundamental operation of a union is essentially a bitwise cast. Operations on values contained within unions are only safe when each type can essentially be filled with garbage. std::string can't, because that would result in memory corruption. Use boost::variant or boost::any.

DeadMG
@Matthieu: Pls check edited version of question.According to me, it should not cause memory corruption.
bjskishore123
@bjskishore: sizeof(string) is not portable. Also, the compiler can't find which constructor/etc to call because it doesn't know what type is in the union. All function calls must be known at compiletime or virtual, and a union is neither polymorphic nor compile-time constant. So the compiler can never know what to do. In addition, the size really, really doesn't matter- the problem is that it is a pointer. If you store a float in the union then access a string, you will have a binary float treated as a pointer, which will almost certainly point to invalid memory and crash your program.
DeadMG
+1  A: 

There is no why... but in the next standard (C++0x) you can do it without any problem.

PC2st
+1 for info. ty
Anders K.
There most certainly is a why.
John Dibling
+14  A: 
KennyTM
awesome explanation!
Chubsdad
What is the definition of trivial or non-trivial constructors?
SilverSun
@SiverSun trivial are those that the compiler creates for you, non-trivial those that you code. @KennyTM, great answer indeed!
FireAphis
@Silver: Basically like http://msdn.microsoft.com/en-us/library/bb982179.aspx.
KennyTM
@KennyTM: Pls check the edited question as i am unable to add more text as part of comment.
bjskishore123
@bjs: Maybe you should ask a new question. (Basically, the string needs to allocate some memory from the heap, ...)
KennyTM
+4  A: 

From the C++ spec §9.5.1:

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.

The reason for this rule is that the compiler will never know which of the destructors/constructors call, since it never really knows which of the possible objects is inside the union.

FireAphis