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.
#ifdef __GNUC__
# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#else
# define UNUSED(x) UNUSED_ ## x
#endif
#ifdef __GNUC__
# define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
#else
# define UNUSED_FUNCTION(x) UNUSED_ ## x
#endif
Then you can do...
void foo(int UNUSED(bar)) { ... }
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...
static void UNUSED_FUNCTION(foo)(int bar) { ... }
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])
or float (*UNUSED(coords))[3]
, This is the only downside to the UNUSED
macro I found so far, in these cases I fall back to (void)coords;
-Wno-unused-parameter
, it's just too noisy and rarely catches bugs esp. when-Wshadow
is used. – Trass3r