Is there a way to call a macro on every argument in __VA_ARGS__?

140 Views Asked by At

for example:

#define M(x) do { x++; f(x); } while(0)

#define M_ALL(...) do { ??? } while(0)

Is there a way to call M on each of the arguments of M_ALL?

such that doing this:

M_ALL(x, y, z)

would expand into:

do { 
    do { x++; f(x); } while(0); 
    do { y++; f(y); } while(0); 
    do { z++; f(z); } while(0); 
} while(0)
1

There are 1 best solutions below

5
Michael Walsh Pedersen On

What about using X macro ? (https://en.wikipedia.org/wiki/X_macro) . E.g:

#define LIST(DO) \
    DO(x) \
    DO(y) \
    DO(z)

#define M(x) do { x++; f(x); } while(0);

#define M_ALL(list) do { list( M ) } while(0) 

M_ALL(LIST);