Is it possible to determine the number of elements

2019-03-09 06:51发布

Is it possible to determine the cardinality of a c++ enum class:

enum class Example { A, B, C, D, E };

I tried to use sizeof, however, it returns the size of an enum element.

sizeof(Example); // Returns 4 (on my architecture)

Is there a standard way to get the cardinality (5 in my example) ?

7条回答
Ridiculous、
2楼-- · 2019-03-09 07:27

You can also consider static_cast<int>(Example::E) + 1 which eliminates the extra element.

查看更多
登录 后发表回答