1

Is it possible to define a variadic C macro that expand to give every other argument? In other words,

EVEN(A,a,B,b)

expands to

a, b

and

EVEN(A,a,B,b,C,c)

expands to

a, b, c

etc.

I can define one that takes a pre-determined number of arguments

#define EVEN(a1,a2,a3,a4,a5,a6) a2, a4, a6

but how do you define a macro that takes an arbitrary number of arguments?

#define EVEN(args...)        ???
Epsilon
  • 1,016
  • 1
  • 6
  • 15
  • https://stackoverflow.com/questions/679979/how-to-make-a-variadic-macro-variable-number-of-arguments duplicate? – Ivan C Dec 09 '20 at 01:53
  • 2
    @IvanC — I don't think that explains how to do differential argument handling necessary to achieve the `EVEN` macro. Yes, `__VA_ARGS__` figures in the solution. You might need the GCC extension `## __VA_ARGS__` even. But it doesn't explain the rest. – Jonathan Leffler Dec 09 '20 at 02:09
  • Some of the things one can do with the C preprocessor are pretty surprising, given its limitations, but I'm having trouble seeing how this would be one of them. – John Bollinger Dec 09 '20 at 02:15
  • 1
    You would need a recursive macro for this, which is not possible. – dbush Dec 09 '20 at 02:20

0 Answers0