void-ing the expression (as in q_unused)
-
qt defines
Q_UNUSED
as follows:#define Q_UNUSED(x) (void)x;
isn't void-ing the expression an old way to do things? is there a modern way (and why qt isn't using it)?
-
@user4592357 The only alternative I'm aware of is to omit parameter name:
void someFunction(int, int)
Or you can comment out the parameter names:
void someFunction(int /*param1*/, int /*param2*/)
But using Q_UNUSED makes it clear that the parameter is not used by intent.
In C++17 there is this: https://en.cppreference.com/w/cpp/language/attributes/maybe_unused
But since Qt needs to support older compilers as well (currently min requirement is C++11) this can't be used by Qt itself. -
while we are here, I never understood why
Q_UNUSED
is preferred to omitting the parameters. the latter actually forces the compiler to make sure it's unused but there's nothing, not even a warning, if you use an argument that was previously marked as unused -
qdoc
uses the source to generate the documentation, thus it uses the parameter name from the function. If you don't specify a parameter name you'd break the docs. I personally prefer to just omit the parameter in my code (wherever possible), but that's not that straightforwardly attainable in Qt's codebase.