固定“ -Wunused-parameter”警告,取决于预处理器条件

问题描述 投票:0回答:1

当变量的使用取决于预处理程序指令(#if,#else ...)条件时,如何解决“ -Wunused-parameter”警告。

void foo(std::string& color)
{
#ifdef PRINT
    printf("Printing color: ", color);
#endif
}

我已经看到(void)的用法,如:

void foo(std::string& color)
{
#ifdef PRINT
    printf("Printing color: ", color);
#else
    (void)color;
#endif
}

这是正确的方法吗?

[[注意]:这里提到的示例只是我的实际用例的一个很低的说明。

c++11 c++14 c-preprocessor compiler-warnings gcc-warning
1个回答
0
投票

使用(void) variable是避免未使用的变量警告的简单方法,例如:在assert左右。另一种解决方案是更改打印的定义方式:

#if defined PRINT
#define PRINTF(str) printf("%s\n", str)
#else
#define PRINTF(str) (void)str;
#endif
© www.soinside.com 2019 - 2024. All rights reserved.