For instance:
Bool NullFunc(const struct timespec *when, const char *who)
{
return TRUE;
}
In C++ I was able to put a /*...*/
comment around the parameters. But not in C of course, where it gives me the error error: parameter name omitted
.
You can use gcc/clang's unused attribute, however I use these macros in a header to avoid having gcc specific attributes all over the source, also having
__attribute__
everywhere is a bit verbose/ugly.Then you can do...
I prefer this because you get an error if you try use
bar
in the code anywhere so you can't leave the attribute in by mistake.and for functions...
Note 1):
As far as I know, MSVC doesn't have an equivalent to
__attribute__((__unused__))
.Note 2):
The
UNUSED
macro won't work for arguments which contain parenthesis,so if you have an argument like
float (*coords)[3]
you can't do,float UNUSED((*coords)[3])
orfloat (*UNUSED(coords))[3]
, This is the only downside to theUNUSED
macro I found so far, in these cases I fall back to(void)coords;
A gcc/g++ specific way to suppress the unused parameter warning for a block of source code is to enclose it with the following pragma statements:
In MSVC to suppress a particular warning it is enough to specify the it's number to compiler as /wd#. My CMakeLists.txt contains such the block:
Now I can not say what exactly /wd4512 /wd4702 /wd4100 /wd4510 /wd4355 /wd4127 mean, because I do not pay any attention to MSVC for three years, but they suppress superpedantic warnings that does not influence the result.
I've seen this style being used:
Labelling the attribute is ideal way. MACRO leads to sometime confusion. and by using void(x),we are adding an overhead in processing.
If not using input argument, use
If not using the variable defined inside the function
Now later using the hash variable for your logic but doesn’t need bkt. define bkt as unused, otherwise compiler says'bkt set bt not used".
NOTE: This is just to suppress the warning not for optimization.
For the record, I like Job's answer above but I'm curious about a solution just using the variable name by itself in a "do-nothing" statement:
Sure, this has drawbacks; for instance, without the "unused" note it looks like a mistake rather than an intentional line of code.
The benefit is that no DEFINE is needed and it gets rid of the warning.
Are there any performance, optimization, or other differences?