I have an enum class I use for bit masking, like so (in Unreal Engine, therefore the uint8 type)
enum class Level : uint8
{
None = 0x0,
Debug = 0x1,
Info = 0x2,
Warning = 0x4,
...
}
I have added inline operators for |
, &
and ^
such that I can use a member as the actual bitmask (e.g. mask = Level::Warning | Level::Debug
), like so:
inline Level operator&(const Level& lhs, const Level& rhs)
{
return (Level)((uint8)lhs & (uint8)rhs);
}
Now at some point I want to query, whether a bit is set or not. However, this does not seem to work without casting to uint8
:
if(mask & Level::Debug) //does not work, C2451
if((uint8)(mask & Level::Debug)) //does work
Is there a way to make the query work without having to cast it to the underlying type?
EDIT:
The goal is to keep the calling code as short (and readable) as possible. The meaning of &
in this case seems to be as clear as using a call such as an extra any
method as suggested in a similar question. Adding the == Level::Debug
code also works of course, but would not shorten the code used. Changing the return type of the operator works, but basically shifts the problem to assignment like Level myLevel = Level::Debug | Level::Warning
, so I wouldn't overall improve my code imo.