我正在玩Visual Studio 11 Beta。我使用的是强类型枚举来形容一些标志在Visual Studio 11中强类型枚举类(测试版)
enum class A : uint32_t
{
VAL1 = 1 << 0,
VAL2 = 1 << 1,
};
uint32_t v = A::VAL1 | A::VAL2; // Fails
当我试图如上我收到以下错误
error C2676: binary '|' : 'A' does not define this operator or a conversion to a type acceptable to the predefined operator
把它们结合起来这与编译器错误或就是我根据C++ 11标准尝试无效?
我的假设是,大家以前枚举声明就相当于写
struct A
{
enum : uint32_t
{
VAL1 = 1 << 0,
VAL2 = 1 << 1,
};
};
uint32_t v = A::VAL1 | A::VAL2; // Succeeds, v = 3
可能的重复[在C++中使用C标志枚举](http://stackoverflow.com/questions/10339076/using-c-flag-enums-in-c) – dasblinkenlight
@dasblinkenlight:我不认为它是一个确切的重复,因为这个问题是关于从'枚举'转换为'int',这是正常的枚举罚款。 –
如果枚举可以隐式转换为不同的类型,那么它不会完全是强类型的......你确定你不想只是普通的旧'enum'吗? – ildjarn