Does Q_UNUSED have any side effects?
Asked Answered
J

3

35

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.

Josefinejoseito answered 24/10, 2013 at 21:20 Comment(5)
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));Plutonium
@H2CO3: Couldn't it redeclare var to make any subsequent use ambiguous? extern qUnusedType var;Aciculate
@H2CO3: correct, it is not that magical.Ann
Possible duplicate of Why cast unused return values to void?Adriaadriaens
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.)Candor
T
45

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.

Torpedo answered 24/10, 2013 at 21:24 Comment(2)
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.Ann
@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.Morey
S
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

Schapira answered 18/5, 2023 at 8:57 Comment(0)
M
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.

Morey answered 18/5, 2023 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.