1

私はマクロに少し慣れていないので簡単ですが、Windowsでコンパイルすると大量の警告を生成する外部ライブラリがあります(Linuxではそれほど悪くありません)。ヘッダーのみのライブラリなので、ライブラリ全体の警告をオフにすることはできませんが、警告を生成している各セクションを無効にすることはできます (これは少し面倒です)。

それで、マクロを作成できるかどうか疑問に思っていたので、以下に次のように入力する代わりに、数行でそれを実行できます。

#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

ただし、次のようなマクロを作成しようとすると

// 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

ただし、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
4

2 に答える 2

2
#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
于 2012-11-27T05:28:55.640 に答える
1

プロローグとエピローグは、「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

#ifdef _WIN32
#pragma warning (pop)
#endif

それぞれ。次に、次の方法で使用できます。

#include "warning_arrest_prologue"

// code that generates warning

#include "warning_arrest_epilogue"
于 2012-11-27T05:13:57.643 に答える