Question

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

Was it helpful?

Solution

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__)))

OTHER TIPS

If you're using gcc/g++ there is a way:

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

From the fine manual:

[...] if the variable arguments are omitted or empty, the `##' operator causes the preprocessor to remove the comma before it.

So gcc has an extension/hack specifically for the problem you are facing.

If you are using GCC, it has an extension to swallow up the comma preceding the __VA_ARGS__. See: http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html.

A common theme in these answers is that we need a GCC specific hack. One way is to use the token-paste ##__VAR_ARGS__, but pasted arguments are not macro expanded, which means that macros cannot be nested. But if you are going to do something GCC specific anyway, then why not use the old-fashioned GCC extension:

 #define VARARG_FOO(ZeroOrMoreArgs...) \
     printf("VARARG_FOO: " ZeroOrMoreArgs)

ZeroOrMoreArgs is simply replaced by all the arguments (if any), commas and all. This includes recursive macro expansion.

  • then VARARG_FOO() expands to printf("VARARG_FOO: ")
  • and VARARG_FOO("I iz %d", 42) expands to printf("VARARGFOO: " "I iz %d", 42)

Finally

 #define NEST_ME "I tawt I taw a puddy tat"
 VARARG_FOO("The evil one says %s", NEST_ME); 

will expand to

 printf("VARARG_FOO: " "The evil one says %s", "I tawt I taw a puddy tat");

Pros:

  • You can nest macro invocations, while having zero-or more aguments.

Cons:

  • The ##__VA_ARGS__ hack might be harmless in standard C programs in the case where they always have at least one comma. (I haven't thought about whether this is true or not).
  • According to @ScootMoonen the ##__VA_ARGS__ hack is an undocumented extension to MSVC.

Unfortunately this cannot be done. You will need to define a separate macro to do this call.

As you end up with invalid arguments when VA_ARGS is substituted with nothing you end up with a floating ,

#define CALL0(f) FN(f)->call((ref(new LinkedList())))

Simply make f part of the ..., and use a separate macro to extract the first argument where you need f.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top