xcode 用于静音未使用变量警告的跨平台宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12198449/
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-09-15 01:22:32  来源:igfitidea点击:

cross platform macro for silencing unused variables warning

c++xcodevisual-c++clangsuppress-warnings

提问by Jean-Denis Muys

In porting a large piece of C++ code from Visual Studio (2008) to Xcode (4.4+), I encounter lines such as:

在将一大段 C++ 代码从 Visual Studio (2008) 移植到 Xcode (4.4+) 时,我遇到了如下几行:

UNUSED_ALWAYS(someVar);

the UNUSED_ALWAYS(x)(through UNUSED(x)) macro expands to xwhich seems to silence Visual C++ just fine. It's not enough for Clang however.

UNUSED_ALWAYS(x)(通过UNUSED(x))宏扩展到x这似乎沉默的Visual C ++就好了。然而,这对 Clang 来说还不够。

With Clang, I usually use the #pragma unused xdirective.

对于 Clang,我通常使用#pragma unused x指令。

The UNUSED_ALWAYSand UNUSEDmacros are defined in an artificial windows.hheader which I control that contains a number of utilities to help Xcode compile Windows stuff.

UNUSED_ALWAYSUNUSED宏在人工定义的windows.h头,我控制包含了一些工具来帮助Xcode的编译的Windows的东西。

Is there a way to define UNUSED(x)to expand to #pragma unused x? I tried this, which Clang fails to accept:

有没有办法定义UNUSED(x)扩展到#pragma unused x?我试过这个,但 Clang 无法接受:

#define UNUSED(x) #pragma unused(x)

I also tried:

我也试过:

#define UNUSED(x) (void)(x)

Which seems to work. Did I miss anything?

这似乎有效。我错过了什么吗?

采纳答案by justin

Yup - you can use this approach for GCC and Clang:

是的 - 您可以将这种方法用于 GCC 和 Clang:

#define MON_Internal_UnusedStringify(macro_arg_string_literal) #macro_arg_string_literal

#define MONUnusedParameter(macro_arg_parameter) _Pragma(MON_Internal_UnusedStringify(unused(macro_arg_parameter)))

although mine did have the (void)approach defined for clang, it appears that Clang now supports the stringify and _Pragmaapproach above. _Pragmais C99.

尽管我的确实(void)为 clang 定义了方法,但似乎 Clang 现在支持_Pragma上面的 stringify 和方法。_Pragma是C99。

回答by Christian Stieber

(void)x;

is fine; has always worked for me. You can't usually expand a macro to a #pragma, although there is usually a slightly different pragma syntax that can be generated from a macro (_Pragma on gcc and clang, __pragma on VisualC++).

很好;一直对我来说有效。您通常不能将宏扩展为 #pragma,尽管通常可以从宏(gcc 和 clang 上的 _Pragma,VisualC++ 上的 __pragma)生成略有不同的编译指示语法。

Still, I don't actually need the (void)x anymore in C++, since you can simply not give a name to a function parameter to indicate that you don't use it:

尽管如此,我实际上不再需要 C++ 中的 (void)x,因为您不能简单地为函数参数命名以表明您不使用它:

int Example(int, int b, int)
{
   ... /* only uses b */
}

works perfectly fine.

工作得很好。

回答by Ajay

#defineand #pragmaboth are preprocessor directives. You cannot define one macro to expand as preprocessor directive. Following would be incorrect:

#define并且#pragma两者都是预处理器指令。您不能定义一个宏来扩展为预处理器指令。以下将是不正确的:

#define MY_MACRO   #if _WIN32 

MY_MACRO cannot expand to #if _WIN32for the compiler.

MY_MACRO 不能扩展#if _WIN32编译器

Your best bet is to define your own macro:

最好的办法是定义自己的宏:

#define UNUSED(_var) _var