46

I'm interested in creating a macro for eliminating the unused variable warning.

This question describes a way to suppress the unused parameter warning by writing a macro inside the function code:

Universally compiler independent way of implementing an UNUSED macro in C/C++

But I'm interested in a macro that can be used in the function signature:

void callback(int UNUSED(some_useless_stuff)) {}

This is what I dug out using Google (source)

#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#elif defined(__cplusplus)
# define UNUSED(x)
#else
# define UNUSED(x) x
#endif

Can this be further expanded for other compilers?

Edit: For those who can't understand how tagging works: I want a solution for both C and C++. That is why this question is tagged both C and C++ and that is why a C++ only solution is not acceptable.

Community
  • 1
  • 1
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151

4 Answers4

35

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.
}
Matt Clarkson
  • 14,106
  • 10
  • 57
  • 85
  • 1
    The first answer solves a completely different problem, the second solution works only for C++. – Šimon Tóth May 23 '16 at 14:51
  • 1
    Casting to void works on unused variables too, unused functions even `(void)printf`. And leaking implementation details by tagging parameters in function signatures tastes funny to begin with. – diapir Aug 16 '16 at 09:36
12

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.

doc_ds
  • 121
  • 1
  • 3
10

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.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • This is what i was missing. I'm actually using `#define UNUSED(x) __pragma(warning(suppress: 4100 4101)) x` to be able to decorate both function arguments and local variables unused (`__attribute__((unused))` works the same way in g++). Super cool! – the swine Jan 31 '14 at 13:33
  • 1
    This didnt work for me with MSVC2013, Still got `C4100` warnings all over. – ideasman42 Feb 17 '14 at 21:51
  • 1
    Out of curiosity, why did you choose this over @MattClarkson's answer? This is certainly less portable compared to simple `(void)x`. – vgru Mar 22 '17 at 13:21
  • I know why I go in this direction: I have unused parameter in a `constexpr` function which (in VS2015) cannot have multiple statements. – CygnusX1 May 06 '17 at 21:32
6

Across many compilers I have used the following, excluding support for lint.

#if (__GNUC__ > 2) || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
#       define PGM_GNUC_UNUSED         __attribute__((__unused__))
#else
#       define PGM_GNUC_UNUSED
#endif

Tested compilers: GCC, Clang, EKOPath, Intel C Compiler / Composer XE, MinGW32 on Cygwin / Linux / MSYS, MinGW-w64 on Cygwin / Linux, Sun ONE Studio / Oracle Solaris Studio, Visual Studio 2008 / 2010.

Example usage:

pgm_tsc_init (
        PGM_GNUC_UNUSED pgm_error_t**   error
        )
{
...
}

PGM is the standard prefix for this C based project. GNUC is the convention from GLib for this attribute.

I think one compile warns about __attribute__ in certain circumstances but certainly no error.

Steve-o
  • 12,678
  • 2
  • 41
  • 60
  • Are you sure? I don't think that MSVC actually defines `__GNUC__` macro, that would be very weird. – Šimon Tóth Aug 17 '11 at 10:32
  • 2
    @Let_Me_Be: if it doesn't, then in the context of a `#if` condition, it's as if `__GNUC__` were defined equal to 0. So you'll get the `#else` branch, which Steve-o has tested to work on Visual Studio 2008 and 2010. Not necessarily at the same warning level you're using. – Steve Jessop Aug 17 '11 at 10:37
  • @Let_Me_Be that is why Cygwin & Mingw use it but MSVC does not, the end result is a working system from Debian 4 / RHEL 4 and newer. – Steve-o Aug 17 '11 at 10:37
  • 2
    @Steve-o Well, then this solution is a step backward, since it will only work on GCC. It won't suppress the warning on any other compiler. – Šimon Tóth Aug 17 '11 at 11:07
  • According to the docs at , the first GCC version that accepts `__attribute__((unused))` is 2.7, not 2.5. – Alex Shpilkin Jan 13 '21 at 18:35