31

Given the following piece of code:

void test(int var)
{
     Q_UNUSED(var);
#ifdef SOMETHING
     printf("%d",var);
     //do something else with var...
#endif
}

Would the Q_UNUSED macro have any effect if I actually use the 'var' variable in some scenario (like in the example above), or it has no effect at all when I suppress compiler warnings for unused variables?

So far I observe it has no effect, but I would like to make sure.

László Papp
  • 51,870
  • 39
  • 111
  • 135
Ilya Kobelevskiy
  • 5,245
  • 4
  • 24
  • 41
  • 1
    Just look at its documentation. If there's none, then read its definition. (hint: it probably hasn't any. It most certainly **cannot possibly** render a variable "unusable". I guess it's something like `((void)(expression));` –  Oct 24 '13 at 21:24
  • @H2CO3: Couldn't it redeclare `var` to make any subsequent use ambiguous? `extern qUnusedType var;` – MSalters Oct 25 '13 at 12:26
  • 2
    @H2CO3: correct, it is not that magical. – László Papp Dec 15 '13 at 11:49
  • Possible duplicate of [Why cast unused return values to void?](http://stackoverflow.com/questions/689677/why-cast-unused-return-values-to-void) – Mikhail Apr 13 '17 at 15:41
  • I posted an ["UNUSED with teeth"](https://codereview.stackexchange.com/questions/159439/) on CodeReview SE... for anyone who is interested in the idea of actually corrupting the data. *(Not ideal to corrupt it but the toothlessness of unused annotation was continued even in standard C++ annotations, which puzzlingly went with [`[[maybe_unused]]` on the parameter definition](https://stackoverflow.com/questions/49320810/when-should-i-use-maybe-unused), instead of letting you mark points in the control flow after which you don't want a variable used and have the compiler catch it.)* – HostileFork says dont trust SE Nov 21 '19 at 11:14

3 Answers3

43

No in many cases (e.g. just passing a simple variable to the macro). The definition is inside qglobal.h:

#  define Q_UNUSED(x) (void)x;

To disable unused variable warnings. You can use the variable after this macro without any problem.

However, if you pass an expression or something else to the macro and the compiler has to evaluate the expression it may has side effects.

Community
  • 1
  • 1
masoud
  • 55,379
  • 16
  • 141
  • 208
  • 6
    The only side effect, I can see, is the porting effort if you switch away from Qt, but that is not a big deal since you would have more issues anyway. – László Papp Dec 15 '13 at 11:48
  • @LászlóPapp it may have side effects if we assume that argument can be _anything_, not just a boring `int`. It's an unconditional load of value. Or an unconditional call to cast operator. `x` might be an atomic so this would put up a memory fence. In fact some compilers are known to generate bogus code on this exact expression even in simple cases. – Swift - Friday Pie May 18 '23 at 09:24
1

There should be no side effects, but on MSVC it can sometimes produce useless memcpy instructions. Simplest example is with volatile. There might be others: https://developercommunity.visualstudio.com/t/visual-c-generates-terrible-code-when-struct-conta/650789

Dorian
  • 377
  • 5
  • 18
0

IT's Qt's way to suppress warning by using it as part of an expression without an effect. Kind of. Old compilers had unportable ways to mark arguments unused or unportable ways to suppress warning temporarily. On other had some newest compilers would generate a diagnostic message about statement that does nothing.

Modern C++ got an alternative. But, if used in header file, it may mess with meta-object compiler.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42