16

When implementing stubs etc. you want to avoid "unused variable" warnings. I've come across a few alternatives of UNUSED() macros over the years, but never one which either is proven to work for "all" compilers, or one which by standard is air tight.

Or are we stuck with #ifdef blocks for each build platform?

EDIT: Due to a number of answers with non c-compliant alternatives, I'd like to clarify that I'm looking for a definition which is valid for both C and C++, all flavours etc.

Rob Latham
  • 5,085
  • 3
  • 27
  • 44
sharkin
  • 12,162
  • 24
  • 86
  • 122
  • 5
    Neither of the standards says anything about when warnings can or should be issued, so there is no way for anything to be airtight by standards in either of the languages you're asking about. – Steve Jessop Jan 31 '11 at 14:39
  • @Steve: Indeed, clumsy line of thought. – sharkin Jan 31 '11 at 14:42
  • possible duplicate of [Will a "variableName;" C++ statement be a no-op at all times?](http://stackoverflow.com/questions/4030959/will-a-variablename-c-statement-be-a-no-op-at-all-times) – Ben Voigt Jan 31 '11 at 18:05
  • I'm more interested in what this C/C++ magic language is. – GManNickG Jan 31 '11 at 19:17
  • very close but no C C++ cross lang requirement: http://stackoverflow.com/questions/3417837/what-is-the-best-way-to-supress-unused-variable-x-warning. Still, you could break up the macro into 2 parts, detect language, and solve accordingly. – Ciro Santilli OurBigBook.com Sep 17 '14 at 07:49

3 Answers3

31

According to this answer by user GMan the typical way is to cast to void:

#define UNUSED(x) (void)(x)

but if x is marked as volatile that would enforce reading from the variable and thus have a side effect and so the actual way to almost guarantee a no-op and suppress the compiler warning is the following:

// use expression as sub-expression,
// then make type of full expression int, discard result
#define UNUSED(x) (void)(sizeof((x), 0))
Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 2
    You found a very complete answer and picked the worst of the suggested solutions? BTW when an answer already exists on SO, it's a good sign the question is a dupe. – Ben Voigt Jan 31 '11 at 18:05
  • @Ben: I was considering making a similar comment, but for the intends and purposes of the macro, I think this is canonical enough. (That is, just to use a parameter and not an expression.) – GManNickG Feb 01 '11 at 00:45
  • @GMan: One hopes that the variable isn't `volatile`, because if it was, this `UNUSED` macro actually would read it. (Please note that I don't see any utility in top-level `volatile` on function parameters, but it could be `volatile T&`). – Ben Voigt Feb 01 '11 at 01:49
  • @Ben: Ah, then good point. Forgot about those. @sharptooth: You might want to take my last consideration as the best solution, then. – GManNickG Feb 01 '11 at 06:08
  • @Ben Voigt: Thank you for pointing that out, since that's not only an extra operation, but also can have a side effect (processor cache line fill or hardware port read). @GMan: Thank you, I hope I got the emphasis right now. – sharptooth Feb 01 '11 at 06:27
  • @sharptooth @Ben: Sorry guys, I actually decided to test my "solution" and it generates a warning! Understandable, since while `(void)x` "uses" `x`, `sizeof((x), 0)` never operates on values, just types. I have updated my answer to be far more complete. – GManNickG Feb 01 '11 at 10:31
  • @GMan: The new solution is unbelievably cool. It's a great illustration of how hard it is to write near-perfect code. – sharptooth Feb 01 '11 at 10:35
  • 6
    Just tried '(void)(sizeof((x), 0))' version on Visual Studio 2010 Pro SP 1 with warning level 4 and got warning C4100 "unreferenced formal parameter". Note that the classic/bad '(void)(x)' produced no warning. – Galadrius Krunthar Mar 26 '13 at 00:17
  • 6
    I was excited to have the no-side-effects-for-volatile feature but this generates a warning on gcc 4.8.2: `warning: left-hand operand of comma expression has no effect [-Wunused-value] #define UNUSED(x) (void)(sizeof((x), 0))` – Rian Sanderson Jul 06 '16 at 23:02
2

In C++, just comment out the names.

void MyFunction(int /* name_of_arg1 */, float /* name_of_arg2*/)
{
  ...
}
tenfour
  • 36,141
  • 15
  • 83
  • 142
  • 2
    this is a solution for C++ only. There is no point in having a single solution which works for both C and C++; the point is that this works for all compilers / platforms in C++. A C solution is independent. – tenfour Jan 31 '11 at 14:03
  • I don't understand your argument; Partly because you generally want to have a c-conformant definition if you have a codebase which blends c and c++ (very common especially with codebases of age or where you implement/wrap 3rd party c layers), and partly because the problem is most evident when building c-code - some c++ compilers doesn't even complain about unused arguments (e.g. microsoft). – sharkin Jan 31 '11 at 14:21
  • 1
    The only time you want the same code compiled as both C and C++ is in forward declarations (e.g. headers), where this warning does not apply anyway. In the case that you actually want the function body to work in both C and C++, you're right: my argument does not apply. – tenfour Jan 31 '11 at 14:34
-1

The universal way is not to turn on warnings options that spam warnings for clearly-correct code. Any "unused variable" warning option that includes function arguments in its analysis is simply wrong and should be left off. Don't litter your code with ugliness to quiet broken compilers.

You might also try sending a bug report to the compiler maintainer/vendor.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • I actually agree in some sense. However, even if I'd hesitate to move the problem to maintaining warning-disablers instead, some compilers don't support individual warnings being disabled. – sharkin Jan 31 '11 at 20:32
  • As long as the compiler you use for development supports disabling individual warnings, all is well. Just disable all warnings on systems other than your development one if needed. Surely gcc and MSVC support disabling this warning... – R.. GitHub STOP HELPING ICE Jan 31 '11 at 20:38
  • 3
    Also, the desired behavior is usually: "Warn unless other wise stated". If void bar(int foo), does not have an UNUSED(foo), the warning should NOT be supressed – aiao Oct 12 '14 at 16:04