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
cross platform macro for silencing unused variables warning
提问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 x
which 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 x
directive.
对于 Clang,我通常使用#pragma unused x
指令。
The UNUSED_ALWAYS
and UNUSED
macros are defined in an artificial windows.h
header which I control that contains a number of utilities to help Xcode compile Windows stuff.
在UNUSED_ALWAYS
和UNUSED
宏在人工定义的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 _Pragma
approach above. _Pragma
is 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
#define
and #pragma
both 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 _WIN32
for the compiler.
MY_MACRO 不能扩展#if _WIN32
为编译器。
Your best bet is to define your own macro:
最好的办法是定义自己的宏:
#define UNUSED(_var) _var