3 ms·
Another win for X macros and for C style in general, though the author didn’t declare it as such.
by king_geedorah 5mo ago
Another win for X macros and for C style in general, though the author didn’t declare it as such.
- SuperV1234 5mo agoAuthor here. It isn't a clear "win" at all, there are tradeoffs to each approach.
- spacechild1 5mo agoThe downside is, of course, that it's ugly and very awkward to use. That's the essence of C++: you're basically trading ergonomics for compile times.
- uecker 5mo agoAre X macros awkward? I find them very straightforward and clear.
- spacechild1 5mo agoThe 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...
- SuperV1234 5mo agoTo 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 https://www.scs.stanford.edu/~dm/blog/va-opt.html
- spacechild1 5mo agoOh, I didn't know about __VA_OPT__(), thanks for that! That looks much nicer indeed, but I still vastly prefer the other solutions, simply because I can just declare regular enums.
- uecker 5mo agoIn 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.
- spacechild1 5mo agoThat doesn't look any better. Yes, xmacros have the best compile times, but you can't possibly argue that they are elegant to use compared to the alternatives.
- uecker 5mo agoIt looks better to me than the other macro solution as it is more transparent what is done compared to DEFINE_ENUM. But I agree it is not as succinct as C++'s reflection syntax.
- spacechild1 5mo ago> It looks better to me than the other macro solution as it is more transparent what is done compared to DEFINE_ENUM. Fair enough.
- psyclobe 5mo agoThey are gross but... effective so shrug
- psyclobe 5mo agoPretty much. Was hoping it would've been a 'reflection slam dunk' but no... same 'ol same 'ol.