Re:Type-safe Bitmasks in C++
by pwagland on Wednesday June 13, 2018 @07:44AM
(#17302163)
Nice technique, however, they don't allow you to do everything that a bitmask does. For example, there is no way to clear a flag, or to select a subset of flags.
That is, given:
enum class Button {
A, B, Select, Start, Up, Down, Right, Left
};
You could also do something similar for the ~ case as well, however that would end up looking a bit uglier, as you would need to create more bit masks, something like:
That is, given:
There is no way to define: And then to find out if either of these buttons are pressed. The only way to do it is: which _does_ generate the optimal code for checking if either is set. However you can't do something like and then call that with something like: Of course, this could be added, see https://godbolt.org/g/48asyeYou could also do something similar for the ~ case as well, however that would end up looking a bit uglier, as you would need to create more bit masks, something like:
For an example, see https://godbolt.org/g/wN2xUV