简体   繁体   English

使用#pragma和#ifdef的多行宏

[英]Macros over multilines using #pragma and #ifdef

I'm a bit new to macros so go easy, but we have an external library that generates loads of warnings when compiled under windows (its not so bad on linux). 我对宏有点新,所以简单一点,但我们有一个外部库,在windows下编译时会产生大量警告(在linux上不是那么糟糕)。 Its a header only library so I can't just turn the warnings off for the whole library, but I can disable each section that is generating the warnings (which is a bit tedius) 它只是一个标题库,所以我不能只关闭整个库的警告,但我可以禁用每个生成警告的部分(这有点tedius)

So I was wondering if it is possible to create a macro, so that instead of typing the following below, i could do it in a couple of lines. 所以我想知道是否有可能创建一个宏,所以我可以用几行来代替下面输入以下代码。

#ifdef _WIN32
#pragma warning (push)
#pragma warning(disable : 4355) // 'this' used in base member initialize list
#endif
    code that generates warning
#ifdef _WIN32
#pragma warning (pop)
#endif

However, when i try and create the macro such as below 但是,当我尝试创建如下的宏时

// Disable a warning on win32 platform
// You must call DISABLE_WIN32_PRAGMA_WARN_END afterwards
#define DISABLE_WIN32_PRAGMA_WARN (nnn) \
#ifdef _WIN32 \
#pragma warning (push) \
#pragma warning(disable : nnn ) \
#endif

#define DISABLE_WIN32_PRAGMA_WARN_END \
#ifdef _WIN32 \
#pragma warning (pop) \
#endif

However I get the following errors when compiling using VS2012 但是在使用VS2012进行编译时出现以下错误

error C2121: '#' : invalid character : possibly the result of a macro expansion
error C2065: 'nnn' : undeclared identifier
error C2351: obsolete C++ constructor initialization syntax
error C2612: trailing 'identifier' illegal in base/member initializer list
#ifdef _WIN32

// Disable a warning on win32 platform
// You must call DISABLE_WIN32_PRAGMA_WARN_END afterwards
#define DISABLE_WIN32_PRAGMA_WARN(nnn) \
__pragma (warning (push)) \
__pragma (warning(disable : nnn)) \

#define DISABLE_WIN32_PRAGMA_WARN_END \
__pragma (warning (pop)) \

#else

#define DISABLE_WIN32_PRAGMA_WARN(nnn)
#define DISABLE_WIN32_PRAGMA_WARN_END

#endif

You can define the prologue and epilogue in a header files say "warning_arrest_prologue.h" and "warning_arrest_epilogue.h" which will contain, 您可以在头文件中定义序言和结尾,例如“warning_arrest_prologue.h”和“warning_arrest_epilogue.h”,其中包含

#ifdef _WIN32
#pragma warning (push)
#pragma warning(disable : 4355) // 'this' used in base member initialize list
#endif

and

#ifdef _WIN32
#pragma warning (pop)
#endif

respectively. 分别。 Then you could use it in the following way, 然后你可以用以下方式使用它,

#include "warning_arrest_prologue"

// code that generates warning

#include "warning_arrest_epilogue"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM