
On 11 Jun 2025 13:15, Ivan Matek wrote:
On Wed, Jun 11, 2025 at 1:34 AM Andrey Semashev via Boost <boost@lists.boost.org <mailto:boost@lists.boost.org>> wrote:
Hi,
There were quite a few times when I wanted to use bitwise operations on atomic<enum>, where the enum lists a number of flags that could be combined with bitwise AND, OR and XOR.
May I ask a bit more about motivation? More precisely why you prefer this over just using unsigned integers for flag? If you are worried about uniqueness of set bit it can be checked at compile time, e.g. something like this: https://godbolt.org/z/xqG866M8h <https://godbolt.org/z/xqG866M8h> (did not properly test this, so it may have bugs, but this is the idea).
I like "scope" for flags(e,g, Color inColor::Red) but that can be done with namespaces, i.e. namespace Color{ constexpr uint32_t Red ...,
If you have atomic<uint32_t> in your code then it isn't clear what this atomic value is about. It may be a counter, or an index, or a bit mask, or it may be something else entirely. If it is a bit mask, which bits are used and what meaning do they have. You can probably infer this information from the surrounding code and comments, but I would rather prefer atomic<enum>, where the enum part immediately gives you that information. It is enough to just follow to the enum definition to see which values are valid and whether it is a bit mask. It doesn't end with the atomic object declaration either. The surrounding code becomes more clear and type safe as well, as the values that you read and write into the atomic are properly typed with the enum. For example, you won't be putting a random wrong number into the atomic or you won't be passing the value from the atomic where it doesn't belong. The scope part is actually a secondary concern to me. Yes, it is useful to have the value names scoped, but it doesn't always matter all that much, if the enum values have names that are unique enough. The proper types of the constants and the atomic are much more important, IMO.