John Fisher said:
void f(int p)
{
}
Many (most?) compilers will report that p is unreferenced here. This may not
be a problem as f may have to match some common prototype. Typically
pointers to functions are involved.
For a long time I have used
#define UNUSED(p) ((void)(p))
so that
void f(int p)
{
UNUSED(p);
}
will not cause a warning on all compilers I have tried it with. However a
third party code vendor uses
#define UNUSED(p) { (p) = (p); }
I believe this code has been ported to many different compilers. On my
compiler this will produce a useless assignment warning, so for that reason
I obviously prefer my approach.
What's your favourite trick and why? Which approach do you think is more
likely to prevent a warning? Has anyone done a survey?
1. If possible encapsulate in a macro;
2. My personal preference is to call the macro IGNORE;
2a. (the macro name UNUSED is misleading because it does not guarantee
that the variable is unused; IGNORE references the variable but
"ignores" it);
3. Obviously different definitions might be needed on different
platforms, but unless this definition fails I would normally use
#define IGNORE(v) ((void) &(v))
Using '&' means IGNORE can also be used with volatile variables
without causing access. Not that parameters are normally volatile,
but writing in good style is a good habit to cultivate; and, there
are times when you might want to use IGNORE on a local variable rather
than a parameter.
Of course, the '&' can cause problems if used on a 'register'
variable; IMO the positive consequences of that -- namely,
discouraging the use of 'register' -- outweigh the negative
consequences.