抑制未使用的参数警告 [英] Supressing Unused Parameter Warnings

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

问题描述

是否有标准方法来抑制有关未使用的

参数的警告?我有一个函数,可能被称为数十万

次,看起来像这样:


const void *

idx_byte (const void * p,int idx,void * context)

{

return(unsigned char *)p + idx;

}


这会产生一个警告上下文。参数未使用。如果我

插入:


context = NULL;


来压制那可能影响性能的因素?有了GCC我就知道了你能做的



const void *

idx_byte(const void * p,int idx,void * __attribute __((未使用))上下文)

{

return(unsigned char *)p + idx;

}


我不确定哪个更丑,但是有没有标准方法来处理

这个?


此外,这个功能是一个可能由

调用者提供的结构的成员,因此使其内联将没有影响吗?有没有办法

来提高这个功能的性能?


谢谢,

迈克

Is there a standard method for supressing warnings regarding unused
parameters? I have a function that might be called hundreds of thousands
of times that looks like this:

const void *
idx_byte(const void *p, int idx, void *context)
{
return (unsigned char *)p + idx;
}

This generates a warning that the "context" parameter is unused. If I
insert:

context = NULL;

to supress that will that potentially impact performace? With GCC I know
you can do:

const void *
idx_byte(const void *p, int idx, void * __attribute__ ((unused)) context)
{
return (unsigned char *)p + idx;
}

I''m not sure which is uglier but is there a standard method for handling
this?

Also, this function is a member of a structure possibly supplied by the
caller so making it ''inline'' will have no impact right? Is there a way
to improve the performance of this function?

Thanks,
Mike

推荐答案



2004年5月7日星期五,Michael B Allen写道:

On Fri, 7 May 2004, Michael B Allen wrote:

是否有标准抑制有关未使用
参数的警告的方法?


编号因为没有关于未使用参数的标准诊断。

这与''if'中的分配情况相同'

语句(可能在FAQ中,但我找不到)。

" unused参数"警告纯粹是一种实施质量问题,因为你的编译器可能提供关闭它的任何方式。

如果我插入:

context = NULL;

压制那可能影响性能的因素?


极不可能。您可以通过检查生成的程序集

或编译器中的机器代码来查找。任何体面的编译器应该是

能够弄清楚该任务是无关紧要的,并编译它

out。

在GCC,你也可以写下


上下文;


交易未使用的参数警告声明无效

警告;或者


(无效)上下文;


完全删除警告。或者,可能是最好的选择,你可以在

的第一个位置简单地将-Wno-unused选项传递给GCC编译器。所有这些都在GCC文档中解释,并且

与它相关的问题应该转到gnu.gcc.help,而不是comp.lang.c.

使用GCC I知道你可以这样做:

const void *
idx_byte(const void * p,int idx,void * __attribute __((unused))context)


在某些情况下,#define UNUSED取决于编译器上的
是否有意义:如果你能告诉你使用的是GCC,那么

#define UNUSED __attribute __( (未使用))

否则

#define未使用

这不是问题的标准答案,但是没有

标准答案,因为它不是标准问题---它是GCC

问题! :)

此外,这个函数是一个结构的成员


不在C中,它不是。功能是功能。结构成员

是数据。两个人永远不会见面。也许你的意思是这个

函数可以*由函数指针

类型的结构成员指向*,但我不知道那是怎么回事相关的。

可能由来电者提供


来电者是什么?

所以让它''内联''将没有影响对吗?有没有办法改善这个功能的性能?

Is there a standard method for supressing warnings regarding unused
parameters?
No. Because there is no standard diagnostic about unused parameters.
It''s the same sort of situation we have with assignments in ''if''
statements (that''s probably in the FAQ, but I can''t find it). The
"unused parameter" warning is purely a "quality of implementation"
issue, as is any way your compiler might provide to shut it off.
If I insert:

context = NULL;

to supress that will that potentially impact performace?
Highly unlikely. You can find out by examining the generated assembly
or machine code from your compiler(s). Any decent compiler should be
able to figure out that that assignment is irrelevant, and compile it
out.
On GCC, you can also write

context;

to trade the "unused argument" warning for a "statement has no effect"
warning; or

(void)context;

to remove the warning altogether. Or, probably the best option, you
can simply pass the -Wno-unused option to the GCC compiler in the
first place. All this is explained in the GCC documentation, and
questions related to it ought to go to gnu.gcc.help, not comp.lang.c.
With GCC I know you can do:

const void *
idx_byte(const void *p, int idx, void * __attribute__ ((unused)) context)
In some contexts, it would make sense to #define UNUSED depending
on the compiler: if you could tell you were using GCC, then
#define UNUSED __attribute__((unused))
otherwise
#define UNUSED
This is not a standard answer to the problem, but then there is no
standard answer because it''s not a standard problem --- it''s a GCC
problem! :)
Also, this function is a member of a structure
Not in C, it''s not. Functions are functions. Structure members
are data. Never the twain shall meet. Maybe you meant that this
function could be *pointed to* by a structure member of function-pointer
type, but I don''t see how that''s relevant.
possibly supplied by the caller
Caller of what?
so making it ''inline'' will have no impact right? Is there a way
to improve the performance of this function?




是的,但它也是非标准的:调高优化级别<你的编译器上有
。 ;)程序员可以做的事情并不多,而且
加快了一次加成!


-Arthur



Yes, but it''s also non-standard: turn up the optimization levels
on your compiler. ;) There''s not much the programmer can do to
expedite a single addition!

-Arthur


Michael B Allen< mb ***** @ ioplex.com>写道:
Michael B Allen <mb*****@ioplex.com> writes:
使用GCC,我知道你可以这样做:

const void *
idx_byte(const void * p,int idx,void * __attribute__ ((未使用))上下文)
{
返回(unsigned char *)p + idx;
}

我不确定哪个更丑,但是有一个标准的处理方法吗?


我通常做这样的事情:

#ifdef __GNUC__

#define UNUSED __attribute __((unused))

#else

#define UNUSED

#endif

....

void foo (int x UNUSED)

(没有什么能阻止其他编译器定义

__GNUC__,除了大批愤怒的程序员。)

此外,这个函数是可能由
调用者提供的结构的成员,因此使其内联将没有任何影响吗?有没有办法改善这个功能的性能?
With GCC I know you can do:

const void *
idx_byte(const void *p, int idx, void * __attribute__ ((unused)) context)
{
return (unsigned char *)p + idx;
}

I''m not sure which is uglier but is there a standard method for handling
this?
I usually do something like this:
#ifdef __GNUC__
#define UNUSED __attribute__ ((unused))
#else
#define UNUSED
#endif
....
void foo (int x UNUSED)
(There is nothing preventing other compilers from defining
__GNUC__, except legions of irate programmers.)
Also, this function is a member of a structure possibly supplied by the
caller so making it ''inline'' will have no impact right? Is there a way
to improve the performance of this function?




如果没有更广泛的背景,我看不出有什么方法可以改善

表现一个函数,基本上是'返回p [idx];''。

你真的知道这是一个瓶颈吗?你有没有用b / b
描述你的程序?

-

我认识的lusers是如此无能为力,如果他们被扼杀了线索< <>
麝香和掉落在一堆角质线索的中间,线索舞会

晚上线索欢乐时光,他们仍然无法得到线索。

- 迈克尔·戈德伍德,在修道院中



Without a broader context I can see no way to improve the
performance of a function that essentially does `return p[idx];''.
Do you actually know that this is a bottleneck? Have you
profiled your program?
--
"The lusers I know are so clueless, that if they were dipped in clue
musk and dropped in the middle of pack of horny clues, on clue prom
night during clue happy hour, they still couldn''t get a clue."
--Michael Girdwood, in the monastery


" Michael B Allen" < MB ***** @ ioplex.com>在消息中写道

news:pa ********************************** @ ioplex .c om ...
"Michael B Allen" <mb*****@ioplex.com> wrote in message
news:pa**********************************@ioplex.c om...
是否有标准方法来抑制有关未使用
参数的警告?我有一个可能被称为数十万次的函数,看起来像这样:

const void *
idx_byte(const void * p,int idx,void * context )
{


我不知道标准方法,但我使用:


(void)context;


这至少适用于GCC,并且与GCC提供的

格式的扩展不同,将使用任何编译器进行编译。

return (unsigned char *)p + idx;
}
这会产生一个警告:context参数未使用。如果我插入:

context = NULL;

压制那可能影响性能的东西?
Is there a standard method for supressing warnings regarding unused
parameters? I have a function that might be called hundreds of thousands
of times that looks like this:

const void *
idx_byte(const void *p, int idx, void *context)
{
No standard method that I know of, but I use:

(void)context;

This works for at least GCC and, unlike the extension for this purpose that
GCC provides, will compile with any compiler.
return (unsigned char *)p + idx;
}

This generates a warning that the "context" parameter is unused. If I
insert:

context = NULL;

to supress that will that potentially impact performace?




也许,但如果开启优化可能不会。在至少一个

编译器中,我使用过这个并不是真正的帮助,因为它将用无用的赋值警告替换未使用的

警告。


Alex



Perhaps, but probably not if optimisations are turned on. On at least one
compiler I have used this won''t really help, as it will replace the unused
warning with a useless assignment warning.

Alex


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

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