Programmatically infer the size of an enumeration regardless of the value of its elements?

Is there a way, possibly requiring C ++ 11/14, to infer the number of elements in an enumeration, regardless of the values ​​of the enumeration elements themselves?

Consider an enumeration of type

enum { Val1 = 1, Val2 = 2, Val3 = 4 }

for which the answer will be 3. I know that this is a wrinkle where I could, for example Val3 = Val1, but it can be ignored for my use.

I have seen quite a few similar questions, both here and elsewhere, but I still have to find the right answer. If not at all.

Usually the suggested solution is to introduce a LAST element, but that will only give me the next higher enum value (using the example above, it will be 5), which would be useless to me.

+4
source share
1 answer

If at all possible.

No.

Even with the new C ++ 17, there is no suggestion that I know that this will allow you to achieve this.

Usually the suggested solution is to introduce a LAST element, but that will only give me the next higher enum value (using the example above, it will be 5), which would be useless to me.

Then you will have to repeat your problem.

+2
source

Source: https://habr.com/ru/post/1690311/


All Articles