Skip to content

Suggestion: bitshift operators #300

@knoxfighter

Description

@knoxfighter

I would love to bitshift an integral value by an enum value.

Example usage:
Creating a flag enum based on an existing enum:

enum class A {
    X = 0,
    Y = 1,
    Z = 2
};

enum class AFlags {
    XF = 1 << A::X,
    YF = 1 << A::Y,
    ZF = 1 << A::Z
};

General Bitshifting by an enum value:

A a = A::Y;
auto flag = 1 << a;

Currently i have that implemented by the C++23 code:

template<typename E>
requires std::is_enum_v<E>
constexpr auto operator<<(const std::integral auto& a, const E& b) {
    return a << std::to_underlying(b);
}

I suggest implementing a bitshift in both directions by an enum. Maybe you want to add bitshifting an enum by an integral as well, and maybe even an enum by an enum.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions