UNREFERENCED_PARAMETER

来源:互联网 发布:网络道德的本质 编辑:程序博客网 时间:2024/06/06 00:32

UNREFERENCED_PARAMETER是一个宏,定义如下

#define UNREFERENCED_PARAMETER(P)          (P)

MSDN上的说明如下:

UNREFERENCED_PARAMETER expands to the parameter or expression passed. Its purpose is to avoid compiler warnings about unreferenced parameters. Many programmers, including yours truly, like to compile with the highest warning level, Level 4 (/W4). Level 4 warnings fall into the category of "things that can be safely ignored." Little infelicities that won't break your code, though they might make you look bad. For example, you might have some line of code in your program like this

    int x=1;

but you never use x. Perhaps this line is left over from a time when you did use x, but then you removed the code and forgot to remove the variable. Warning Level 4 can find these minor mishaps. So why not let the compiler help you achieve the highest level of professionalism possible? Compiling with Level 4 is a way to show pride in your work. Level 4 is de rigueur if you're writing a library for public consumption. You don't want to force your developers to use a lower level to compile their code cleanly.

The problem is, Level 4 is really fussy. At Level 4, the compiler complains about such harmless things as—well, unreferenced parameters (unless, of course, you really did mean to use the parameter, in which case it's not so harmless). Say you have a function with two arguments, but you only use one:

int SomeFunction(int arg1, int arg2){  return arg1+5;}

With /W4, the compiler complains: "warning C4100: 'arg2' : unreferenced formal parameter." To fool the compiler, you can add UNREFERENCED_PARAMETER(arg2). Now your function references arg2 so the compiler will shut up.

也就是说,UNREFERENCED_PARAMETER的作用是用来消除编译警告,为引用的参数的警告。

在VC编译器下,如果你声明了一个变量,但并没有使用它时,编译器就会报警告:

"warning C4100: '***' : unreferenced formal parameter."

为了让编译器不必检测这种警告,就可以使用UNREFERENCED_PARAMETER语句。




原创粉丝点击