Re:Type-safe Bitmasks in C++
by 8xde0wcNwpslOw on Wednesday June 13, 2018 @07:26AM
(#17302065)
Bitfields would be, in theory, safer still: no manual bit-twiddling to begin with and also quite straightforward usage. Unfortunately, they are (and I guess will remain) underspecified as far as portability is concerned.
You can, of course, do something similar with member functions, e.g.
class controller
{
unsigned value;
static constexpr unsigned BTN_A = 0x01;
...
public:
bool a() const { return value & BTN_A; }
void a(bool set) { if (set) value |= BTN_A; else value &= ~BTN_A; }
};
You can, of course, do something similar with member functions, e.g.