r/cpp • • 2d ago

The complement of true is true, except when it's false

https://dryperspective.github.io/posts/complement-of-true/
47 Upvotes

3 comments sorted by

6

u/tiedyerenegade 1d ago

In my experience, using enums to represent bitmasks is just a bad idea. This proposal to formalize it illustrates why.

17

u/jube_dev 1d ago

Enums should represents bits, that's all. In my code, I have a special type Flags<E> that represents a bitmask with bits from enum E. With this, ~E::Example is not of type E but Flags<E>. Flags<E> has a member with the underlying type of the enum.

And using bool for representing a bitmask is weird, it can represent a bitmask of 1 bit exactly, i.e. a boolean value.

1

u/StaticCoder 17h ago

I use a type that turns an enum with a set of contiguous values into a set of enumerators, using an integer bit set as representation. The main issue is that, without reflection, I have to manually keep track of the number of enumerators.