我正在使用愚蠢的范围保护,它正在工作,但它会生成一个警告,指出该变量未使用:
warning: unused variable ‘g’ [-Wunused-variable]
编码:
folly::ScopeGuard g = folly::makeGuard([&] {close(sock);});
如何避免这样的警告?
我正在使用愚蠢的范围保护,它正在工作,但它会生成一个警告,指出该变量未使用:
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方法:声明一个什么都不做的模板函数并使用它
template <typename T>
void ignore_unused (T const &) { }
...
folly::ScopeGuard g = folly::makeGuard([&] {close(sock);});
ignore_unused(g);