##__VA_ARGS__ 是什么意思?

21

我想知道在这个宏定义中,##是什么意思:

#define debug(M, ...) fprintf(stderr,M "\n",##__VA_ARGS __)

我谷歌搜索了一个答案,我找到了以下内容。

##在没有变量参数的情况下将删除逗号。 因此,如果像这样调用宏

debug("message");

没有引号时,它会被扩展为

fprintf(stderr,"message");
不。
fprintf(stderr,"message",);

为什么要去掉逗号?

2个回答

22

6
那么这与##的一般行为、连接无关,对吗? - Mohammed Micro
是的,这只是滥用了那个运算符。 - Trass3r
@Maverick 他不是刚说“从C++20开始”吗? :) - Johann Gerell
1
@JohannGerell 是的,我只是想明确并强调一下,即使是我拥有的 Visual Studio 2017 的专业版也不支持我最初随意想到的这个宏。 - Maverick
1
VS2017不支持C++20哈哈。希望你运气好的话,VS2020会支持。有关文档,请参见https://en.cppreference.com/w/cpp/preprocessor/replace。 - Carlo Wood
显示剩余3条评论

2
来自于https://en.cppreference.com/w/cpp/preprocessor/replace
Note: some compilers offer an extension that allows ## to appear after a
comma and before __VA_ARGS__, in which case the ## does nothing when the 
variable arguments are present, but removes the comma when the variable
arguments are not present: this makes it possible to define macros such as
fprintf (stderr, format, ##__VA_ARGS__)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接