跨平台宏,用于沉默未使用的变量警告 [英] cross platform macro for silencing unused variables warning

查看:250
本文介绍了跨平台宏,用于沉默未使用的变量警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  UNUSED_ALWAYS(someVar); 

UNUSED_ALWAYS(x) code> UNUSED(x))宏扩展为 x 这似乎使Visual C ++沉默。



对于Clang,我通常使用 #pragma未使用的x 指令。 p>

UNUSED_ALWAYS UNUSED 宏定义在人为 windows.h 头,我控制它包含一些实用程序来帮助Xcode编译Windows的东西。



方法定义 UNUSED(x)展开为 #pragma未使用x ?我试过这个,Clang无法接受:

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

我也尝试过:

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

解决方案

Yup - 您可以对GCC和Clang使用此方法:



#define MON_Internal_UnusedStringify(macro_arg_string_literal)#macro_arg_string_literal

#define MONUnusedParameter(macro_arg_parameter)_Pragma(MON_Internal_UnusedStringify $ b

虽然我们已经定义了(void)对于clang,看起来Clang现在支持stringify和 _Pragma 方法。 _Pragma 为C99。


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

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.

With Clang, I usually use the #pragma unused x directive.

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.

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

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

I also tried:

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

Which seems to work. Did I miss anything?

解决方案

Yup - you can use this approach for GCC and 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.

这篇关于跨平台宏,用于沉默未使用的变量警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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