【问题标题】:parameterized warning silencing macro trouble参数化警告静音宏故障
【发布时间】:2018-02-10 11:33:24
【问题描述】:

以下doesn't compile

#define SUPPRESS(w) _Pragma("GCC diagnostic ignored " ## w)

SUPPRESS("-Wuseless-cast")

int main() {
    int a = (int)4;
    return a;
}

这是错误:

error: pasting ""GCC diagnostic ignored "" and ""-Wuseless-cast"" does not give a valid preprocessing token

我怎样才能让它工作?

【问题讨论】:

  • 根据处理phase of translation_Pragma 指令,您也许可以依赖标准的连续文字字符串连接。这意味着如果您有两个文字字符串常量,它们之间只有空格(或 cmets),那么它们将自动连接成一个字符串。如果是这样,那么您不需要预处理器连接运算符。您可能想尝试一下。
  • @Someprogrammerdude 喜欢_Pragma("GCC diagnostic ignored " w)?不起作用。您也可以尝试我提供的在线编译器链接。

标签: c++ gcc c-preprocessor token suppress-warnings


【解决方案1】:

问题是_Pragma 想要像这样转义字符串文字

_Pragma("GCC diagnostic ignored \"-Wuseless-cast\"")

所以诀窍是在SUPPRESS 的调用和_Pragma 的调用之间添加另一层字符串化,如下所示

#define xSUPPRESS(w) _Pragma(#w)
#define SUPPRESS(w) xSUPPRESS(GCC diagnostic ignored w)

SUPPRESS("-Wuseless-cast")

查看here 的实际应用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 2015-02-20
    • 1970-01-01
    相关资源
    最近更新 更多