Reputation: 7242
There are many different techniques to disable unused variable warnings. Some with void, same with attribute. They all though assume that you know that it is a variable.
I have a case of macro that changes its behavior from release and debug, say:
#if NDEBUG
#define MACRO(X)
#else
#define MACRO(X) do_something(X)
#endif
Code like:
void foo(int a) {
MACRO(a);
}
may result in warning in release. I would like to change this to:
#if NDEBUG
#define MACRO(X) UNUSED(X)
#else
#define MACRO(X) do_something(X)
#endif
So the question is what I should define UNUSED
to, when the task is complicated from the fact that the argument to MACRO
is not limited to variable, but it could also be a function call or any other expression. Or
MACRO(5);
MACRO(a+b);
MACRO(foo());
are also valid uses of MACRO
Upvotes: 1
Views: 707
Reputation: 1
You might have:
#define UNUSED(X) (false && ((X),true))
and-then with comma-operator (for typing reasons)
Or
#define UNUSED(X) do{if (false) (void)(X);}while(0)
always false conditional
Upvotes: 1
Reputation: 14390
Here's an example I like:
#ifdef NDEBUG
#define MACRO(X) ((void)sizeof((X), 0))
#else
#define MACRO(X) do_something((X))
#endif
The names you use in the macro argument end up being used, but in an unevaluated context and then that gets cast to void
which prevents a different warning.
Upvotes: 1