I’m playing around with the Visual Studio 11 Beta at the moment. I’m using a strongly typed enum to describe some flags
enum class A : uint32_t
{
VAL1 = 1 << 0,
VAL2 = 1 << 1,
};
uint32_t v = A::VAL1 | A::VAL2; // Fails
When I attempt to combine them as above I get the following error
error C2676: binary '|' : 'A' does not define this operator or a conversion to a type acceptable to the predefined operator
Is this a bug with the compiler or is what I’m attempting invalid according to the c++11 standard?
My assumption had been that the previous enum declaration would be equivalent to writing
struct A
{
enum : uint32_t
{
VAL1 = 1 << 0,
VAL2 = 1 << 1,
};
};
uint32_t v = A::VAL1 | A::VAL2; // Succeeds, v = 3
Strongly typed enums are not implicitly convertible to integer types even if its underlying type is
uint32_t, you need to explicitly cast touint32_tto achieve what you are doing.