Is it possible to determine the cardinality of a c++ enum class:
enum class Example { A, B, C, D, E };
I tried to use si
constexpr auto TEST_START_LINE = __LINE__;
enum class TEST { // Subtract extra lines from TEST_SIZE if an entry takes more than one
ONE = 7
, TWO = 6
, THREE = 9
};
constexpr auto TEST_SIZE = __LINE__ - TEST_START_LINE - 3;
This is derived from UglyCoder's answer but improves it in three ways.
BEGIN and SIZE) (Cameron's answer also has this problem.)
It retains UglyCoder's advantage over Cameron's answer that enumerators can be assigned arbitrary values.
A problem (shared with UglyCoder but not with Cameron) is that it makes newlines and comments significant ... which is unexpected. So someone could add an entry with whitespace or a comment without adjusting TEST_SIZE's calculation.