Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

namespaces for enum types - best practices

Often, one needs several enumerated types together. Sometimes, one has a name clash. Two solutions to this come to mind: use a namespace, or use 'larger' enum element names. Still, the namespace solution has two possible implementations: a dummy class with nested enum, or a full blown namespace.

I'm looking for pros and cons of all three approaches.

Example:

// oft seen hand-crafted name clash solution enum eColors { cRed, cColorBlue, cGreen, cYellow, cColorsEnd }; enum eFeelings { cAngry, cFeelingBlue, cHappy, cFeelingsEnd }; void setPenColor( const eColors c ) {     switch (c) {         default: assert(false);         break; case cRed: //...         break; case cColorBlue: //...         //...     }  }   // (ab)using a class as a namespace class Colors { enum e { cRed, cBlue, cGreen, cYellow, cEnd }; }; class Feelings { enum e { cAngry, cBlue, cHappy, cEnd }; }; void setPenColor( const Colors::e c ) {     switch (c) {         default: assert(false);         break; case Colors::cRed: //...         break; case Colors::cBlue: //...         //...     }  }    // a real namespace?  namespace Colors { enum e { cRed, cBlue, cGreen, cYellow, cEnd }; };  namespace Feelings { enum e { cAngry, cBlue, cHappy, cEnd }; };  void setPenColor( const Colors::e c ) {     switch (c) {         default: assert(false);         break; case Colors::cRed: //...         break; case Colors::cBlue: //...         //...     }   } 
like image 527
xtofl Avatar asked Jan 27 '09 09:01

xtofl


People also ask

Should you store enum in database?

By keeping the enum in your database, and adding a foreign key on the table that contains an enum value you ensure that no code ever enters incorrect values for that column. This helps your data integrity and is the most obvious reason IMO you should have tables for enums.

Can enums have space?

Normally, the name of an enum in c# can't contain any special characters or spaces.

Should enum names be all caps?

Because they are constants, the names of an enum type's fields are in uppercase letters. You should use enum types any time you need to represent a fixed set of constants.

Where should enum be placed?

Put the enums in the namespace where they most logically belong. (And if it's appropriate, yes, nest them in a class.)


2 Answers

Original C++03 answer:

The benefit from a namespace (over a class) is that you can use using declarations when you want.

The problem with using a namespace is that namespaces can be expanded elsewhere in the code. In a large project, you would not be guaranteed that two distinct enums don't both think they are called eFeelings

For simpler-looking code, I use a struct, as you presumably want the contents to be public.

If you're doing any of these practices, you are ahead of the curve and probably don't need to scrutinize this further.

Newer, C++11 advice:

If you are using C++11 or later, enum class will implicitly scope the enum values within the enum's name.

With enum class you will lose implicit conversions and comparisons to integer types, but in practice that may help you discover ambiguous or buggy code.

like image 54
Drew Dormann Avatar answered Sep 20 '22 11:09

Drew Dormann


FYI In C++0x there is a new syntax for cases like what you mentioned (see C++0x wiki page)

enum class eColors { ... }; enum class eFeelings { ... }; 
like image 25
jmihalicza Avatar answered Sep 18 '22 11:09

jmihalicza