C++ – Variadic macros with zero arguments

cc-preprocessorgccvariadic-macros

I am working on a call macro,

#define CALL(f,...) FN(f)->call((ref(new LinkedList()), __VA_ARGS__))

which when called,

CALL(print,2,3,4,5);

adds 2 3 4 5 to the linked list (, is overloaded to do so) and calls print which expects a linked list which works as expected how ever there are some calls which do not require arguments,

CALL(HeapSize);

It still takes a linked list but an empty one, above does not work, I am trying to come up with a macro that woud work with either style?

EDIT: Digging throug gcc docs I found that adding ## before VA_ARGS removes the , when there are no arguments but with that I can not nest macros,

CALL(print,CALL(HeadSize));

this causes CALL not defined error how ever if I separate the the calls it works

Best Answer

As for the updated question, by the use of auxiliary macro VA_ARGS like the following, the arguments will be expanded as expected.

#define VA_ARGS(...) , ##__VA_ARGS__
#define CALL(f,...) FN(f)->call((ref(new LinkedList()) VA_ARGS(__VA_ARGS__)))
Related Topic