Learn without tiring; teach without growing weary.
Counting the Elements in a C++ Enum
Counting the Elements in a C++ Enum I recently had a special requirement to determine the number of elements in an enum type, and in the process I learned the following clever tricks. Explanation These techniques originated with a question on Stack Overflow: “Can the number of elements in a C++ enum class be determined?” The Craftsperson Basic The simplest approach is: 1 enum class Example { A, B, C, D, E, Count }; Since enumerator values increase from 0 by default, the number of elements can be obtained with static_cast<int>(Example::Count). ...