Bit sets - Better Enums (original) (raw)

This is an example of code you can write on top of Better Enums. It's a valid program — you can download it and try it out. The program is also part of the test suite.

Bit sets

If you want to use std::bitset or a similar library to have enums be keys into a bit set, you need to know the number of bits at compile time. You can easily automate this with Better Enums, even when constants are not declared in increasing order.


We simply need to find the maximum value of any given enum type.

#include #include #include <enum.h>

template <_typename Enum_> constexpr Enum maxloop(Enum accumulator, size_t index) { return index >= Enum::size() ? accumulator : Enum::values()[index] > accumulator ? maxloop(Enum::values()[index], index + 1) : maxloop(accumulator, index + 1); }

template <_typename Enum_> constexpr Enum max() { return maxloop(Enum::values()[0], 1); }

And use that to declare a bit set template:

template <typename Enum_> using EnumSet = std::bitset<_max().tointegral() + 1>;

Now, we can have bit sets that are wide enough to hold whatever range we declared. We just declare enums, and the numeric values of their constants will be bit indices. The rest is straightforward.

BETTER_ENUM(EFLAGS, int, Carry, Parity = 2, Adjust = 4, Zero, Sign, Trap, Interrupt, Direction, Overflow, NestedTask = 14, Resume = 16, V8086, AlignmentCheck, CPUIDPresent = 21)

int main() { EnumSet<_EFLAGS_> eflags = 1 << EFLAGS::Carry | 1 << EFLAGS::Zero;

if (eflags.test(_EFLAGS::Carry_))
    eflags.set(_EFLAGS::Trap_);

std::cout << _eflags_ << std::endl;

return 0;

}


If we want bit sets of fixed known width instead, we can use the code above to check that we haven't declared any bit indices out of range: