Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]

The code:

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

How to avoid such warning?

like image 370
Alex Avatar asked Jan 07 '23 14:01

Alex


1 Answers

You can just label the variable as being unused:

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

Or cast it to void:

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

Neither is great, imo, but at least this lets you keep the warnings.

like image 120
Barry Avatar answered Jan 14 '23 14:01

Barry