logoalt Hacker News

spacechild1yesterday at 8:42 PM2 repliesview on HN

The implementation doesn't look too bad, but the usage is terrible:

  #define E_LIST(X) \
      X(V0) X(V1) X(V2) X(V3)

  DEFINE_ENUM(E, E_LIST)
That's not how I want to declare my enums...

Replies

ueckertoday at 6:39 AM

In practice it is written like like this:

  #define MY_ENUM(x) x(MY_A) x(MY_B) x(MY_C)
  enum my_enum { MY_ENUM(ENUM_ENTRIES) };

  static const char *my_enum_names[] = { MY_ENUM(ENUM_NAMES) };
but one could also make it even more compact if one cared.
show 1 reply
SuperV1234yesterday at 8:56 PM

To be honest there are ways to make that much nicer. I believe that if you use recursive macros using the VA_OPT feature, you should be able to provide enumerators directly to define enum as a list.

The underlying machinery implementation is going to be much uglier and complex, though.

See https://www.scs.stanford.edu/~dm/blog/va-opt.html

show 1 reply