magic_enum icon indicating copy to clipboard operation
magic_enum copied to clipboard

Support for max/min Value within Enumeration.

Open stellarpower opened this issue 2 years ago • 2 comments

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

stellarpower avatar Dec 18 '23 23:12 stellarpower

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

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

Neargye avatar Dec 19 '23 09:12 Neargye

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

stellarpower avatar Dec 19 '23 15:12 stellarpower

Hi, yes, enum_values is guaranteed to be numerically

Neargye avatar Jun 29 '24 13:06 Neargye