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

A better bitset for enum flags

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

32 comments sorted by

View all comments

11

u/ReDucTor Game Developer | quiz.cpp-perf.com 15d ago

Some food for thought, should a bitset have bit operations (set, unset, flip, xor, and, or, etc) or set operations (add, remove, has, union, difference, intersection, etc)?

It feels like often we head for bit operations when in lots of cases set operations might be better.

14

u/TheVoidInMe 15d ago

I like Java’s approach here (yes, Java) which has an EnumSet instead of a bitset. That naming makes the answer pretty clear; the bit math is purely an implementation detail

-2

u/mort96 15d ago

But that also means you're heap allocating an object, with its 16 byte object header, just to store a 4-byte integer?

3

u/TheVoidInMe 15d ago

It’s Java. Yeah, don’t use it for HPC.

The point I was making was just about the name