Variadic macros with zero arguments

▼魔方 西西 提交于 2019-11-27 14:36:50

问题


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


回答1:


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



回答2:


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.




回答3:


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.




回答4:


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.



回答5:


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



回答6:


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




回答7:


__VA_OPT__ (c++2a) should be more reliable, eg: from http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0306r2.html

The canonical use case of VA_OPT is for an optional separator:

#define LOG(msg, ...) printf(msg __VA_OPT__(,) __VA_ARGS__)



来源:https://stackoverflow.com/questions/5891221/variadic-macros-with-zero-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!