Is there a way to find the maximum and minimum defined values of an enum in c++?
-
Take a look at the https://github.com/Neargye/magic_enum lib – dalle Jun 10 '19 at 09:05
-
3One reason I might want to know the max value of an enum without adding it to the enum itself is so my compiler doesn't complain (C4061) when my 'switch' statement does not explicitly handle the "max value" value. – snips-n-snails Nov 19 '19 at 23:59
7 Answers
No, there is no way to find the maximum and minimum defined values of any enum in C++. When this kind of information is needed, it is often good practice to define a Last and First value. For example,
enum MyPretendEnum
{
Apples,
Oranges,
Pears,
Bananas,
First = Apples,
Last = Bananas
};
There do not need to be named values for every value between First
and Last
.

- 61,417
- 20
- 137
- 189
-
3Still useful for non-sequential enums - as long as you dont expect every value to exist – Adrian Cornish Jan 11 '12 at 06:50
-
@Adrian: Yes, that is true. You don't need every value to have a defined name for this technique to be useful. I'll edit accordingly – Jeff Yates Jan 11 '12 at 20:22
-
No, not in standard C++. You could do it manually:
enum Name
{
val0,
val1,
val2,
num_values
};
num_values
will contain the number of values in the enum.

- 18,057
- 5
- 57
- 81
-
1
-
15@lizusek: `num_values` will contain the number of values in the enum, except `num_values` itself. – dalle Mar 19 '14 at 16:43
-
3@dalie yes, that is why it will be: number of values - 1. Of course I know that you mean a values that means values excluding last one which is not a real value, but a helper. However I would suggest to make it more clearer. – 4pie0 Mar 19 '14 at 16:46
-
1
-
-
1I prefer this approach over others as there is no need to update it's value when new ones are added so long as it's the last one in the list – hookenz May 30 '19 at 00:32
-
1The downside of this approach is that introduces an enum option that should never be used. You have to trust (or verify) that coders never set a variable to num_values. Because of this, I normally prefer the accepted answer. On way this option can be helpful is if you wish to have an invalid NA option for variables. You can initialize a variable to num_values when instantiating it. If you encounter the value later, you know the value was never set to a non-default option. – Ellis Miller Apr 05 '21 at 06:05
No. An enum in C or C++ is simply a list of constants. There is no higher structure that would hold such information.
Usually when I need this kind of information I include in the enum a max and min value something like this:
enum {
eAaa = 1,
eBbb,
eCccc,
eMin = eAaaa,
eMax = eCccc
}
See this web page for some examples of how this can be useful: Stupid Enum Tricks

- 1,886
- 3
- 17
- 24
-
1the article you link is from a time where the authors compiler "does not support the new official C++ Boolean type" .... I have never read something that ancient before :P. – 463035818_is_not_an_ai Jun 23 '20 at 11:47
-
Version 5.0 of Borland C++ added support for `bool` as part of the language itself, around 1997. C added `_Bool` as a native type in 1999. I'm not sure when Turbo C++ added support for `bool`. – Mooing Duck Nov 09 '22 at 16:10
enum My_enum
{
FIRST_VALUE = 0,
MY_VALUE1,
MY_VALUE2,
...
MY_VALUEN,
LAST_VALUE
};
after definition, My_enum::LAST_VALUE== N+1

- 541
- 3
- 11
-
-
For systems using strongly-typed data, this introduces a new valid enum entry that we must deal with. This is why the accepted answer is preferred. – Brent K. Aug 17 '23 at 11:58
Although the accepted answer correctly states that there is no standardized way to get the min
and max
values of enum
elements, there is at least one possible way in newer versions of gcc (>= 9.0), which allows to write this:
enum class Fruits { Apples, Oranges, Pears, Bananas };
int main() {
std::cout << "Min value for Fruits is " << EnumMin<Fruits>::value << std::endl; // 0
std::cout << "Max value for Fruits is " << EnumMax<Fruits>::value << std::endl; // 3
std::cout << "Name: " << getName<Fruits, static_cast<Fruits>(0)>().cStr() << std::endl; // Apples
std::cout << "Name: " << getName<Fruits, static_cast<Fruits>(3)>().cStr() << std::endl; // Bananas
std::cout << "Name: " << getName<Fruits, static_cast<Fruits>(99)>().cStr() << std::endl; // (Fruits)99
}
This works without any custom traits or hints.
It's a very rough proof of concept and I'm sure it can be extended much further, this is just to show that this is possible today.
This snippet compiles in C++14 and with a few tweaks, it can definitely run also in C++11, but I don't think this would have been possible in pre-C++11
WARNING: This might break in the future compiler releases.

- 1,903
- 3
- 22
- 46
-
-
-
Just glanced at it, but it looks like you're just trying every value in the numeric_limits of the underlying type, which would (will?) take FOREVER for enums with -say- an uint64_t as underlying type. It would be more interesting if it found the maximum and minimum value very quickly (i.e. in constant time). – Carlo Wood Feb 16 '23 at 21:35
-
@CarloWood What you see in the demo is a minimal proof of concept. You can obviously make numerous optimizations to make it faster. For example, use binary search instead of the linear search. But remember that this is just a hack anyway and may break at any point. – ProXicT Feb 19 '23 at 20:10
-
There is no way to do a binary search I think. That would require a means to compare if an attempt is greater or less than "the maximum"; but all that can be done is use it as a specialization of a template and then see if __PRETTY_FUNCTION_NAME__ spits out something or not. When I post the above I thought that perhaps it was possible (and had hoped you were doing that) but now I think there is no way to do it differently then like -say- magic enum does it. Unless you demand users to use a macro to define enums (which is what I do now). – Carlo Wood Mar 02 '23 at 20:58
-
Well... if you know that the enum values are defined in a contiguous range (e.g. 1, 2, 3, 4, ... N-1, N), then a binary search for N is possible. I suppose that is what you meant. But often they are not like that and that is the case that I was talking about. – Carlo Wood Mar 02 '23 at 21:00
-
@CarloWood Of course, the binary search assumes the values in the enum are contiguous. But it's surely possible. – ProXicT Mar 04 '23 at 00:08
If it is certain that all enum values are in ascending order (or at least the last one is guaranteed to have the greatest value) then magic enum library can be used without need to add an extra element to enum definition. It is used like this:
#include <magic_enum.hpp>
...
const size_t maxValue = static_cast<size_t>(magic_enum::enum_value<MyEnum>(magic_enum::enum_count<MyEnum>() - 1));
Magic enum git: https://github.com/Neargye/magic_enum

- 101
- 1
- 8
you don't even need them, what I do is just I say for example if you have:
enum Name{val0,val1,val2};
if you have switch statement and to check if the last value was reached do as the following:
if(selectedOption>=val0 && selectedOption<=val2){
//code
}
-
22This is a very fragile solution. If you add a new enum value, you have to survey your entire code base to update such tests. – Marcelo Cantos Aug 04 '13 at 08:46