Portable UNUSED parameter macro used on function signature for C and C++ Portable UNUSED parameter macro used on function signature for C and C++ c c

Portable UNUSED parameter macro used on function signature for C and C++


The way I do it is like this:

#define UNUSED(x) (void)(x)void foo(const int i) {    UNUSED(i);}

I've not had a problem with that in Visual Studio, Intel, gcc and clang.

The other option is to just comment out the parameter:

void foo(const int /*i*/) {  // When we need to use `i` we can just uncomment it.}


Just one small thing, better using __attribute__((__unused__)) as __attribute__((unused)), because unused could be somewhere defined as macro, personally I had a few issues with this situation.

But the trick I'm using is, which I found more readable is:

#define UNUSED(x) (void)x;

It works however only for the variables, and arguments of the methods, but not for the function itself.


After testing and following the comments, the original version mentioned in the question turned out to be good enough.

Using: #define UNUSED(x) __pragma(warning(suppress:4100)) x (mentioned in comments), might be necessary for compiling C on MSVC, but that's such a weird combination, that I didn't include it in the end.