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

C++ enum to its value #143

Open
aben20807 opened this issue Mar 16, 2023 · 0 comments
Open

C++ enum to its value #143

aben20807 opened this issue Mar 16, 2023 · 0 comments
Labels
catg:Cpp catg:PL Programming Language

Comments

@aben20807
Copy link
Owner

https://stackoverflow.com/questions/8357240/how-to-automatically-convert-strongly-typed-enum-into-int/33083231#33083231
https://godbolt.org/z/nvaxW5a74

#include <type_traits>

enum Color {
    Red,
    Green,
    Blue,
};

template <typename E>
constexpr auto to_underlying(E e) noexcept {
    return static_cast<std::underlying_type_t<E>>(e);
}

int main() {
    Color c = Green;
    int c_int = to_underlying(c);
    return c_int;
}
@aben20807 aben20807 added catg:PL Programming Language catg:Cpp labels Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
catg:Cpp catg:PL Programming Language
Projects
None yet
Development

No branches or pull requests

1 participant