Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unused parameter in c++11

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!

like image 545
inkooboo Avatar asked Apr 02 '13 12:04

inkooboo


People also ask

What is unused parameter in C?

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).

How do you solve unused variables?

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.

What are unused variables?

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.


2 Answers

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.

like image 113
Henrik Avatar answered Oct 21 '22 22:10

Henrik


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; } 
like image 28
Tomilov Anatoliy Avatar answered Oct 21 '22 22:10

Tomilov Anatoliy