Does Q_UNUSED have any side effects?

22,884

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.

Share:
22,884

Related videos on Youtube

Ilya Kobelevskiy
Author by

Ilya Kobelevskiy

Updated on November 21, 2020

Comments

  • Ilya Kobelevskiy
    Ilya Kobelevskiy over 3 years

    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.

    • Admin
      Admin over 10 years
      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));
    • MSalters
      MSalters over 10 years
      @H2CO3: Couldn't it redeclare var to make any subsequent use ambiguous? extern qUnusedType var;
    • László Papp
      László Papp over 10 years
      @H2CO3: correct, it is not that magical.
    • Mikhail
      Mikhail about 7 years
    • HostileFork says dont trust SE
      HostileFork says dont trust SE over 4 years
      I posted an "UNUSED with teeth" 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, 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.)
  • László Papp
    László Papp over 10 years
    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.