Re: [Interest] Flags: Non-intrusive, type-safe bitwise operators for enums
Dominique Devienne via Boost <[email protected]>
| Newsgroups | gmane.comp.lib.boost.devel |
|---|---|
| Message-ID | <CAFCRh-8p0SLQL0Bxa5SrkO5n1wz2keJXTO8A=O-0TKeVNyJ6hQ@mail.gmail.com> |
On Mon, Mar 16, 2026 at 12:00 PM Tobias Loew via Boost <[email protected]> wrote: > I would like to gauge interest in a library [...] which provides type-safe, > non-intrusive bitwise operators for C++ enumerations. > [...] I am interested in hearing if the community sees a place for this in Boost FWIW, I rolled-up my own a while back, like many I'm sure, so there's obviously a need, IMHO. I'd welcome a Boost quality one, which would be, I'm sure, heads and shoulders better than mine. --DD // Adapted (and fixed) from // https://softwareengineering.stackexchange.com/a/338472/189774 template <typename Enum> class BitFlags { static_assert(std::is_enum_v<Enum>); using underlying_t = typename std::underlying_type<Enum>::type; constexpr const static int number_of_bits = std::numeric_limits<underlying_t>::digits; public: ... private: std::bitset<number_of_bits> bits_; }; template <typename Enum> constexpr std::enable_if_t<std::is_enum_v<Enum>, BitFlags<Enum>> operator|(Enum left, Enum right) { return BitFlags<Enum>(left) | right; } template <typename Enum> constexpr std::enable_if_t<std::is_enum_v<Enum>, BitFlags<Enum>> operator&(Enum left, Enum right) { return BitFlags<Enum>(left) & right; } template <typename Enum> constexpr std::enable_if_t<std::is_enum_v<Enum>, BitFlags<Enum>> operator^(Enum left, Enum right) { return BitFlags<Enum>(left) ^ right; } _______________________________________________ Boost mailing list -- [email protected] To unsubscribe send an email to [email protected] https://lists.boost.org/mailman3/lists/boost.lists.boost.org/ Archived at: https://lists.boost.org/archives/list/[email protected]/message/EVNHKRDZHP6YXY5JHIWFTJ52EVOYKXJI/