枚举类型的命名空间 - 最佳做法 [英] namespaces for enum types - best practices
问题描述
我正在寻找所有三种方法的利弊
示例:
//看到手工制作的名称冲突解决方案
枚举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)使用一个类作为命名空间
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:// ...
// ...
}
}
//一个真正的命名空间?
命名空间颜色{enum e {cRed,cBlue,cGreen,cYellow,cEnd}; };
命名空间感觉{enum e {cAngry,cBlue,cHappy,cEnd}; };
void setPenColor(const Colors :: e c){
switch(c){
default:assert(false);
break; case Colors :: cRed:// ...
break; case颜色:: cBlue:// ...
// ...
}
}
命名空间
( class
)是您可以使用使用
声明。
使用命名空间
的问题是,可以在代码的其他位置扩展命名空间。在一个大型项目中,不能保证两个不同的枚举不会被认为是 eFeelings
对于更简单的代码,我使用 struct
,因为你可能希望内容是公开的。
如果你正在做任何这些做法,那么你就超出了曲线,可能不需要再进一步细化。
答案:
如果您使用C ++ 11或更高版本,枚举类
将隐含范围枚举名称中的枚举值。
使用枚举类
将会丢失隐式转换和比较整数类型,但在实践中可能会帮助您标记模糊或错误的代码。
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: //...
//...
}
}
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.
Addendum for this rather dated answer:
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 flag ambiguous or buggy code.
这篇关于枚举类型的命名空间 - 最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!