问题描述
在C语言中,与C ++不同,函数定义的所有参数都必须被命名。而不是使用来消除未使用的参数错误( void)a
,或者公开使用 __ attribute __((unused))
,我创建了以下宏:
#define UNUSED2(var,uniq)UNUSED_ ## line ## var __attribute((未使用))
//压缩未使用的变量警告,是否可以没有var就完成了吗?
#define UNUSED(var)UNUSED2(var,__func__)
p>
void blah(char const * UNUSED(path)){}
有什么方法可以保证唯一的虚拟变量名称(显然 LINE
和 __ func __
无法削减它),或忽略命名未使用的变量?
Update0
最终使用的代码是。
#ifdef __cplusplus
// C ++允许您省略参数名称if它们未被使用
#define OMIT_PARAM
#else
//必须在C中提供一个变量名称,因此将其标记为未使用
#define OMIT_PARAM3(uniq) const ignored_parameter _ ## uniq VARATTR_UNUSED
#define OMIT_PARAM2(uniq)OMIT_PARAM3(uniq)
#define OMIT_PARAM OMIT_PARAM2(__ COUNT ER__)
#endif
#ifdef _MSC_VER
#define VARATTR_UNUSED
#else
#define VARATTR_UNUSED __attribute __((未使用))
# endif
它是这样使用的:
void blah(char const * OMIT_PARAM){}
这两个参数都是未使用的参数,未命名的参数警告,并保证它不会打开其他变量名称。 有一个 __ COUNTER __
宏这给出了唯一性,看起来GCC有一个与4.3.5版同名的同名宏(尽管目前我还找不到实时链接)。
In C, unlike C++, all parameters to a function definition must be named.
Instead of quashing "unused parameter" errors with (void)a
, or openly using __attribute__((unused))
, I've created the following macro:
#define UNUSED2(var, uniq) UNUSED_ ## line ## var __attribute((unused))
// squash unused variable warnings, can it be done without var?
#define UNUSED(var) UNUSED2(var, __func__)
Used like this
void blah(char const *UNUSED(path)) {}
Is there some way I can guarantee a unique "dummy" variable name (obviously LINE
and __func__
can't cut it), or neglect to name the unused variables at all?
Update0
The final code used is available here.
#ifdef __cplusplus
// C++ allows you to omit parameter names if they're unused
# define OMIT_PARAM
#else
// A variable name must be provided in C, so make one up and mark it unused
# define OMIT_PARAM3(uniq) const omitted_parameter_##uniq VARATTR_UNUSED
# define OMIT_PARAM2(uniq) OMIT_PARAM3(uniq)
# define OMIT_PARAM OMIT_PARAM2(__COUNTER__)
#endif
#ifdef _MSC_VER
# define VARATTR_UNUSED
#else
# define VARATTR_UNUSED __attribute__((unused))
#endif
It's used like this:
void blah(char const *OMIT_PARAM) {}
And avoids both unused parameter, unnamed parameter warnings, and guarantees that it's not going to clobber some other variable name.
VC has a __COUNTER__
macro which gives uniqueness, it appears that GCC has an equivalent macro with the same name from version 4.3.5 (although I can't at the moment find a live link).
这篇关于C中的未命名参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!