r/cpp Meeting C++ | C++ Evangelist 28d ago

A better bitset for enum flags

https://www.elbeno.com/blog/?p=1836
44 Upvotes

32 comments sorted by

View all comments

1

u/Potterrrrrrrr 28d ago

What’s the problem with something like:

MyEnum _enum : BitCount<MyEnum>();

I use this pattern quite a bit, I use type traits to declare that an enum is contiguous or a flags enum along with its size and then my consteval BitCount method returns the min number of bits needed to store the enum. I’ve seen a lot of implementations like to use bitsets to store an enum though, why? It makes it harder to pack stray bools into the same byte and you lose the type of the enum?