eliminating unreferenced parameter warnings

A

Alex Fraser

Kevin Bracey said:
In message <[email protected]>


Bloomin' well should be though. Just the sort of simple improvement that
C++ introduced that is worth adopting.

I agree. Seems useful to me.

Is there any particular problem (eg because of language grammar)? I can't
see one. I'm no C++ expert, but if there's no real problem there I would
expect the same would apply to C.

Has it been suggested and rejected? If so, does anyone know why?

Alex
 
C

Chris Croughton

To summarize the discussion:
Possible methods of eliminating the warning are

1. ((void)(p))
2. ((p) = (p))
3. #pragma unused p
4. GNU C's __attribute__((unused))
5. /*ARGUSED*/

It is not known which is supported by the largest proportion of compilers.
None of these methods are 100% portable. For this reason some believe that
it is not worth doing.

They are all portable in that they won't produce errors (they may
increase the number of warnings, though).
A macro could be used to switch between methods 1, 2 and nothing at all, or
method 4 and nothing at all. Either don't attempt to eliminate the warning,
or find the method or combination of methods that works best for your
particular set of compilers.

Bearing in mind that none of them may work. On at least one compiler
I've used certain warnings cannot be switched off without suppressing
all warnings (or redirecting output to /dev/null, with the same effect).

I favour using #define UNUSED(var), then (a) that can be customised for
each compiler and (b) if all of the methods fail and produce more
warnings it can be defined as nothing and you're no worse off, the code
will still be self-documenting that the parameter is unused.

Chris C
 
T

Tim Rentsch

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top