Skip to content

Latest commit

 

History

History
51 lines (42 loc) · 1.67 KB

README.md

File metadata and controls

51 lines (42 loc) · 1.67 KB

bitflags

Clang GCC MSVC Note: currently no released compiler supports reflections

bitflags is a small header-only library that allows you to create bit flags from enumerations. In contrast to many other approaches no magic constants need to be inserted to mark the last element. Also, the values of the enum elements do not have to be multiples of 2, but can be freely selected and macros are not used as well.

Requirements

Example

Live Example https://godbolt.org/z/K71WhYn3v

#include <bitflags/bitflags.h>

enum class Flags {
    Flag1,
    Flag2,
    Flag3,
    Flag4,
    Flag5 = 5,
    Flag6 = 20,
};

auto usage(bitflags::bitflags<Flags> flags) {
    if (flags & Flags::Flag1) {
        std::puts("Flag1 is set");
    }
    if (flags & Flags::Flag2) {
        std::puts("Flag2 is set");
    }
    if (flags & Flags::Flag3) {
        std::puts("Flag3 is set");
    }
    if (flags & Flags::Flag4) {
        std::puts("Flag4 is set");
    }
    // ...
}

int main() {
    using bitflags::operator|; // or using namespace bitflags;
    usage(Flags::Flag1 | Flags::Flag2 | Flags::Flag5);
    return 0;
}