Is there a way to find the maximum and minimum defined values of an enum in c++?
In C language, an enum is guaranteed to be of size of an int . There is a compile time option ( -fshort-enums ) to make it as short (This is mainly useful in case the values are not more than 64K). There is no compile time option to increase its size to 64 bit.
Enums can only be ints, not floats in C# and presumably unityScript.
C static code analysis: Values of different "enum" types should not be compared.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With