0

One can access the contents of ... inside a function using stdarg.h:

void fn(int nargs, ...){
  va_list args; va_start(args,nargs);
  i64 arg0 = va_arg(args,i64);
  va_end(args);
}

The only way I know of using __VA_ARGS__ is to pass it to another macro or, eventually, a function. Eg.

#define __fn(...)  fn(number_of_args(__VA_ARGS__), __VA_ARGS__)

but I wonder if it's possible to "unpack" the values of __VA_ARGS__ within a macro itself. Something like va_start(), va_arg(), and va_end(), but for macros.

étale-cohomology
  • 2,098
  • 2
  • 28
  • 33
  • How about something like `#define HEAD(x,...)` and `#define TAIL(x,...) __VA_ARGS__`. Now you extract the head/tail with `HEAD __VA_ARGS__` and `TAIL __VA_AGS__` – camel-cdr Jun 07 '22 at 20:31

1 Answers1

1

if it's possible to "unpack" the values of VA_ARGS within a macro itself.

Yes, you can get the head #define HEAD(x, ...) x of the pack and iterate over the list with EVAL(EVAL(... recursive expansion. See https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms .

I find overloading on the number of macro arguments to produce way more readable error messages than EVAL(EVAL(.. expansions, so I prefer explicitly overloading for each argument.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111