使用示波器保护时如何避免警告? [英] How to avoid warning when using scope guard?
本文介绍了使用示波器保护时如何避免警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用愚蠢的作用域保护器,它正在工作,但是会生成警告,指出变量未使用:
I am using folly scope guard, it is working, but it generates a warning saying that the variable is unused:
warning: unused variable ‘g’ [-Wunused-variable]
代码:
folly::ScopeGuard g = folly::makeGuard([&] {close(sock);});
如何避免此类警告?
推荐答案
您可以通过 -Wno-unused-variable
禁用此警告,尽管这样做有些危险(您必须放松所有 realy
You can disable this warnings by -Wno-unused-variable
, though this is a bit dangerous (you loose all realy unused variables).
一种可能的解决方案是实际使用变量,但不执行任何操作。例如,使它无效:
One possible solution is to actually use the variable, but do nothing with it. For example, case it to void:
(void) g;
可以将其制成宏:
#define IGNORE_UNUSED(x) (void) x;
或者,您可以使用提升方法:声明一个不执行任何操作并使用它的模板化函数
Alternatively, you can use the boost aproach: declare a templated function that does nothing and use it
template <typename T>
void ignore_unused (T const &) { }
...
folly::ScopeGuard g = folly::makeGuard([&] {close(sock);});
ignore_unused(g);
这篇关于使用示波器保护时如何避免警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文