##__VA_ARGS__ 是什么意思?

Moh*_*cro 10 c++ macros preprocessor variadic-macros

我想知道##这个宏定义有什么作用:

#define debug(M, ...) fprintf(stderr,M "\n",##__VA_ARGS __)
Run Code Online (Sandbox Code Playgroud)

我用谷歌搜索了一个答案,我想出了以下内容。

##如果没有给宏提供变量参数,将删除逗号。所以,如果宏是这样调用的

debug("message");
Run Code Online (Sandbox Code Playgroud)

没有引号,它被扩展为

fprintf(stderr,"message");
Run Code Online (Sandbox Code Playgroud)

不是

fprintf(stderr,"message",);
Run Code Online (Sandbox Code Playgroud)

为什么要去掉逗号?

Tra*_*s3r 10

这是 gcc 引入的一种不可移植的语法,专门用于处理这种根本不传递参数的极端情况。如果没有##,它会抱怨尾随逗号是语法错误。

https://gcc.gnu.org/onlinedocs/gcc/Variadic-Macros.html

__VA_OPT__为此目的引入的 C++20 :https : //en.cppreference.com/w/cpp/preprocessor/replace

  • 所以它与 ## 串联的一般行为无关,对吗? (6认同)