对C和C函数签名用于便携式未使用的参数宏观++ [英] Portable UNUSED parameter macro used on function signature for C and C++

查看:126
本文介绍了对C和C函数签名用于便携式未使用的参数宏观++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很感兴趣,为了消除未使用变量警告创建宏。

此问题描述了通过编写函数code内宏燮preSS的未使用的参数警告:

<一个href=\"http://stackoverflow.com/questions/4851075/universally-compiler-independant-way-of-implementing-an-unused-macro-in-c-c\">Universally用C实现一个未使用的宏编译器独立方式/ C ++

但我感兴趣的是可以在函数签名中使用的宏:

的回调(INT未使用(some_useless_stuff)){}

这就是我挖出来使用谷歌(

  #IFDEF未使用
#elif指令定义(__ GNUC__)
#定义未使用(X)UNUSED_ ##点¯x__attribute __((未使用))
#elif指令定义(__ LCLINT__)
#定义未使用(X)/ * @未使用@ * / X
#elif指令定义(__ CPLUSPLUS)
#定义未使用(X)
#其他
#定义未使用(x)x
#万一

这能进一步为其他编译器扩展?

编辑:对于那些谁不明白标注是如何工作的:我要为C和C ++的解决方案。这就是为什么这个问题被标记C和C ++,这就是为什么一个C ++唯一的解决办法是不行的。


解决方案

经过测试并按照意见,在问题中提到原来的版本竟然是不够好。

使用:的#define未使用(X)__pragma(警告(SUP preSS:4100))×(注释中提到的),可能有必要编译C在MSVC,但是这是这样一个奇怪的组合,那我不包括它到底。

I'm interested in creating a macro for eliminating the unused variable warning.

This question describes a way to suppress the unused parameter warning by writing a macro inside the function code:

Universally compiler independant way of implementing an UNUSED macro in C/C++

But I'm interested in a macro that can be used in the function signature:

void callback(int UNUSED(some_useless_stuff)) {}

This is what I dug out using Google (source)

#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#elif defined(__cplusplus)
# define UNUSED(x)
#else
# define UNUSED(x) x
#endif

Can this be further expanded for other compilers?

Edit: For those who can't understand how tagging works: I want a solution for both C and C++. That is why this question is tagged both C and C++ and that is why a C++ only solution is not acceptable.

解决方案

After testing and following the comments, the original version mentioned in the question turned out to be good enough.

Using: #define UNUSED(x) __pragma(warning(suppress:4100)) x (mentioned in comments), might be necessary for compiling C on MSVC, but that's such a weird combination, that I didn't include it in the end.

这篇关于对C和C函数签名用于便携式未使用的参数宏观++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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