//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
class
fromenum class
The
class
keyword means that you can't implicitly convert between thatenum
andint
Removing theclass
keyword means yourenum
will work in the same way as in previous versions of C++. You won't need the cast, but you loose the safety ofstrongly typed enums
by allowing implicit casts to integral values.You can read more about
strongly typed enums
hereThe other option is to cast it, like you are doing. But you should avoid the old
C-style cast
and usestatic_cast<>
instead as this has better type safety and is more explicit.