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

Support for max/min Value within Enumeration. #324

Open
stellarpower opened this issue Dec 18, 2023 · 2 comments
Open

Support for max/min Value within Enumeration. #324

stellarpower opened this issue Dec 18, 2023 · 2 comments
Milestone

Comments

@stellarpower
Copy link

Hi,

Would be nice as an extra feature to be able to extract the entry that has the greatest/smallest value for the underlying type. I.e.

enum Suits{
    Clubs = -2,
    Hearts = 4,
    Spades = 7,
    Diamonds = -12
};

static_assert(magic_enum::max<Suits>() == Suits::Spades);

In an ideal world, most enums would start at 0 and go up by one each time, but not always the case.

Form the implementation, looks like some of this is happening already, so hopefully wouldn't be too much work to expose it externally.

Thanks

@Neargye
Copy link
Owner

Neargye commented Dec 19, 2023

Hi,
As a solution, for now, I can recommend using

magic_enum::enum_values<Suits>().front() // min
magic_enum::enum_values<Suits>().back() // max

@stellarpower
Copy link
Author

Great, thanks! So enum_values is guaranteed to be numerically ordered?

@Neargye Neargye added this to the v0.9.6 milestone Jan 1, 2024
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