473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Union with anonymous struct


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
Jul 22 '05 #1
5 2713

"JKop" <NU**@NULL.NULL> wrote in message
news:1x*******************@news.indigo.ie...

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
Jul 22 '05 #2
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

JKop <NU**@NULL.NULL> wrote in message news:<1x*******************@news.indigo.ie>...
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

Jul 22 '05 #3

"Arijit" <pa*****@yahoo.co.in> wrote in message
news:df*************************@posting.google.co m...
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
Jul 22 '05 #4
John Harrison wrote:

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.
Jul 22 '05 #5
JKop wrote:
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
};

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: alexwu | last post by:
typedef union _MYUNION struct { short a; short b; }; long c; } MYUNION;
1
by: Michael B Allen | last post by:
Consider the following structure with a union: struct shape { int type; union { struct circle c; struct square s; struct polydoodle p; } u; };
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
18
by: Mockey Chen | last post by:
My friend ask me a question as following: give a union SU define as: typedef union _SU { short x; struct y{ char a; short b; char c;
3
by: Lighter | last post by:
Why does Platform SDK define union LARGE_INTEGER in such a way? ntdef.h defines the union LARGE_INTEGER as follows: typedef union _LARGE_INTEGER { struct { ULONG LowPart;
6
by: steve.kim | last post by:
Hello, I'm trying to make a class like below... class myClass { public: // ctor / dtor .... // methods
2
by: c.a.l | last post by:
Hi, I have found a piece of code which declares union like this: union vector_s { struct {float x,y,z}; float m; } v; there is unnamed structure m ( matrix 1x3 ) which has same offset as x, so...
7
by: David Resnick | last post by:
I'm faced with a header with anonymous structures declared inside a union like this: union msg { struct { int a; int b; } s1; struct {
3
by: SRK | last post by:
Hi, I wanted to use an anonymous union within an structure something like below - struct Test { union { std::string user; //char user; std::string role; //char role;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.