抑制“未使用参数”警告 [英] Suppressing "Parameter not used" Warning

查看:66
本文介绍了抑制“未使用参数”警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意crosspost。


通常在编写需要函数指针的代码时,必须使用

来编写忽略其形式参数的函数。例如,

a状态机功能可能需要状态输入,但某个

错误处理状态可能会忽略它:


typedef void(* State_Fn)(uint8_t);


void error_state(uint8_t status)

{

NOT_USED(status) ;


/ *代码处理错误但忽略状态* /

}


在另一组中,一张海报询问如何将上面显示的宏NOT_USED定义为

来安静编译器警告。他的建议

实施是


#define NOT_USED(p)((void)(p))


在这种特殊情况下,他注意到他正在使用的编译器即使在存在这个宏的情况下也会产生警告。我建议使用




#define NOT_USED(p)((p)=(p))


他很高兴它有效,但担心前一个实现得到更广泛的支持,而后者可能会生成可执行代码。我以前从未见过前者,

虽然我经常看到后者(通常不会隐藏在宏观背后),

我永远不会看到它实际上生成代码。至少,不是在过去十年左右的



所以我很好奇。哪种形式(如果有的话)更常见?是否有任何

实现会为后者生成可执行代码?


谢谢,

- =戴夫


- =戴夫

-

改变是不可避免的,进步不是。

Please note crosspost.

Often when writing code requiring function pointers, it is necessary
to write functions that ignore their formal parameters. For example,
a state machine function might take a status input, but a certain
error-handling state might ignore it:

typedef void (*State_Fn)(uint8_t);

void error_state(uint8_t status)
{
NOT_USED(status);

/* code handling error but ignoring status */
}

In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning. His suggested
implementation was

#define NOT_USED(p) ((void)(p))

In this particular case, he noted the compiler he was using would
generate the warning even in the presence of this macro. I suggested
he use

#define NOT_USED(p) ((p)=(p))

He was pleased that it worked, but was concerned that the former
implementation was more widely supported, and that the latter might
generate executable code. I for one had never seen the former before,
though I''ve often seen the latter (usually not hidden behind a macro),
and I''ve never seen it actually generate code. At least, not in the
last ten years or so.

So I''m curious. Which form (if either) is more common? Are there any
implementations that will generate executable code for the latter?

Thanks,
-=Dave

-=Dave
--
Change is inevitable, progress is not.

推荐答案

Dave Hansen写道:
Dave Hansen wrote:

请注意crosspost。

通常在编写需要函数指针的代码时,有必要
编写忽略其形式参数的函数。例如,状态机功能可能需要状态输入,但某种错误处理状态可能会忽略它:

typedef void(* State_Fn)(uint8_t);

void error_state(uint8_t status)
{
NOT_USED(status);

/ *代码处理错误但忽略状态* /
}

在另一组中,一张海报询问如何定义上面显示的宏NOT_USED以安静编译器警告。他建议的实施是

#define NOT_USED(p)((void)(p))

在这个特殊情况下,他注意到他正在使用的编译器即使存在这个宏,它也会生成警告。我建议他使用

#define NOT_USED(p)((p)=(p))

他很高兴它有效,但担心前一个实现得到了更广泛的支持,后者可能会生成可执行代码。


非可执行代码往往会产生警告。

我以前从未见过前者,
虽然我经常看到后者(通常不会隐藏在宏后面),
我从未见过它实际生成代码。至少,不是在过去十年左右。

所以我很好奇。哪种形式(如果有的话)更常见?是否有任何
实现将为后者生成可执行代码?

Please note crosspost.

Often when writing code requiring function pointers, it is necessary
to write functions that ignore their formal parameters. For example,
a state machine function might take a status input, but a certain
error-handling state might ignore it:

typedef void (*State_Fn)(uint8_t);

void error_state(uint8_t status)
{
NOT_USED(status);

/* code handling error but ignoring status */
}

In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning. His suggested
implementation was

#define NOT_USED(p) ((void)(p))

In this particular case, he noted the compiler he was using would
generate the warning even in the presence of this macro. I suggested
he use

#define NOT_USED(p) ((p)=(p))

He was pleased that it worked, but was concerned that the former
implementation was more widely supported, and that the latter might
generate executable code.
Non executable code tends to generate warnings.
I for one had never seen the former before,
though I''ve often seen the latter (usually not hidden behind a macro),
and I''ve never seen it actually generate code. At least, not in the
last ten years or so.

So I''m curious. Which form (if either) is more common? Are there any
implementations that will generate executable code for the latter?




在分发计时程序的分发文件中

my分布函数参数具有以下形式:

(e_type *数组,size_t n,长无符号*种子)


但只有部分使用种子PRNG。


其他人是这样的:


void sorted(e_type * a,size_t n,long unsigned * seed)

{

a + = n;

while(n--!= 0){

(* - a) .data = n;

}

种子;

}


所以,我只是制作种子的表达陈述

这似乎停止了警告。


-

pete



In distributions file for a sort timing program
my distribution function arguments are of this form:
(e_type *array, size_t n, long unsigned *seed)

but only some of them use the seed for a PRNG.

Others are like this:

void sorted(e_type *a, size_t n, long unsigned *seed)
{
a += n;
while (n-- != 0) {
(*--a).data = n;
}
seed;
}

So, I just make an expression statement out of seed
and that seems to stop the warnings.

--
pete


在comp.lang.c中Dave Hansen< id ** @ hotmail.com>写道:
In comp.lang.c Dave Hansen <id**@hotmail.com> wrote:
typedef void(* State_Fn)(uint8_t);
void error_state(uint8_t status)
{
NOT_USED(status);
/ *代码处理错误但忽略状态* /
}
typedef void (*State_Fn)(uint8_t); void error_state(uint8_t status)
{
NOT_USED(status); /* code handling error but ignoring status */
}




为什么不只是


void error_state(uint8_t)/ *不关心形式参数* /

{

/ * code * /

}


? (我不足以回答你真正的问题。)


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



Why not just

void error_state( uint8_t ) /* don''t care about formal parameter */
{
/* code */
}

? (I''m not enough of a guru to answer your real question.)

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


Christopher Benson-Manica写道:
Christopher Benson-Manica wrote:
在comp.lang.c中Dave Hansen< id ** @ hotmail.com>写道:

In comp.lang.c Dave Hansen <id**@hotmail.com> wrote:

typedef void(* State_Fn)(uint8_t);
typedef void (*State_Fn)(uint8_t);





void error_state (uint8_t status)
{
NOT_USED(status);
void error_state(uint8_t status)
{
NOT_USED(status);





/ *代码处理错误但忽略状态* /
}
/* code handling error but ignoring status */
}



为什么不只是

void error_state(uint8_t)/ *不关心形式参数* /
{


Why not just

void error_state( uint8_t ) /* don''t care about formal parameter */
{



因为那不合法C.如果是,我们显然不需要任何

黑客攻击。你可以在原型中做到这一点;在C ++中,您也可以在

定义中执行此操作。但是不在C中。


S.


Because that''s not legal C. If it were, we obviously wouldn''t need any
hacks. You can do this in prototypes; in C++ you can also do it in
definitions. Not in C, however.

S.


这篇关于抑制“未使用参数”警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆