Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does this macro mean ? #define UNUSED(x) ((x)=(x))

Tags:

c++

c

what does this macro mean ? I just find the following macro in source files:

#define UNUSED(x) ((x)=(x))
like image 845
1lOtzM291W Avatar asked Nov 15 '13 07:11

1lOtzM291W


3 Answers

It is probably there to suppress compiler warnings of unused variables/arguments to functions. You could also use this:

// C++ only
void some_func(int /*x*/)

Or

// C and C++
void some_func(int x)
{
    (void)x;
}

Or your compiler may support a flag to do so, but these are portable and won't skip over valid warnings.

like image 100
Ed S. Avatar answered Oct 16 '22 12:10

Ed S.


Use it to get rid of any compiler warning referring an unused variable.

like image 42
alk Avatar answered Oct 16 '22 12:10

alk


Some compilers issue a warning about unused variables - variables that are defined but never referenced. Sometimes you have code that references a variable only under some conditional ifdefs (only on some platforms or only in debug) and it is inconvenient to duplicate those conditions at the point the variable is defined. A macro like this can be used to suppress the unused variable warning in such cases.

like image 2
mattnewport Avatar answered Oct 16 '22 12:10

mattnewport