用于 C 和 C++ 函数签名的可移植 UNUSED 参数宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7090998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:27:54  来源:igfitidea点击:

Portable UNUSED parameter macro used on function signature for C and C++

c++cmacroswarnings

提问by ?imon Tóth

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 independent way of implementing an UNUSED macro in C/C++

在 C/C++ 中实现 UNUSED 宏的通用编译器独立方式

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

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

void callback(int UNUSED(some_useless_stuff)) {}

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.

编辑:对于那些无法理解标记如何工作​​的人:我想要一个适用于 C 和 C++ 的解决方案。这就是为什么这个问题被标记为 C 和 C++,这就是为什么只使用 C++ 的解决方案是不可接受的。

采纳答案by ?imon Tóth

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.

使用:(#define UNUSED(x) __pragma(warning(suppress:4100)) x在评论中提到),在 MSVC 上编译 C 可能是必要的,但这是一个如此奇怪的组合,我最后没有包含它。

回答by Matt Clarkson

The way I do it is like this:

我的做法是这样的:

#define UNUSED(x) (void)(x)
void foo(const int i) {
    UNUSED(i);
}

I've not had a problem with that in Visual Studio, Intel, gccand clang.

我在 Visual Studio、Intelgccclang.

The other option is to just comment out the parameter:

另一种选择是注释掉参数:

void foo(const int /*i*/) {
  // When we need to use `i` we can just uncomment it.
}

回答by doc_ds

Just one small thing, better using __attribute__((__unused__))as __attribute__((unused)), because unused could be somewhere defined as macro, personally I had a few issues with this situation.

只是一件小事,最好使用__attribute__((__unused__))as __attribute__((unused)),因为未使用的地方可以定义为宏,我个人对这种情况有一些问题。

But the trick I'm using is, which I found more readable is:

但我使用的技巧是,我发现更具可读性的是:

#define UNUSED(x) (void)x;

#define UNUSED(x) (void)x;

It works however only for the variables, and arguments of the methods, but not for the function itself.

然而,它仅适用于方法的变量和参数,而不适用于函数本身。

回答by Steve-o

Across many compilers I have used the following, excluding support for lint.

在许多编译器中,我使用了以下内容,不包括对 lint 的支持。

#if (__GNUC__ > 2) || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
#       define PGM_GNUC_UNUSED         __attribute__((__unused__))
#else
#       define PGM_GNUC_UNUSED
#endif

Tested compilers: GCC, Clang, EKOPath, Intel C Compiler / Composer XE, MinGW32 on Cygwin / Linux / MSYS, MinGW-w64 on Cygwin / Linux, Sun ONE Studio / Oracle Solaris Studio, Visual Studio 2008 / 2010.

测试编译器:GCC、Clang、EKOPath、Intel C Compiler / Composer XE、Cygwin / Linux / MSYS 上的 MinGW32、Cygwin / Linux 上的 MinGW-w64、Sun ONE Studio / Oracle Solaris Studio、Visual Studio 2008 / 2010。

Example usage:

用法示例:

pgm_tsc_init (
        PGM_GNUC_UNUSED pgm_error_t**   error
        )
{
...
}

PGMis the standard prefix for this C based project. GNUCis the convention from GLib for this attribute.

PGM是这个基于 C 的项目的标准前缀。 GNUC是 GLib 对此属性的约定。

I think one compile warns about __attribute__in certain circumstances but certainly no error.

我认为__attribute__在某些情况下编译会发出警告,但肯定没有错误。