Boost logo

Boost :

From: tobi_at_[hidden]
Date: 2024-03-23 07:02:43


Hi,

I would like to gauge interest in a library that makes the usage of
flag-like enumerations safer.
The library is called Boost.Flags, and its key features are

- provides type-safe bitwise operations for flag-like scoped / unscoped
enumerations
- turns undetected logical errors into type errors

Further characteristics are

- non-intrusive - only a function overload or a template specialization
required to opt-in
- single header-file library
- no dependencies (only standard-library includes)
- everything is constexpr
- zero-overhead in optimized builds
- requires at least C++11, uses newer features if available

 From the Boost.Flags documentation:

> Why do we use the bits of integer types for Boolean options?
>
> - easy to define, e.g. as macros, integer constants or enumerators
> - language support for setting and querying through bitwise operators
> - compact representation in memory
> - simple bulk transfer in interfaces (on many platforms an int can
> hold up to 32 different Boolean options)
> - enforces the usage of names, especially to prevent bool parameters
> in interfaces:
> - "Boolean arguments loudly declare that the function does more than
> one thing. They are confusing and should be eliminated." [C. Martin,
> Clean Code]

But C++ already has everything to work with integer-based flags, so why
do we need Boost.Flags?

The answer is type-safety!

With integer-based flags we can do too much.
E.g. when there are multiple different integer-based flag sets, nothing
prevents the programmer from accidentally mixing flags from unrelated
flag-sets. And of course, there are problems with the bitwise negation
operator~. If you want to know, what possibly could go wrong, read the
docs.

The Github page: https://github.com/tobias-loew/flags
The online docs: https://tobias-loew.github.io/flags
Example on Godbolt: https://godbolt.org/z/qbojncnd7

I am looking forward to your comments!

Tobias


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk