使用护目镜时如何避免警告?

Ale*_*lex 4 c++ scopeguard c++11 folly

我正在使用愚蠢的范围保护,它正在工作,但它会生成一个警告,说该变量未使用:

warning: unused variable ‘g’ [-Wunused-variable]
Run Code Online (Sandbox Code Playgroud)

代码:

folly::ScopeGuard g = folly::makeGuard([&] {close(sock);});
Run Code Online (Sandbox Code Playgroud)

如何避免这种警告?

Bar*_*rry 5

您可以将变量标记为未使用:

folly::ScopeGuard g [[gnu::unused]] = folly::makeGuard([&] {close(sock);});
Run Code Online (Sandbox Code Playgroud)

或者将其转为无效:

folly::ScopeGuard g = folly::makeGuard([&] {close(sock);});
(void)g;
Run Code Online (Sandbox Code Playgroud)

既不是伟大的,imo,但至少这可以让你保持警告.