当变量的使用取决于预处理程序指令(#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
}
这是正确的方法吗?
[[注意]:这里提到的示例只是我的实际用例的一个很低的说明。
使用(void) variable
是避免未使用的变量警告的简单方法,例如:在assert
左右。另一种解决方案是更改打印的定义方式:
#if defined PRINT
#define PRINTF(str) printf("%s\n", str)
#else
#define PRINTF(str) (void)str;
#endif