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