Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define USE(x) (x) = (x)

In one of the C source code files I found the following line (macro):

#define USE(x) (x) = (x)

It is used like this:

int method(Obj *context)
    {
    USE(context);
    return 1;
    }

After googling for it, I found the following description:

// Macro to get rid of some compiler warnings

Could you please tell me more about this macro?

Thanks for your answers!

like image 894
Cybex Avatar asked Nov 15 '10 07:11

Cybex


2 Answers

Some compilers complain when variables are never actually used for anything. for instance:

int main(int argc, char **argv) {
  return 0;
}

Gives:

Output from llvm C/C++/Fortran front-end (llvm-gcc)

/tmp/webcompile/_7618_1.c: In function 'main':
/tmp/webcompile/_7618_1.c:9: warning: unused parameter 'argc'
/tmp/webcompile/_7618_1.c:9: warning: unused parameter 'argv'

Queerly, I can just get rid of those warnings using your macro:

#define USE(x) (x) = (x)


int main(int argc, char **argv) {
  USE(argc); /* get rid of warnings */
  USE(argv); /* get rid of warnings */
  return 0;
}
like image 160
SingleNegationElimination Avatar answered Oct 21 '22 04:10

SingleNegationElimination


The compilers give warnings when a variable is defined/declared but never used. These include function arguments. Some coding styles require to always name function arguments, but some of them may not be used in the function. They are reserved for future use. For these cases you could USE(param) to avoid the warning

like image 40
Armen Tsirunyan Avatar answered Oct 21 '22 05:10

Armen Tsirunyan