Reputation: 11258
GCC tells me you can't use the same names for separate enumerated type values, e.g.
enum flag_one {
SUCCESS,
FAIL
}
enum flag_two {
SUCCESS,
FAIL
}
is not allowed by the compiler. So scoping is not 'witihin' the enum definition?
Is the approach to do something like:
enum flag_one {
FLAG_ONE_SUCCESS,
FLAG_ONE_FAIL
}
enum flag_two {
FLAG_TWO_SUCCESS,
FLAG_TWO_FAIL
}
Slightly confused as I like using enums for return integer codes as its more readable/descriptive but I'm already starting to get name clashes
Upvotes: 4
Views: 1067
Reputation: 111130
So scoping is not 'witihin' the enum definition?
No. This is not allowed. Enumerator lists define constants. Your enum
s happen to be in the same scope -- the file scope. You cannot have two constants with the same name within the same scope.
From the draft of CX:
6.7.2.2 Enumeration specifiers
Semantics
3 The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted.127) [...]
Also, from footnote 127 (which is technically non-normative and for informational purposes only):
127) Thus, the identifiers of enumeration constants declared in the same scope shall all be distinct from each other and from other identifiers declared in ordinary declarators.
.
Slightly confused as I like using enums for return integer codes [...]
Use EXIT_SUCCESS
and EXIT_FAILURE
defined in stdlib.h
.
Upvotes: 6
Reputation: 92261
The enum values are visible in the scope of the enum type. That means they have to be unique in that scope.
Some other language, like C++11, have added a new kind of emums enum class
where the values are not directly visible in the enclosing scope.
You would then have to write flag_one::SUCCESS
and flag_two::SUCCESS
to get the values, so it's not a huge difference from your second option.
Upvotes: 3