1lOtzM291W
1lOtzM291W

Reputation: 470

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

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

#define UNUSED(x) ((x)=(x))

Upvotes: 7

Views: 4072

Answers (4)

BЈовић
BЈовић

Reputation: 64223

It silence the compiler from complaining that a variable is not used.

Other ways to do it :

  • completely remove the variable : void foo( int )
  • out comment the variable : void foo( int /* value */ )
  • use that macro : void foo( int value ){ UNUSED(value); }

Upvotes: 2

Ed Swangren
Ed Swangren

Reputation: 124712

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.

Upvotes: 9

mattnewport
mattnewport

Reputation: 14067

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.

Upvotes: 2

alk
alk

Reputation: 70951

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

Upvotes: 5

Related Questions