Getting the highest value of an enum
05:04 06 May 2014

I have C++ code that uses a bitset to store which values of an enum were found in my data structures (it's actually a bit more complex, but that doesn't matter for the question).

This means that when I have an enum like this:

enum Color
   {
     RED
   , GREEN
   , BLUE
   };

I want to define my bitset like this:

std::bitset<3>

Of course I don't want to hard-code the value 3.

In some cases I can simply add a 'terminator' to the enum, like this:

enum Color
   {
     RED
   , GREEN
   , BLUE
   , _COLOR_TERMINATOR
   };

And I can write this:

std::bitset<_COLOR_TERMINATOR>

But I cannot do this in all of my enums. If I would do this on some of my enums, code-checkers (like Lint) would complain that not all enum-values are used in a switch-statement.

Is there a way to get the maximum of the values in an enum without changing something in the enum itself? E.g. something like std::max?

Using Visual Studio 2013 and C++.

c++ visual-studio-2013