r/cpp • Meeting C++ | C++ Evangelist • Jul 19 '26

A better bitset for enum flags

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

29 comments sorted by

View all comments

31

u/03D80085 Jul 19 '26

Neither P4313 nor this suggestion seem particularly ergonomic to me. Why aren't bitfields more widely used for this purpose?

struct permissions {
    bool read  : 1 = false;
    bool write : 1 = false;
    bool exec  : 1 = false;
}

Or a fully typed example: Godbolt. A lot of boilerplate there admittedly, but that is precisely what reflection could help with.

If the answer is that bitfield layouts are implementation defined then that is something we should be working on standardising with appropriate flags rather than introducing new language features.

19

u/ack_error Jul 19 '26

There are several reasons that bitfields are annoying where manual bit flags are generally used.

Bitfields have few guarantees with layout and so are unsuitable for interop, where specific bits must be set in integers of specific size. Plain integer arithmetic is far more universal than C or C++ bitfields.

The common ABIs have finicky rules regarding how types of bitfields affect layout. The type of each bitfield determines the integer that the bitfield is fit within, and in some cases this can require unnatural types to get the bitfields densely packed. https://gcc.godbolt.org/z/eK51eKraq

Should you want to check the bitfield layout at compile time, it requires C++20 for constexpr bit_cast and bitfields must fully and exactly cover the enclosing machine words so the struct is fully initialized... and currently it's not supported in Clang. https://gcc.godbolt.org/z/v8EYnafsn

Member initializers for bitfield members also require C++20.

Bitfields can't be addressed or indexed. This means that patterns for quickly iterating over set bitfields or changing bitfields by a variable index can't be applied without bit-casting to and from integer.

7

u/_Noreturn Jul 19 '26

Bitfields have few guarantees with layout and so are unsuitable for interop,

Yes, which is a shame.

0

u/[deleted] Jul 20 '26

[deleted] — view deleted comment

8

u/_Noreturn Jul 20 '26

It isn't UB it is unspecified there is a difference

0

u/[deleted] Jul 20 '26

[deleted] — view deleted comment

2

u/_Noreturn Jul 20 '26

You are welcome

3

u/fdwr fdwr@github 🔍 Jul 20 '26

But if I wanted to make it correct...

For your particular struct above, I foresee no issues with {clang, gcc, msvc}, as they should yield identical results, since all the bitfields nicely align to the alignof of each base type (e.g. you don't have the : 2 straddling across two bytes). The [[gnu::packed]] shouldn't even be needed, right? Of course, there's still the endianness consideration if you built with gcc on a BE8 architecture, which would flip all the bitfields around (so type, system, and descriptor_privilege_level would become descriptor_privilege_level, system, and type 🙃), but endianness is a bigger problem anyway as you'd have to deal with the non-bitfield fields too like uint16_t segment_limit_low. The remaining sting is the niche compilers for weirder architectures, where say sizeof(char) == sizeof(int).

1

u/archialone Jul 20 '26 edited Jul 20 '26

How about a use case like checking partial flag set? I don't think it's convenient with structs.

Permission user_perms = Permission::Read | Permission::Write | Permission::Execute;

Permission can_read_and_write = Permission::Read | Permission::Write;

if ((user_perms & can_read_and_write) == can_read_and_write)
    std::cout << "Can read and write";

4

u/03D80085 Jul 20 '26

if (permissions.read && permissions.write) ...?

1

u/Brisngr368 Jul 20 '26

Couldn't you use custom operators to make it easier

1

u/archialone Jul 21 '26

What do you mean? Can you give an example?

2

u/Brisngr368 Jul 21 '26 edited Jul 21 '26

Couldn't you define operators for the struct so like '|=' etc to try and simplify this somewhat.

Maybe implement it so you can OR bitfields together and such by overloading the | operator, so you can use the bitfields how you would like to