抑制“未使用”的最佳方法警告? [英] best way to suppress "unused" warning?
问题描述
我正在使用一个可以进行大量回调的API。在经典回调
样式中,每个例程都提供一个void *指针来携带用户定义的
数据。但是,有时候,不需要用户定义的指针,而b $ b会导致编译器吐出有用的指针。警告。例如:
%cat unused.c
#include< stdio.h>
int
foo(char * str,void * data)
{
puts(str);
返回0;
}
%gcc -c -W -Wall unused.c
unused.c:4:警告:未使用的参数''data' '
我正在寻找一种合适的(安全,合理的自我记录)方式
在我预期的情况下抑制警告(因此我不是在寻找编译器标志来抑制警告 - 我只是想
能够标记我所知道的地方)。我使用过像
data = data;
data ++;
if(data!= data)return;
他们都工作但是根据我的口味有点混淆。我肯定
很多其他人都遇到过这种情况 - 是否有推荐的技术?
RM
I''m using an API which does a lot of callbacks. In classic callback
style, each routine provides a void * pointer to carry user-defined
data. Sometimes, however, the user-defined pointer is not needed which
causes the compiler to spit out a "helpful" warning. For example:
% cat unused.c
#include <stdio.h>
int
foo(char *str, void *data)
{
puts(str);
return 0;
}
% gcc -c -W -Wall unused.c
unused.c:4: warning: unused parameter ''data''
I''m looking for an appropriate (safe, reasonably self-documenting) way
of suppressing that warning in cases where I expect it (thus I''m not
looking for the compiler flags to suppress the warning - I just want to
be able to mark the places I know about). I''ve used constructs like
data = data;
data++;
if (data != data) return;
And they all work but are a little too obfuscated for my taste. I''m sure
many other people have run into this - is there a recommended technique?
RM
推荐答案
Rex Mottram说:
Rex Mottram said:
我正在使用一个可以进行大量回调的API。在经典回调
样式中,每个例程都提供一个void *指针来携带用户定义的
数据。但是,有时候,不需要用户定义的指针,而b $ b会导致编译器吐出有用的指针。警告。例如:
%cat unused.c
#include< stdio.h>
int
foo(char * str,void * data)
{
puts(str);
返回0;
}
%gcc -c -W -Wall unused.c
unused.c:4:警告:未使用的参数''data' '
我正在寻找一种合适的(安全,合理的自我记录)方式
在我预期的情况下抑制警告(因此我不是在寻找编译器标志来抑制警告 - 我只是想
能够标记我所知道的地方)。我使用过像
data = data;
data ++;
if(data!= data)return;
他们都工作但是根据我的口味有点混淆。我很确定
很多其他人都遇到过这种情况 - 是否有推荐的技术?
I''m using an API which does a lot of callbacks. In classic callback
style, each routine provides a void * pointer to carry user-defined
data. Sometimes, however, the user-defined pointer is not needed which
causes the compiler to spit out a "helpful" warning. For example:
% cat unused.c
#include <stdio.h>
int
foo(char *str, void *data)
{
puts(str);
return 0;
}
% gcc -c -W -Wall unused.c
unused.c:4: warning: unused parameter ''data''
I''m looking for an appropriate (safe, reasonably self-documenting) way
of suppressing that warning in cases where I expect it (thus I''m not
looking for the compiler flags to suppress the warning - I just want to
be able to mark the places I know about). I''ve used constructs like
data = data;
data++;
if (data != data) return;
And they all work but are a little too obfuscated for my taste. I''m sure
many other people have run into this - is there a recommended technique?
很多人为此使用(无效)数据,这与你的任何
一样好提到,虽然我在犹豫之前声称它'
"推荐因为这样的索赔留下了一些重要的问题
未得到答复(例如,由谁推荐?)。
您可能要考虑:
#define DISCARD_PARAMETER(无效)
用法:
DISCARD_PARAMETER数据;
或者您可能更喜欢:
#define DISCARD_PARAMETER(p)(无效)p
用法:
DISCARD_PARAMETER(数据);
-
Richard Heathfield< http://www.cpax。 org.uk>
电子邮件:-http:// www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
Lots of people use (void)data for this, and it''s just as good a way as any
of those you mention, although I''d hesitate before claiming it''s
"recommended" because such a claim leaves some important questions
unanswered (for instance, "recommended by whom?").
You might want to consider:
#define DISCARD_PARAMETER (void)
Usage:
DISCARD_PARAMETER data;
or you might prefer:
#define DISCARD_PARAMETER(p) (void)p
Usage:
DISCARD_PARAMETER(data);
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
2008年4月25日1:24,Richard Heathfield写道:
On 25 Apr 2008 at 1:24, Richard Heathfield wrote:
Lots人们使用(无效)数据,这就像
一样好你提到的任何一个
Lots of people use (void)data for this, and it''s just as good a way as
any of those you mention
混淆你的代码来安抚你的编译器或者lint给我带来的好处
让尾巴摇尾巴。
Obfuscating your code to pacify your compiler or lint smacks to me of
letting the tail wag the dog.
Antoninus Twink写道:
Antoninus Twink wrote:
2008年4月25日1:24,Richard Heathfield写道:
On 25 Apr 2008 at 1:24, Richard Heathfield wrote:
>很多人们使用(无效)数据,它就像你提到的任何人一样好。
>Lots of people use (void)data for this, and it''s just as good a way
as any of those you mention
模糊你的代码安抚你的编译器或者lint对我来说是让b / b
让尾巴摇尾巴。
Obfuscating your code to pacify your compiler or lint smacks to me of
letting the tail wag the dog.
在现实生活中,可能存在需要此编码的编码标准。
我曾经在一家公司工作过必须使用-Wall -Werror,
或类似的东西以及所有新代码和所有旧代码一旦获得
触及。
这是一个普通人的痛苦,但导致更好的代码和许多
尚未解释的错误神秘地失望...
再见,Jojo
In real life there may be coding standards that demand for this.
At one company I used to work for it was mandatory to use "-Wall -Werror",
or something eqivalent and all new code and on all older code once it got
touched.
It was a pain in the proverbian, but lead to much better code and lots of
yet unexplained faults mysteriously disappaered...
Bye, Jojo
这篇关于抑制“未使用”的最佳方法警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!