I'm in the process of modernizing a code base and was about to switch from old-style, nested enum definitions to Scoped Enumerations (since C++11..??)
In this particular example we're using enums for flags -
Except the compiler won't handle bitwise OR for the variant with scoped enumerations, even when explicitly providing the type as int
.
Could anybody enlighten me what I'm missing here and why the two flavours of declaring enums are apparently not equivalent?
The compiler error message when using scoped enumerations is
no match for 'operator|' (operand types are 'DrawingStyle' and 'DrawingStyle')
A minimal sample snippet:
#include <iostream>
enum class DrawingStyle : int {
Fill = 1,
Line = 2
};
struct DrawingStyle2 {
enum {
Fill = 1,
Line = 2
};
};
int main(int argc, char * argv[])
{
#if BROKEN
auto test = DrawingStyle::Fill | DrawingStyle::Line;
#else
auto test = DrawingStyle2::Fill | DrawingStyle2::Line;
#endif
std::cout << "Result: "<< test << std::endl;
return 0;
}
Link to sample: https://godbolt.org/z/48x3ccx77