Union with anonymous struct

J

JKop

You know how from time to time, you want to have an array which you can
access via:

array_name[element];

But also, you'd like the more user-friendly option:

array_name.element1
array_name.element2

I believe that some people use a union with an anonymous struct for this
(which is not legal Standard C++) as follows:

union Week
{
unsigned days[7];

struct
{
unsigned monday;
unsigned tuesday;
... //and so on
};
} christmas_week;


Well firstly, even if anonymous structs *were* legal, the above code would
still not necessarily work as expected - to be specific, "tuesday" doesn't
necessarily have the same address as days[1], and why? padding.

So... the following is my way of doing it. The only drawback is that it's no
longer an aggreagate nor a POD:

struct Week
{
unsigned days[7];

unsigned &monday;
unsigned &tuesday;
... //and so on

Week() : monday( days[0] ), tuesday( days[1] ) //and so on
};


-JKop
 
C

Chris Theis

JKop said:
You know how from time to time, you want to have an array which you can
access via:

array_name[element];

But also, you'd like the more user-friendly option:

array_name.element1
array_name.element2
[SNIP]

Whether the second option is more userfriendly is arguable. However, you
could use a map with strings as keys if you deem this more legible or
user-friendly.

Chris
 
A

Arijit

Good work. However, your struct will probably require twice as much
memory as the union solution.

Also, padding shouldn't be a problem with ints, but there certainly is
no definite way to tell.

BTW, why is anonymous structs illegal in unions ? Will allowing it
create any specific problem ?

-Arijit
 
J

John Harrison

Arijit said:
Good work. However, your struct will probably require twice as much
memory as the union solution.

Also, padding shouldn't be a problem with ints, but there certainly is
no definite way to tell.

BTW, why is anonymous structs illegal in unions ? Will allowing it
create any specific problem ?

Anonymous structs are not legal at all, in unions or otherwise.

john
 
R

Ron Natalie

John said:
Anonymous structs are not legal at all, in unions or otherwise.
There's not even a definition of that term. But if you take the
C++ defintion of anonymous union and replace union with struct,
it isn't legal.
 
D

Derek

JKop said:
So... the following is my way of doing it. The only drawback
is that it's no longer an aggreagate nor a POD:

Nice, but the other drawback is that it's twice the size of
the original structure.
struct Week
{
unsigned days[7];

unsigned &monday;
unsigned &tuesday;
... //and so on

Week() : monday( days[0] ), tuesday( days[1] ) //and so on
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top