【发布时间】:2010-08-15 11:19:04
【问题描述】:
在 C 中,与 C++ 不同,函数定义的所有参数都必须命名。
我没有使用(void)a 消除“未使用的参数”错误,或者公开使用__attribute__((unused)),而是创建了以下宏:
#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__)
这样使用
void blah(char const *UNUSED(path)) {}
有什么方法可以保证一个唯一的“虚拟”变量名(显然LINE 和__func__ 不能删除它),或者根本忽略命名未使用的变量?
更新0
最终使用的代码是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
它是这样使用的:
void blah(char const *OMIT_PARAM) {}
并避免未使用的参数、未命名的参数警告,并保证它不会破坏其他变量名。
【问题讨论】:
-
链接断开 [2013-08-26]。 :|
-
@unwind:我想我现在修好了。