niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
Tagged unions
Post new topic   Reply to topic Page 1 of 1 [2 Posts] View previous topic :: View next topic
Author Message
Tak-Shing Chan
*nix forums beginner


Joined: 10 Jul 2006
Posts: 19

PostPosted: Fri Jul 21, 2006 1:50 pm    Post subject: Re: Tagged unions Reply with quote

On Fri, 21 Jul 2006, Johan Tibell wrote:

Quote:
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).

Quote:
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
Back to top
johan.tibell@gmail.com
*nix forums beginner


Joined: 18 Jul 2006
Posts: 3

PostPosted: Fri Jul 21, 2006 1:00 pm    Post subject: Tagged unions Reply with quote

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).
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [2 Posts] View previous topic :: View next topic
The time now is Thu Nov 20, 2008 8:14 am | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Perl +lists/unions/intersection: oleg Perl 9 Wed Jul 12, 2006 2:13 am
No new posts classes in unions? uralmutlu@gmail.com C++ 4 Mon Jul 10, 2006 9:21 pm
No new posts Warning comes when using Unions Kantha C 13 Thu May 25, 2006 5:47 am
No new posts gcc, aliasing rules and unions Hallvard B Furuseth C 3 Tue Apr 18, 2006 1:05 am
No new posts Problem with drives "tagged as part of hardware RAID array" danielbuus@gmail.com Suse 4 Thu Apr 06, 2006 10:37 am

Loans | Bad Credit Loan | Remortgaging | Remortgaging | Mortgage Calculator
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1362s ][ Queries: 20 (0.0631s) ][ GZIP on - Debug on ]