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]
The code:
folly::ScopeGuard g = folly::makeGuard([&] {close(sock);});
How to avoid such warning?
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:
which can be made into a macro:
Alternatively, you can use the boost aproach: declare a templated function that does nothing and use it