The declaration of an enum in C++ does not create a new naming scope as, for example, declaring a new class or struct does. There is no MyEnum::Value. The enum values become members of the scope that contains the enum declaration and that is what every code completion tool I have seen shows (and certainly what every C++ compiler does when resolving these names). So, for example, you cannot compile this:
struct Test {
enum OneType { One, Two, Three };
enum AnotherType { Three, Four, Five };
OneType one;
AnotherType another;
};
struct Test {
enum OneType { One, Two, Three };
enum AnotherType { Three, Four, Five };
OneType one;
AnotherType another;
};
To copy to clipboard, switch view to plain text mode
$ g++ -c main.cpp
main.cpp:3: error: declaration of ‘Three’
main.cpp:2: error: conflicts with previous declaration ‘Test::OneType Test::Three’
$ g++ -c main.cpp
main.cpp:3: error: declaration of ‘Three’
main.cpp:2: error: conflicts with previous declaration ‘Test::OneType Test::Three’
To copy to clipboard, switch view to plain text mode
You can wrap your enums in classes to also gain type safety, but a typical approach to this using templates, also breaks some code completion implementations (e.g. Qt Creator).
http://en.wikibooks.org/wiki/More_C%...Type_Safe_Enum
Bookmarks