Could one write a function that returns the number of elements in an enum? For example, say I have defined:
enum E {x, y, z};
Then f(E) would return 3.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Nope.
If there were, you wouldn’t see so much code like this:
Note that this code is also a hint for a (nasty) solution — if you add a final “guard” element, and don’t explicitly state the values of the enum fields, then the last “COUNT” element will have the value you’re looking for — this happens because enum count is zero-based: