55 points · by david2ndaccount · 1mo ago · 41 comments
One of those is the two-stage CAT, another one is the NARG the article talks about.
__COUNTER__ is going to be added to C29, the next version of the standard, maybe there's hope for __VA_COUNT__ and __CAT__ too!
Otherwise, it's probably a good idea to pretend that the only preprocessing directive is #include. With constexpr, even #define is obsolescent.
Getting rid of something because someone sometimes abuses it, is short-sighted.
As an aside, clangd can dereference a macro in place to make debugging easy.
> #define T(x, ...) printf(#x " = %d\n", x); __VA_TAIL__()
> T(1, 2, 3, 4, x, y, z)
You can play with it in the proposal author's ellipsis[2] and my slimcc[3], it's simple enough to implement (only about 30 lines of C) so I kept it in despite not seeing signs of it getting accepted.
[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3307.htm
(I am not talking about metalang99 or Cello like stuff, which exploits the Turing tarpit of the currently specified preprocessor but rather tasteful additions to the preprocessor itself).
// Create a stack-allocated argv from N args gcc, clang, tcc, mingw, msvc, etc
// argc is always correct, for portability argv has a dummy element when argc==0
// This avoids problematic 0-length compound literal arrays.
#define _ARG_CV(type, ...) _ARG_CV1(((type []){0, __VA_ARGS__}))
#define _ARG_CV1(array) _ALEN(array) - 1, array + (_ALEN(array) > 1)
#define _ALEN(array) sizeof array / sizeof *array
Then it's just: #define sum(...) sum(_ARG_CV(int, __VA_ARGS__))Your version expands `sum(1, 2, 3, 4, 5, 6)` to:
sum(sizeof ((int []){0, 1, 2, 3, 4, 5, 6}) / sizeof *((int []){0, 1, 2, 3, 4, 5, 6}) - 1, ((int []){0, 1, 2, 3, 4, 5, 6}) + (sizeof ((int []){0, 1, 2, 3, 4, 5, 6}) / sizeof *((int []){0, 1, 2, 3, 4, 5, 6}) > 1))
whereas with __VA_COUNT__ it is just: sum(6, (int[]){1, 2, 3, 4, 5, 6})Well, I keep hearing that Lisp is, but I haven't put much effort into it. (I'd need a Lisp that runs in the browser, for starters!) Maybe I should take another look...
Or maybe I wish the VM was programmable. "Warn me when this type of operation takes place", or "Print the name of every function before it is invoked", etc. I end up just copy-pasting my own temporary statements all over the program.
We would be in a very different world if Netscape’s exec team had let Brendan Eich just use scheme as he had planned to.
__map(STR, X)
Should this produce "X" (like STR(X) does), or "42" (if __map expands its arguments first)?