1

C++ still doesn't have the ability to count the number of items in an enumeration. There are various workarounds, but they are primarily hacks with serious drawbacks:

enum Information
{
   MIN = 0,
   NAME  = MIN,
   ADDRESS,
   BIRTHDATE,
   Count
};
int NumOfElements = Count; // only works items sequential

My understand is that enums are evaluated and substituted at compile time. What I don't understand is why the compiler can't count the number of enum members, like so:

#define INFO_COUNT = #COUNT_MEMBERS(Information)

int NumOfElement = INFO_COUNT; // NumOfElements is 5

Is there something I am missing? Is there a gotcha lurking somewhere?

ATL_DEV
  • 9,256
  • 11
  • 60
  • 102
  • Probably for the same [reasons C++ doesn't have reflection in general](https://stackoverflow.com/q/359237/10077). – Fred Larson Jul 07 '20 at 18:45
  • @FredLarson: Most of those arguments relate to runtime cost, which wouldn't be an issue here, or language complexity, which would seem minimal. – Nate Eldredge Jul 07 '20 at 18:49
  • 3
    What good would this feature be? – Dialecticus Jul 07 '20 at 18:49
  • C++ also has no feature which will tell you how many members a `struct` has. Just sayin'. – rici Jul 07 '20 at 19:12
  • @NateEldredge: This particular feature probably wouldn't add any runtime cost, but I think it comes down to "It's a lot of work to add, and the C++ committee is fairly conservative, and don't spend time on radical new features unless they're sure it'll pay off." What change would have to be made to the language to allow this? Some new variant of `sizeof`? Eww. Some special `length` member of an `enum` type? Not likely. It's just not worth the trouble. – Fred Larson Jul 07 '20 at 19:14
  • @Dialecticus In lieu of not having the ability to iterate over enums as in other languages, which requires performance impacting runtime support, such a feature increases code reliability. If the number of enum items change, for instance, a piece of code that depends on the number of items can fail. Such a feature can save your foot with no runtime cost and encourages best practices. – ATL_DEV Jul 08 '20 at 16:56
  • But you still won't be able to iterate over them, even if you had the count, because enums can have arbitrary values. – Dialecticus Jul 08 '20 at 17:04
  • @Dialecticus That's fine, but such a feature still helps a lot. If you had a enum of command ids, a count, for instance, lets you know how many commands you have. – ATL_DEV Jul 24 '20 at 12:57
  • Yeah, and then what? How do you use this information in a useful and meaningful way? – Dialecticus Jul 24 '20 at 17:07
  • @Dialecticus Assume you have an ordered set of 5 colors as an enum. You want to calculate some value based on the number of colors. You could use the literal 5 or a constant, but neither are good programming styles. If later on, you add a 6th color to you will have to replace the literal or reassign the constant. If you overlook making this change, it can lead to a bug. – ATL_DEV Aug 07 '20 at 15:23

0 Answers0