如果我想排除枚举问题并在C++中键入redefinition,我可以使用代码:
struct VertexType
{
enum
{
Vector2 = 1,
Vertor3 = 2,
Vector4 = 3,
};
};
struct Vector2 { ... };
struct Vector3 { ... };
struct Vector3 { ... };
Run Code Online (Sandbox Code Playgroud)
有没有办法删除枚举上面的包装器.我查看了C++ 0x但没有找到关于解决这个问题的附加信息.
由于您在谈论C++ 0x,只需使用新enum class语法:
enum class VertexType {
Vector1 = 1,
Vector2 = 2,
Vector4 = 3
};
Run Code Online (Sandbox Code Playgroud)
只能通过VertexType类型访问枚举器值VertexType::Vector1.
标准的一些引用:
§7.2/ 2 [...] enum-keys枚举类和枚举结构在语义上是等价的; 使用其中一个声明的枚举类型是作用域枚举,其枚举器是作用域枚举器.[...]
§7.2/ 10 [...]每个范围的枚举器都在枚举范围内声明.[...]
// example in §7.2/10
enum class altitude { high=’h’, low=’l’ };
void h() {
altitude a; // OK
a = high; // error: high not in scope
a = altitude::low; // OK
}
Run Code Online (Sandbox Code Playgroud)