Skip to content

A small header-only library that allows you to create bit flags from enumerations

License

Notifications You must be signed in to change notification settings

MichaelMiller-/bitflags

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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;
}

About

A small header-only library that allows you to create bit flags from enumerations

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published