简体   繁体   中英

Selectively suppress “unused variable” warnings for unused lambdas

Is there any way to suppress "unused variable" warnings for a specific file, namespace, or specific variable?

I ask because I have a namespace containing a big list of lambda functions. Some are not used now, but might be used in time. If these were regular free functions, I would not be warned if some were unused. However, because they are lambdas, I end up with a stack of compiler warnings.

I do not want to use a compiler flag to remove all of this type of warning, as normally, it is very useful to have the compiler catch unused variables. However, a stack of warnings about unused utility functions adds noise to otherwise useful information.

There are two approaches that come to mind. First of all, most build environments enable per-source compiler flags, so you should be able to turn off that warning just for the single file where all those lambdas are defined.

Then there is a general approach to silence such warnings for single variables: use it, but not really do anything with it. On some compilers this can be achieved with a simple cast to void:

auto x = /* ... */;
(void) x;

But more effective is defining a simple function that makes it look (for the compiler) as if the variable was used:

template <class T>
void ignore_unused(T&) {} 

//later...
auto x = /* ... */;
ignore_unused(x);

Note the parameter has no name, so the compiler will not complain about that one to be unused.

The idea is pretty common: do something with the variable that effectively makes no operation but silences the static analyzer that emits the "unused variable" warning.

Boost has a similar function, boost::ignore_unused_variable_warning()

For more information, see Herb Sutter's blog .

In C++ you can static_cast anything to void .

What is the use of such a cast if it does not produce any side effects or a value one might ask?

Precisely to tell the compiler that an object is "used" in a portable way.

So,

auto x =  /*...*/;
static_cast<void>(x);

Lambdas are just syntactic sugar for functors . Functors are a type of object (one that has operator() overloaded). So the compiler will warn if that variable (object) goes unused.

I recommend the block-comment method for hushing the compiler ;). Other than that, there's not much you can do to selectively and cleanly silence the compiler in the general case.

Note that if you have a more specific case, such as passing arguments in a function, you can make the argument nameless if you do not use it. Another thing you could do is put a (void)var reference somewhere in your code (although this is cheating; now you've referenced it!). Lastly, if your compiler supports it (the almighty GCC does), you might try using the __attribute__((unused)) preprocessor directive (use [[gnu::unused]] for Clang).

Depending on your situation, these suggestions may or may not help.

How about hiding the lambdas inside of generators. That way they don't even get created unless they are used. So instead of:

auto add_one= [](int x){ return x + 1 } ;

Use:

std::function<int(int)> gen_addone(void)
{
    static auto l_= [](int x){ return x + 1 ; } ;
    return l_ ;
}   

Sorry, you'll have to wait till c++1y for automatic return type deduction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM