这将适用于多个编译器(以及不同版本的编译器)。
标题“推”
#if defined(__clang__)
# pragma clang diagnostic push
#endif
#if defined(_MSC_VER)
# pragma warning(push)
#endif
#if defined(YOUR_FAVORITE_COMPILER)
# pragma your compiler push warning
#endif
标题“流行”
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
一些警告
#if defined(__clang__)
# pragma clang diagnostic ignored "-Wunused-parameter"
# pragma clang diagnostic ignored "-Wunused-variable"
# if __has_warning("-Wnew-special-warning")
# pragma clang diagnostic ignored "-Wnew-special-warning"
# endif
#endif
#if defined(_MSC_VER)
# pragma warning(disable: 4100) // unreferenced formal parameter
# if _MSC_VER > _MSC_SOME_VERSION
# pragma warning(disable: xxxx) // disable one more for special version
# endif
#endif
用法
// This code reports warnings
// ...
#include <ignore_compiler_warning/push>
#include <ignore_compiler_warning/warning_type_1>
#include <ignore_compiler_warning/warning_type_2>
#include <ignore_compiler_warning/warning_type_3>
// This code ignores warnings type_{1,2,3}
// ...
#include <ignore_compiler_warning/pop>
// Back to reporting warnings
// ...
另外包括警卫可以检查没有双推/弹出/禁用警告编译指示。
更新