具有零参数的可变参数宏 [英] Variadic macros with zero arguments

查看:98
本文介绍了具有零参数的可变参数宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理呼叫宏,

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

被调用时

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

将2 3 4 5添加到链接列表(这样做很重载)并调用print,它期望链接列表能够按预期工作,而无论是否有一些不需要参数的调用,

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?

在gcc文档中进行挖掘,我发现在 VA_ARGS 之前添加##会删除,当没有参数但无法嵌套宏时,

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

这会导致CALL未定义错误,如果我分开它可以正常工作的呼叫

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

推荐答案

对于更新后的问题,通过使用辅助宏VA_ARGS 接下来,参数将按预期扩展.

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

这篇关于具有零参数的可变参数宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆