以下代码生成一个警告,指出未使用 temp
(这是真的):
#include <cstdio>
int f() { return 5; }
int main() {
if(const int& temp = f()) {
printf("hello!\n");
}
return 0;
}
问题是我需要在不使用 gcc -Wall
和 clang -Weverything
生成警告的情况下执行此操作(我正在实现类似于 Catch 的 SECTION()
内容的功能。
那么有什么方法可以让它静音呢?我尝试使用 __attribute__((unused))
。
全局使用 -Wno-unused-variable
对我来说不是一个选择,因为我正在编写一个仅 header 的库。
最佳答案
#include <cstdio>
int f() { return 5; }
int main()
{
if (const int &temp __attribute__((unused)) = f()) {
printf("hello!\n");
}
return 0;
}
这会消除 GCC 和 clang 的警告。
关于c++ - 在 if 语句中沉默 -Wunused-variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35570835/