12
votes
  • An enum is a "named collection of constants": enum MyType_e {A, B, C};
  • Those constants are declared in the parent scope of the enum i.e. if the enum is declared in file scope, and is unnamed, it is equivalent to a series of e.g.#define A 0 statements
  • The underlying type for enum constants is always int i.e. int var = A is fully legal, although var is not of type MyType_e

So what purpose does the enum name serve?

EDIT As per the comments below, my understanding of enums appears to be quite flawed. An enum has nothing to do with #define statements. Enums are resolved at compile time, and are typed.

3
Can you point to sections of the C standard where you're getting these ideas from? C doesn't have namespaces (at least in the sense that I think you're referring to here), and nor is the underlying type for enums always int. Or is this from a newer C standard?Paul Hankin
@Anonymous: 6.7.2.2/3 of the C99 standard says "The identifiers in an enumerator list are declared as constants that have type int...". This is kind of confusing, though, since it later goes on to say, "Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined...." So the constants themselves are int, but the enum type itself might not be. By "namespace", I think Vorac meant "scope".jamesdlin
@jamesdlin, Anonymous: I modified the question to be less incorrect. The implementation-dependant type for enums is news for me. Quite interesting.Vorac
"These constants... parent scope... and is unnamed, it is equivalent." Should "is" be "if"? And what's an unnamed scope? I think the question is about enums in C++ and not C, and that correcting "namespace" to "scope" was an error.Paul Hankin
Also, I don't understand the sense in which an enum is ever equivalent to a series of #define statements. The question has lots of upvotes, but it contains fundamentally wrong assumptions.Paul Hankin

3 Answers

8
votes

Using the enum type conveys intent.

Suppose you have a function that takes an enum constant as an argument.

void foo(enum MyType_e e);

is self-documenting about what valid inputs are but:

void foo(int e);

is not. Moreover, the compiler may issue warnings if you attempt to pass incompatible values for the enum type. From Annex I ("Common warnings") of the ISO C99 specification:

An implementation may generate warnings in many situations.... The following are a few of the more common situations.

[...]

  • A value is given to an object of an enumeration type other than by assignment of an enumeration constant that is a member of that type, or an enumeration variable that has the same type, or the value of a function that returns the same enumeration type (6.7.2.2).

Some compilers (for example, gcc) might even generate warnings if you use switch on an enum type but neglect to handle all of its constants and don't have a default case.

3
votes

While you can perfectly say

int var = A

the variant

enum mytype var = A

is better for documentatory reasons.

1
votes

The answers above are good and correct, I just want to add that certain debuggers, are able to show the enum name when hovering or mointoring an enum type variable. This really helps clarity, especially when enums are used for states in state machine implementation.