0

Using C++11; MingW; Ubuntu 12.04 LTS:

Given:

    enum TMyEnum
    {
        Enum_1, Enum_3, Enum_3
    };

What function will tell me how many members are in TMyEnum, in this case 3?

Vector
  • 10,879
  • 12
  • 61
  • 101
  • 1
    What is your final goal? and note you're not using c++11 enum's there: http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html – ninMonkey Sep 18 '13 at 03:30

2 Answers2

4

One trick is to provide a count item at the end such as:

enum TMyEnum
{
    Enum_1, Enum_2, Enum_3, 
    Enum_4, Another_Enum, 
    Enum_count
};

Then TMyEnum::Enum_count should provide the cardinality of your set of enumerations. Just make sure you add new enumerations before Enum_count. Example:

#include <iostream>

enum TMyEnum
{
    Enum_1, Enum_2, Enum_3, 
    Enum_4, Another_Enum, 
    Enum_count
};

int main() {
    std::cout << TMyEnum::Enum_count << std::endl;   
}

Output

5
0

If you're going to use it with default values, you can have like following :

enum TMyEnum
    {
        Enum_1, Enum_3, Enum_3, No_of_Enums
                                //^^=3
    };
P0W
  • 46,614
  • 9
  • 72
  • 119