473,394 Members | 1,739 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,394 software developers and data experts.

Tagged unions

I use a tagged union to represent different expression types in one of
my programs.

struct exp {
enum {
LIT,
VAR
} type;
union {
int lit;
char *var;
} form;
};

In my implementation I've put the enum outside of the struct and given
it a name, "exp_type".

enum exp_type { /* ... */ };

struct my_struct {
enum exp_type type;
/* ... */
};

What would be the pros and cons of having it unnamed inside the struct
versus named outside the struct respectively? I can think of a few:

Pros:
* Less pollution of the namespace. I currently have two different
structs so I have to prefix my enum type names with "structname_" (e.g.
exp_type).
* Saves me some typing.
* Avoid repetition of the name "type" in the variable declaration
inside the struct (e.g. exp_type type).

Cons:
* Can't create a variable of the enum type since the type can't be
referred to. (Would it even be possible to refer to the enum type if it
was named _and_ declared inside the struct?). This can also be a good
thing if no more variables of the enum type will ever be created but it
can be a bit difficult to predict in advance.

This is probably more of a stylistic question than anything else (and
hence I expect 10^100 replies).

Jul 21 '06 #1
7 5065
On Fri, 21 Jul 2006, Johan Tibell wrote:
I use a tagged union to represent different expression types in one of
my programs.

struct exp {
enum {
LIT,
VAR
} type;
union {
int lit;
char *var;
} form;
};

In my implementation I've put the enum outside of the struct and given
it a name, "exp_type".

enum exp_type { /* ... */ };

struct my_struct {
enum exp_type type;
/* ... */
};

What would be the pros and cons of having it unnamed inside the struct
versus named outside the struct respectively? I can think of a few:

Pros:
* Less pollution of the namespace. I currently have two different
structs so I have to prefix my enum type names with "structname_" (e.g.
exp_type).
* Saves me some typing.
* Avoid repetition of the name "type" in the variable declaration
inside the struct (e.g. exp_type type).

Cons:
* Can't create a variable of the enum type since the type can't be
referred to. (Would it even be possible to refer to the enum type if it
was named _and_ declared inside the struct?). This can also be a good
thing if no more variables of the enum type will ever be created but it
can be a bit difficult to predict in advance.
Why would you ever need to create such variables? That is
bad programming practice in my book (creating unnecessary
couplings).
This is probably more of a stylistic question than anything else (and
hence I expect 10^100 replies).
My preference is to use anonymous enums in this situation.

Tak-Shing
Jul 21 '06 #2
Tak-Shing Chan wrote:
I use a tagged union to represent different expression types in one of
my programs.

Why would you ever need to create such variables? That is
bad programming practice in my book (creating unnecessary
couplings).
In this case I'm representing an abstract syntax tree (AST) created
using lex/yacc which will be passed to a function eval for evaluation.
In functional languages one usually uses an algebraic data type to
represent such ASTs and in OO languages a class hierarchy is often
(always?) used. I assumed that a tagged union would be the
corresponding representation in C. If you know of a better alternative
please let me know, I'm not an experienced C programmer.

Or perhaps I don't quite understand what part of my implementation you
think is bad. Are you referring to the whole tagged union thing?

Jul 21 '06 #3
On Fri, 21 Jul 2006, Johan Tibell wrote:
Tak-Shing Chan wrote:
>>I use a tagged union to represent different expression types in one of
my programs.

Why would you ever need to create such variables? That is
bad programming practice in my book (creating unnecessary
couplings).
[You are quoting me out of context here. By ``such
variables'' I am referring to reused enums, not tagged unions.]
In this case I'm representing an abstract syntax tree (AST) created
using lex/yacc which will be passed to a function eval for evaluation.
In functional languages one usually uses an algebraic data type to
represent such ASTs and in OO languages a class hierarchy is often
(always?) used. I assumed that a tagged union would be the
corresponding representation in C. If you know of a better alternative
please let me know, I'm not an experienced C programmer.

Or perhaps I don't quite understand what part of my implementation you
think is bad. Are you referring to the whole tagged union thing?
You have misread my post. What I said was, tagged unions
are fine but reused enums are bad (in the context of this
thread). IMHO. YMMV.

Tak-Shing
Jul 21 '06 #4
"Johan Tibell" <jo**********@gmail.comwrites:
struct exp {
enum {
LIT,
VAR
} type;
union {
int lit;
char *var;
} form;
};
....versus...
enum exp_type { /* ... */ };

struct my_struct {
enum exp_type type;
/* ... */
};
Cons:
* Can't create a variable of the enum type since the type can't be
referred to. (Would it even be possible to refer to the enum type if it
was named _and_ declared inside the struct?).
(Yes, it would.)
This can also be a good thing if no more variables of the enum
type will ever be created but it can be a bit difficult to
predict in advance.
I often use the former style, where the enum is declared without
a tag inside the struct. If later it becomes necessary to refer
to its type explicitly (which is fairly rare), it's only the
matter of a moment's work to add a tag.
--
"When in doubt, treat ``feature'' as a pejorative.
(Think of a hundred-bladed Swiss army knife.)"
--Kernighan and Plauger, _Software Tools_
Jul 21 '06 #5
Johan Tibell wrote:
I use a tagged union to represent different expression types in one of
my programs.

struct exp {
enum {
LIT,
VAR
} type;
union {
int lit;
char *var;
} form;
};

In my implementation I've put the enum outside of the struct and given
it a name, "exp_type".

enum exp_type { /* ... */ };

struct my_struct {
enum exp_type type;
/* ... */
};

What would be the pros and cons of having it unnamed inside the struct
versus named outside the struct respectively? I can think of a few:

Pros:
* Less pollution of the namespace. I currently have two different
structs so I have to prefix my enum type names with "structname_" (e.g.
exp_type).
* Saves me some typing.
* Avoid repetition of the name "type" in the variable declaration
inside the struct (e.g. exp_type type).
Cons:
* Can't create a variable of the enum type since the type can't be
referred to. (Would it even be possible to refer to the enum type if it
was named _and_ declared inside the struct?). This can also be a good
thing if no more variables of the enum type will ever be created but it
can be a bit difficult to predict in advance.

This is probably more of a stylistic question than anything else (and
hence I expect 10^100 replies).
If something like this is for use inside a programming langauge
implementation it is likely to be used a lot. In this case I'd
recommend separating the meaning of the data from it's structure. You
could for example create a set of inline functions that access parts of
the struct. That way it becomes much easier to change the inside of
the struct without breaking other things. (eg functions like get_type,
set_type, get_lit, etc)

This type of pseudo-OO in C isn't always a good idea, but it's useful
for something like this.

Jul 21 '06 #6
Tak-Shing Chan wrote:
[You are quoting me out of context here. By ``such
variables'' I am referring to reused enums, not tagged unions.]
I was a bit unsure about which part you were addressing (since you
almost quoted my entire message) and therefore I included the caveat at
the very end of my message. Thank you for the clarification.

Jul 21 '06 #7
In article <11**********************@i42g2000cwa.googlegroups .com>
Johan Tibell <jo**********@gmail.comwrote:

[vertically compressed]
>struct exp {
enum { LIT, VAR } type;
union { int lit; char *var; } form;
};
[vs
enum exp_type { LIT, VAR };
struct exp {
enum exp_type type;
union { int lit; char *var; } form;
};
]
>What would be the pros and cons of having it unnamed inside the struct
versus named outside the struct respectively? I can think of a few:

Pros:
* Less pollution of the namespace. I currently have two different
structs so I have to prefix my enum type names with "structname_" (e.g.
exp_type).
This is a smaller pro than it may look: enumeration members are
in the ordinary namespace, at the same scope as the overall definition
of the structure type, so LIT and VAR can appear anywhere up to
the end of the current scope and must be unique. That is:

struct expression {
enum { LIT, VAR } type;
...
};
struct fuse {
enum { UNLIT, LIT } type;
...
};

is no good -- the two "LIT"s conflict. (In C++ each struct has its
own little sub-namespace, but C is not C++.)

Thus, the only namespace you avoid polluting is the "tag" namespace.
>* Saves me some typing.
Not much, since you can also write:

struct exp {
enum exp_type { LIT, VAR } type;
union { ... } form;
}
>* Avoid repetition of the name "type" in the variable declaration
inside the struct (e.g. exp_type type).

Cons:
* Can't create a variable of the enum type since the type can't be
referred to. (Would it even be possible to refer to the enum type if it
was named _and_ declared inside the struct?).
Easily fixed by adding an enum tag, as above. Yes, you can refer to
"embedded" types afterward in C:

struct foo {
enum zot { ZOT_A, ZOT_B } zot;
struct bar {
int i;
};
char *p;
};
enum zot zed;
struct bar bar;

(Again, C++ is different -- another reason not to try to compile C
code with a C++ compiler: valid C code is sometimes invalid, but
sometimes valid yet meaning something else, in C++.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 22 '06 #8

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

Similar topics

15
by: David | last post by:
Some developers in my group are using UNIONS to define their data types in a C++ program for an embedded system. Are there any pro and cons in doing this when you can define a CLASS to do the same...
4
by: Brian Wilson | last post by:
Hi Newsgroup! I know many of you must have bought the Access Developer's Handbook and used the Tagged Values class, so perhaps I can ask someone to check something. On page 303, it describes the...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
16
by: Tim Cambrant | last post by:
Hi. I was reading up a bit on the features of C I seldom use, and I came across unions. I understand the concept, and that all the contained variables etc. share the same memory. Thus, when a new...
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
4
by: uralmutlu | last post by:
Hi, I was wandering if I can have classes in unions? I basically have source code in a format very similar to: union example { ClassA variable1; ClassB variable2; };
67
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the...
26
by: Old Wolf | last post by:
Ok, we've had two long and haphazard threads about unions recently, and I still don't feel any closer to certainty about what is permitted and what isn't. The other thread topics were "Real Life...
11
by: pereges | last post by:
Hello, can some one please guide me a little into using unions. I read about unions in K & R but I am finding it difficult to apply to my problem at hand. I want to save up some space by using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.