Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: bitshift operators #300

Open
knoxfighter opened this issue Oct 7, 2023 · 0 comments
Open

Suggestion: bitshift operators #300

knoxfighter opened this issue Oct 7, 2023 · 0 comments
Milestone

Comments

@knoxfighter
Copy link

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.

@Neargye Neargye modified the milestones: v0.9.5, v0.9.6 Nov 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants