Practices - Enumerations
All enumerations should be declared within their own wrapping structure or namespace. The enum itsself should be named 'mnemonic'
struct example { enum mnemonic { first , second , third }; };
Rationale: such construct allows to qualify from where given enumeration comes from by using such call:
example::mnemonic enumerated_symbolic_value = example::first;
Which plays nicely with syntax completion tools such as visual assist or intellisense.
Rationale: it allows to qualify the enumerated values by their holding structure. With:
enum example { first , second , third };
qualifying by enum value is not universally supported. Ie
example enumerated_symbolic_value = example::first;
which would be a syntactic equivalent of the previous construct is not possible.
Additionally, such structs/namespaces allow to explictly define additional functions which are relevant for a given enum. These may be converters, iterators or operators
namespace my_enum { enum mnemonic { a , b , c , d , e , f , g } bool operator==( mnemonic _left, mnemonic _right ); void next( mnemonic & _current ); mnemonic begin(); mnemonic end(); } // iterate over all enumeration members for( my_enum::mnemonic iterator = my_enum::begin() , iterator != my_enum::end() , my_enum::next( iterator ) ) { }