Alex Rivera | Logout

Max and min values in a C++ enum

Asked 2008-10-01T18:27:26.500
95

Is there a way to find the maximum and minimum defined values of an enum in c++?

Edit
Report

2 Answers

114

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.

answered 2008-10-01T18:30:33.747
-7

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
}
answered 2010-10-23T18:35:56.323

Your Answer