我想抑制 GCC 可变参数宏参数对零参数的警告,例如通过以下方式生成:
// for illustration purposes only:
int foo(int i) { return 0; };
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
FOO(1);
^ warning: ISO C++11 requires at least one argument for the "..." in a variadic macro
使用 GCC 5.3.0 时源文件中的特定宏定义。
在 clang 中,这是按如下方式完成的:
// ... large file
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
#pragma clang diagnostic pop
// ... large file
// not necessary on the same file
FOO(1); // doesnt trigger the warning
在海湾合作委员会它看起来像-pedantic
是一种神奇的警告类型,因此以下内容不起作用:
// ... large file
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#define FOO(A, ...) foo(A, ##__VA_ARGS__)
#pragma GCC diagnostic pop
// ... large file
需要明确的是,除了这个特定的代码片段之外,应该在整个程序中启用警告。这是关于细粒度的控制。只需不通过即可在整个程序中禁用 GCC 中的警告-pedantic
给编译器。