Reputation: 9
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
Upvotes: 0
Views: 1249
Reputation: 1820
Strongly typed enums do not have operator |
in any form. Have a look there: http://code.google.com/p/mili/wiki/BitwiseEnums
With this header-only library you can write code like
enum class WeatherFlags {
cloudy,
misty,
sunny,
rainy
}
void ShowForecast (bitwise_enum <WeatherFlags> flag);
ShowForecast (WeatherFlags::sunny | WeatherFlags::rainy);
Add: anyway, if you want uint32_t value, you'll have to convert bitwise_enum to uint32_t explicitly, since it's what enum class for: to restrict integer values from enum ones, to eliminate some value checks unless explicit static_casts to and from enum class-value are used.
Upvotes: 0
Reputation: 52365
Strongly typed enums are not implicitly convertible to integer types even if its underlying type is uint32_t
, you need to explicitly cast to uint32_t
to achieve what you are doing.
Upvotes: 2