4

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.

learnvst
  • 15,455
  • 16
  • 74
  • 121

4 Answers4

6

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.

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
3

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);
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
2

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.

Community
  • 1
  • 1
George Hilliard
  • 15,402
  • 9
  • 58
  • 96
  • You say "Lambdas are just syntactic sugar for functors." I used to think this, but I'm not so sure any more. Consider: `auto f = [](int x){return x+1;}; for (int i = 0; i < n; ++i) f = [f](int x) { return f(f(x)); }`. The translation to functor is not straightforward (I guess the capture-by-value of f would need to be implemented as a unique_ptr or equivalent, rather than just a plain member variable). – Don Hatch Oct 28 '19 at 19:08
  • I'm confused by your first paragraph. It sounds like you are saying "lambdas are pretty much like functors; therefore an unused lambda will get a warning just like unused functors do". But unused functors do *not* get a warning. See https://stackoverflow.com/questions/5302848/g-does-not-show-a-unused-warning – Don Hatch Oct 28 '19 at 19:12
0

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.

woolstar
  • 5,063
  • 20
  • 31