tags:

views:

389

answers:

5

What if I have this:

union{
    vector<int> intVec ;
    vector<float> floatVec ;
    vector<double> doubleVec ;
} ;

Of course, I'll be using just one of the 3 vectors. But... what happens when all the 3 vectors are contructed??
Would the consructors of the 3 vectors interfere with each other?? (since the 3 of them are in the same memory address)

Thanks.

+10  A: 

Current C++ standard does not allow non-POD types inside unions. You will get this compiler error from gcc:

error: member ‘std::vector<int, std::allocator<int> >
<anonymous union>::i’ with constructor not allowed in union
error: member ‘std::vector<int, std::allocator<int> >
<anonymous union>::i’ with destructor not allowed in union

New C++ standard (C++0x) proposes unrestricted unions, but it adds yet more object lifetime pitfalls to C++.

Alex B
Oh, so it's not possible.That's what I thought and it has perfect sense due to the problem I explained in the question.Thanks.
GetFree
+7  A: 

You cannot have unions containing non-POD class types. Your sample will not compile.

You can use boost::variant as a safe alternative to C unions. See the documentation on boost.org. You might, however, reconsider your design and use polymorphism instead. Depends on what you're trying to accomplish, of course.

avakar
Yes, I know Boost::variant and Boost:any. I just wanted to be sure if this would be possible or not before considering a different approach. Thanks.
GetFree
You can have unions containing class types, but they must be POD. see Checkers' answer.
Iraimbilanja, you're right of course. Fixed.
avakar
+2  A: 

From the C++ Standard, section 9.5:

An object of a class with a non-trivial constructor (12.1), a non-trivial copy constructor (12.8), a non-trivial destructor (12.4), or a non-trivial copy assignment operator (13.5.3, 12.8) cannot be a member of a union,

Here, for "non-trivial" read "useful" :-)

anon
Even more than that, trivial implies implicitly defined. If the constructor is defined in code, it is inherently non-trivial.
Volte
+1  A: 

You might want to have a look at Boost.Variant which can contain a single value of varying types.

Volte
+2  A: 

Would the consructors of the 3 vectors interfere with each other?? (since the 3 of them are in the same memory address)

The C++ standard doesn't allow your program, so it's (at best!) implementation-defined what happens.

If, say, your implementation invokes all three default contructors, and those all alocate memory, and stores the pointer to the newly allocated space, the you have a memory leak (the first Two allocations are overwritten by the third).

If the destructors are all invoked and they all free "their" memory, you will be doing a double free (triple, acually); this is likely to corrupt the allocation data structure, which is a Bad Thing. Be happy if you crash, because it's much harder to debug if you don't.

I think these problems might be why the standard doesn't allow this.

(A more sensical thing might be to only default-construct the first class, but that's still not sensical, just less insane...)

Jonas Kölker
So basicly it would be a mess. That's what I thought. And as you say, that's why it is not allowed at all.
GetFree