I have several flag-like enumerations, in C++. For example:
enum some_state {
state_normal = 1 << 0,
state_special = 1 << 1,
state_somethingelse = 1 << 2,
state_none = 0,
};
some_state var1;
Now on using bit operators like & or |, I get compiler errors. I know I can overload operator | et.al. for enums, but I hate to do that again for each and every enum. Is there a nice way to reuse the operator overloads?
I tried and searched, and I think the best solution is a #define. Based on something I found:
This can be used like:
For Visual Studio one might need this to silence some warnings:
In fact, the Windows SDK has the
DEFINE_ENUM_FLAG_OPERATORSmacro to do just that.A different approach is a wrapper class like
DEF_ENUM_FLAGS_TYPEuses.Or use something like the
LLVM_MARK_AS_BITMASK_ENUMmacro. You might need a recent compiler though.