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?
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?
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;
}
5
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
};