In c++03 and earlier to disable compiler warning about unused parameter I usually use such code:
#define UNUSED(expr) do { (void)(expr); } while (0)
For example
int main(int argc, char *argv[]) { UNUSED(argc); UNUSED(argv); return 0; }
But macros are not best practice for c++, so. Does any better solution appear with c++11 standard? I mean can I get rid of macros?
Thanks for all!
Unused parameters : __attribute__((unused))It is not (yet) part of the C standard, but is supported by the GNU gcc compiler as an extension (ie. works with gcc, but maybe not with other compilers).
Solution: If variable <variable_name> or function <function_name> is not used, it can be removed. If it is only used sometimes, you can use __attribute__((unused)) . This attribute suppresses these warnings.
An unused variable refers to a variable of which the data element structure is unreferenced in the main program, including the parent and the subordinate items. An unused copybook refers to a copybook with the aforementioned copybook of an unused variable.
You can just omit the parameter names:
int main(int, char *[]) { return 0; }
And in the case of main, you can even omit the parameters altogether:
int main() { // no return implies return 0; }
See "§ 3.6 Start and Termination" in the C++11 Standard.
There is the <tuple>
in C++11, which includes the ready to use std::ignore
object, that's allow us to write (very likely without imposing runtime overheads):
void f(int x) { std::ignore = x; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With