Can you implement standard conformant (as described in 17.5.2.1.3 of the n3242 draft) type safe bitmasks using enum class? The way I read it, a type T is a bitmask if it supports the |,&,^,~,|=,&= and ^= operators and further you can do if(l&r) where l and r are of type T. Missing from the list are the operator != and == and to allow sorting one probably also wants to overload <.
Getting the operators to works is just annoying boilerplate code but I do not see how to do if(l&r). At least the following does not compile with GCC (besides being extremely dangerous as it allows an erroneous implicit conversion to int):
enum class Foo{
operator bool(){
return (unsigned)*this;
}
};
EDIT: I now know for certain that enum classes can not have members. The actual question how to do if(l&r) remains though.
bool; why mustif (l&r)be well-formed?static_casts you'll need to make it compile). The way I see it, if you want a bitfield, you should use anenum, not anenum class.has_flagsfunction to test if a value has all of a certain set of flags set. I find it is a lot better than&, because it can check multiple flags without potential for mistakes (writing(l&r)instead of(l&r)==r); and it is a lot more readable.