Maximum and minimum values ​​in a C ++ enumeration

Is there a way to find the maximum and minimum enum values ​​in C ++?

+57
c ++ enums
01 Oct '08 at 18:27
source share
5 answers

No, there is no way to find the maximum and minimum specific values ​​of any enum in C ++. When such information is needed, it is often useful to determine the meaning of Last and First. For example,

enum MyPretendEnum { Apples, Oranges, Pears, Bananas, First = Apples, Last = Bananas }; 

You do not need to have named values ​​for each value between First and Last .

+68
01 Oct '08 at 18:30
source share

No, not in standard C ++. You can do it manually:

 enum Name { val0, val1, val2, num_values }; 

num_values will contain the number of values ​​in the enumeration.

+21
01 Oct '08 at 18:32
source share

No. An enumeration in C or C ++ is just a list of constants. There is no more strict structure that would contain such information.

Usually, when I need such information, I include in the enum value max and min something like this:

 enum { eAaa = 1, eBbb, eCccc, eMin = eAaaa, eMax = eCccc } 

See this web page for some examples of how this might be useful: Stupid workaround tricks

+4
01 Oct '08 at 18:39
source share
  enum My_enum { FIRST_VALUE = 0, MY_VALUE1, MY_VALUE2, ... MY_VALUEN, LAST_VALUE }; 

after definition, My_enum :: LAST_VALUE == N + 1

+3
02 Oct '08 at 8:25
source share

you don’t even need what I am doing, just say, for example, if you have:

 enum Name{val0,val1,val2}; 

if you have a switch statement and to check if the last value has been reached, follow these steps:

 if(selectedOption>=val0 && selectedOption<=val2){ //code } 
-2
Oct 23 '10 at 18:35
source share



All Articles