How to avoid warning when using area protection?

I use crazy coverage control, it works, but it generates a warning that the variable is not used:

warning: unused variable ‘g’ [-Wunused-variable]

The code:

folly::ScopeGuard g = folly::makeGuard([&] {close(sock);});

How to avoid such a warning?

+4
source share
2 answers

You can disable these warnings with -Wno-unused-variable, although this is a bit dangerous (you will lose all really unused variables).

One possible solution is to actually use this variable, but do nothing with it. For example, if it is not valid:

(void) g;

which can be done in a macro:

#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);
+4

:

folly::ScopeGuard g [[gnu::unused]] = folly::makeGuard([&] {close(sock);});

void:

folly::ScopeGuard g = folly::makeGuard([&] {close(sock);});
(void)g;

, imo, , , .

+5

All Articles