ifarbod / cpp17-enum-flag

Enable bitwise operators on scoped enums.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cpp17-enum-flag

Enable bitwise operators on scoped enums.

Library compiles with no warnings (-std=c++17 -Wall -Wshadow -Wextra -Wnon-virtual-dtor -pedantic) on Clang 14.

Example usage

enum class TestEnum
{
    Baz = 1 << 0,
    Fou = 1 << 1,
    Moo = 1 << 2,
    Foo = 1 << 3,
    Bar = 1 << 4,
};

template <>
struct zsl::EnumFlags<TestEnum>
{
    static constexpr bool is_flags = true;
};

// ...

auto main() -> int
{
    TestEnum e{TestEnum::Baz};
    auto b = zsl::EnumIsSet(e & TestEnum::Baz);
    std::cout << (b ? "true" : "false");
    
    e = TestEnum::Foo | TestEnum::Baz;
    return 0;
}

About

Enable bitwise operators on scoped enums.

License:MIT License


Languages

Language:C++ 100.0%