//c++03
enum Something
{
S1 = 0,
S2,
SCOUNT
};
int arr[SCOUNT];
//c++11
enum class Something
{
S1 = 0,
S2,
SCOUNT
};
int arr[(int) Something::SCOUNT];
How can I use enums in this case without casting count of enum to int?
You can't... But you can remove the
classfromenum classThe
classkeyword means that you can't implicitly convert between thatenumandintRemoving theclasskeyword means yourenumwill work in the same way as in previous versions of C++. You won't need the cast, but you loose the safety ofstrongly typed enumsby allowing implicit casts to integral values.You can read more about
strongly typed enumshereThe other option is to cast it, like you are doing. But you should avoid the old
C-style castand usestatic_cast<>instead as this has better type safety and is more explicit.