c++ - 如何抑制源文件中特定宏定义的零参数的 GCC 可变参数宏参数警告

标签 c++ gcc macros

<分区>

我想抑制零参数的 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

在 gcc 中,-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

需要说明的是,除此特定代码片段外,整个程序都应启用警告。这是关于细粒度控制。只需不将 -pedantic 传递给编译器即可在 GCC 中为整个程序禁用警告。

最佳答案

你应该可以使用

来抑制它

#pragma GCC system_header

但这适用于文件的其余部分,您不能只在包含的文件中使用它。因此不提供完美的范围界定,可能需要对头文件进行一些重组/间接包含。

(但坦率地说,如果您不能修复头文件使其符合标准,您不妨将整个头文件视为一个 system_header,这样它就不会产生大多数警告。)

参见 https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html

关于c++ - 如何抑制源文件中特定宏定义的零参数的 GCC 可变参数宏参数警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35587137/

相关文章:

c - 使用两个参数重载宏

c++ - 请帮助......无法编译 "Point Cloud Library"

c++ - 转换到同一类型

c - 可执行文件在编译机上运行,​​而不是在第二台机器上运行

macros - 在Clojure中,使用递归实现宏时如何进行代码模板化

macros - 反引号字符串插值

c++ - 如何用矩形积分法得出这个百分比误差

c++ - 您将如何着手在模板中定义未定义的函数?

Cplex库gcc编译链接错误

c++ - 就嵌入式系统的大小而言,我可以获得多小的完整编译器(如 clang 或 gcc)?