How to find out the number of enum members?

Let the class set enum : enum a{a1,a2,a3,a4,a5}; , and in another class there is a method that iterates over vector values ​​by members of a with the help of for(int...) . But if I then increase the number of members a , then I will have to change this in the method. And I would like to set the size of the vector to the size of a .

Or find out which element is the last.

    1 answer 1

    Find out the number of constants in the listing is not possible. If you are using a loop iteration, then it may be sufficient to simply set a constant talking about the maximum size. Well, or if you still want to have named constants, then you can set the last element in enum as aMax :

     enum A { a1, a2, a3 ... , aMax }; 

    The cycle in this case will look like this:

     for( A v = a1; v < aMax; ++v ) { ... } 

    I add that this whole scheme works only if there are no gaps in the enumeration values. For example:

     enum A { a1, a2 = 100, a3 ... , aMax }; 

    will give rise to the problem of the absence of values ​​in the range [1..99]. Those. the loop should somehow take this into account, and not go in the usual increment.

    • Thank you, but something is fixing me on a function that calculates the length) - Dmitry