例如:
Bool NullFunc(const struct timespec *when, const char *who)
{
return TRUE;
}
在C++中,我能够在参数周围放置一个/*...*/
注释。当然,在C语言中不是这样,因为它给了我错误:
错误:省略参数名
发布于 2010-08-30 09:16:32
我通常写这样的宏:
#define UNUSED(x) (void)(x)
可以对所有未使用的参数使用此宏。(请注意,这适用于任何编译器。)
例如:
void f(int x) {
UNUSED(x);
...
}
发布于 2010-08-30 09:24:58
在GCC中,您可以使用属性标记参数。
此属性附加于变量,意味着该变量可能未使用。GCC不会对这个变量提出警告。
在实践中,这是通过在参数之前放置__attribute__ ((unused))
来实现的。例如:
void foo(workerid_t workerId) { }
变成了
void foo(__attribute__((unused)) workerid_t workerId) { }
发布于 2012-10-15 07:39:59
您可以使用GCC或嘎吱声的https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html#index-g_t_0040code_007bunused_007d-attribute_002e-2640属性,但是,我在头中使用这些宏,以避免在源代码中都有GCC特定的属性,而__attribute__
无处不在也有点冗长/丑陋。
#ifdef __GNUC__
# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#else
# define UNUSED(x) UNUSED_ ## x
#endif
#ifdef __GNUC__
# define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
#else
# define UNUSED_FUNCTION(x) UNUSED_ ## x
#endif
那你就可以..。
void foo(int UNUSED(bar)) { ... }
我更喜欢这样做,因为如果在代码中任何地方使用bar
,都会出现错误,因此不能错误地保留属性。
至于功能..。
static void UNUSED_FUNCTION(foo)(int bar) { ... }
注1):
据我所知,MSVC没有与__attribute__((__unused__))
等价的东西。
注2):
对于包含括号的参数,UNUSED
宏不起作用,
所以如果你有一个像float (*coords)[3]
,,不能做的争论,
float UNUSED((*coords)[3])
或float (*UNUSED(coords))[3]
.这是我到目前为止发现的UNUSED
宏的唯一缺点,在这些情况下,我回到了(void)coords;
。
https://stackoverflow.com/questions/3599160
复制相似问题