tags:

views:

194

answers:

3

I keep looking, but it seems like there's zero interest from compiler developers in supporting these.

To me, it seems odd - basically, current C++ has restrictions on unions that were always an irritation and never appropriate. You'd think that basically removing a few error checks would be a relatively simple way to tick an extra c++0x support box, but AFAICT no compiler developers have done so yet.

Why I'm interested is because it provides a simple solution to a recurring problem in data structure coding - how to reserve memory for an instance of some unknown (template parameter) type, preferably with as much type safety as possible in the circumstances, but without invoking any constructor that happens to be defined on that type. The really important point is that alignment rules must be followed.

An unrestricted union is perfect for this - it gives you a type which has no constructors or destructors, but which has the right size and alignment to allow any member. There are of course ways to explicitly construct and destruct when needed, and when you need typesafe access, you just use the appropriate union member to access it. Support for "proper" unions can be useful too, but you get huge benefits even for a single-member union such as...

union Memory_For_Item_t
{
  Item_t  m_Item;
};

Even with the standardized alignment handling features in C++0x, this approach wins for convenience and safety when e.g. you want space for x items in a node, not all of which will be in use (or constructed) at any time. Without C++0x, we are still in the dark ages WRT alignment issues - every compiler does it its own non-standard way.

The only problem with unrestricted unions - there's no support for them that I can find.

+3  A: 

Near future? I wouldn't count on it. As http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport lays out well, none of the current compilers support it even though many over C++0x features are being implemented.

However as N2544 explains:

The current work-around to the union limitations is to create a fake union using template programming or casts.

So, the situation you are describing probably already has a solution, although a bit messy.

Joseph Piche
Yeah - I have a template approach now, but it relies on preprocessor conditionals to identify compilers and other nastiness, mainly to get the alignment handling right. IOW it's brittle and generally nasty, and the sooner I can replace it the better.
Steve314
Hmmm - iteresting table, but of only three completely blank rows, one just has to be for the feature I'm most looking forward to. Oh well - lamda expressions and closures will be nice, I suppose.
Steve314
A: 

Yeah - I have a template approach now, but it relies on preprocessor conditionals to identify compilers and other nastiness, mainly to get the alignment handling right. IOW it's brittle and generally nasty

Why don't you just use boost::variant? if you're not going to use boost and you're already using C++0x then use type traits such as std::alignment_of/std::aligned_storage those are standardized and cross-platform and if your compiler supports them now you can use variadic templates. Use template meta-programming to compute the largest type in the type list etc.

snk_kid
My template predates boost::variant by quite a bit. Replacing all the uses with some boost feature or C++0x feature that isn't ideal, only to replace it again later, is a bit pointless. I'm not using C++0x at all - just looking forward to being able to use a particular feature. Finally "just use boost" isn't really the magic instant fix you make it out to be - yet another build dependency issue etc etc. If it had long term value then fine, but as things stand I'll just keep on doing what I'm doing until the unrestricted union turns up.
Steve314
@Steve314 it's not pointless, it really depends on what you're using the unions for, if you're going to use it like an algebraic data-type aka discriminated/tagged union then it's not useless at all because a raw (generalized) union isn't useful to work with as is. Things like are boost::variant just as useful in C++0x irregardless of generalized unions or not.
snk_kid
@Steve314 "I'm looking for a way to reserve the memory for a particular type without invoking constructors etc"You don't need a union for that, what you want is **placement** new operator which is only used for construction and explicitly calling the destructor at the correct time. If you're managing the uninitialized memory you still need to be careful of alignment issues and that is why you could use type traits, boost provides type traits for determining alignment and generate aligned storage types at compile-time it's cross-platform, in fact this is how boost::variant works underneath.
snk_kid
@Steve314 First of all you need to turn off the attitude and don't talk to me like that, I'm trying to help you. I never once told you to change any of your existing code and I do not know what your level of C++ is. I only mentioned boost type traits because you said you're not using C++0x features but there exists a cross-platform solution for dealing with alignment already, I don't "love boost" it's just a library/tool.
snk_kid
Stressy comments deleted - sorry for that bad mood stuff.
Steve314
+2  A: 

GCC now lists it as supported in version 4.6. It seems to have been introduced on july 14th, with this patch. The future is now.

Cubbi
like, woah... dude.
Robert Karl