gpt4 book ai didi

c++ - 如果已实现,请使用 pragma 禁用警告

转载 作者:行者123 更新时间:2023-11-30 03:33:24 24 4
gpt4 key购买 nike

Clang 最近实现了一个烦人的警告。如果我使用 #pragma clang diagnostic ignored 禁用它,那么旧的 Clang 版本将发出“未知警告组”警告。

是否有某种方法可以测试是否实现了警告?

最佳答案

最新版本的 Clang 实现了 __has_warning 功能检查宏。由于 Clang 仅使用一个警告标志池来模拟 GCC(反之亦然),因此使用功能检查内省(introspection)来针对 GCC 进行编码是合理的:

#if __GNUC__ && defined( __has_warning )
# if __has_warning( "-Wwhatever" )
# define SUPPRESSING
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wwhatever"
# endif
#endif

// Code that trips warning

#ifdef SUPPRESSING
# undef SUPPRESSING
# pragma GCC diagnostic pop
#endif

这是一个有点麻烦的copypasta。使用包含文件可以避免这种情况,如下所示:

#define SUPPRESS_WARNING "-Wwhatever"
#include "suppress_warning.h"

// Code that trips warning

#include "unsuppress_warning.h"

suppress_warning.h 有点棘手,因为 __has_warning#pragma 不接受宏作为参数。所以,从Github得到它或者这个 Wandbox demo .

关于c++ - 如果已实现,请使用 pragma 禁用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42979987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com