使用范围防护时如何避免警告
How to avoid warning when using scope guard?
我正在使用愚蠢的作用域保护,它正在工作,但它生成一个警告,指出该变量未使用:
warning: unused variable ‘g’ [-Wunused-variable]
代码:
folly::ScopeGuard g = folly::makeGuard([&] {close(sock);});
如何避免此类警告?
您可以将变量标记为未使用:
folly::ScopeGuard g [[gnu::unused]] = folly::makeGuard([&] {close(sock);});
或者将其转换为无效:
folly::ScopeGuard g = folly::makeGuard([&] {close(sock);});
(void)g;
两者都不是很好,imo,但至少这可以让你保持警告。
您可以通过
-Wno-unused-variable
禁用此警告,尽管这有点危险(您丢失了所有实际未使用的变量)。
一种可能的解决方案是实际使用该变量,但不对它执行任何操作。例如,将其作废:
(void) g;
可以制作成宏:
#define IGNORE_UNUSED(x) (void) x;
或者,您可以使用 boost aproach:声明一个不执行任何操作的模板化函数并使用它
template <typename T>
void ignore_unused (T const &) { }
...
folly::ScopeGuard g = folly::makeGuard([&] {close(sock);});
ignore_unused(g);
相关文章:
- 如何避免浮点值的q警告
- 如何避免NRVO的"pessimizing-move"警告?
- c++:需要正确的语法以避免 MISRA 14-6-1 警告.具有依赖基类的类模板
- 有没有办法在初始化字符串时避免来自 clang-tidy(fuchsia-default-arguments)的警告?
- 避免对仅在源文件中定义的类发出弱 vtable 警告
- 静态强制转换以避免 IDE 警告?
- 移植到 64 位时如何避免size_t int 强制转换警告?
- 如何避免"'=': truncation of constant value"的警告消息?
- 通常如何避免重定义警告?
- 避免在 openmp 并行块中使用预编译器语句出现未使用的变量警告
- GCC:通过显式 memcPY 避免严格混叠违规警告
- 如何避免警告C4244:从'ULONGLONG'转换为"双倍",可能丢失数据
- 是否有任何方法可以避免警告/错误模板实例化回溯
- 使用范围防护时如何避免警告
- Qt避免警告QProcess:在进程仍在运行时被销毁(助手)
- 避免/警告C++成员的自我初始化
- 具有多重继承的消息映射MFC:如何避免警告C4407和运行时崩溃
- Qt避免警告"QProcess:在进程仍在运行时被破坏
- 如何正确组合来自基类和派生类的函数以避免警告
- 避免在 gperf 输出文件中避免"警告:声明 UserSuppliedStruct 不声明任何内容"