如何抑制“ISO C++ 不支持‘__int128’警告?

How do I suppress the "ISO C++ does not support ‘__int128’ warning?

我正在使用 gcc、-Wall -Wextra -Wpedantic 开关和非扩展标准集(比如 -std=c++14)编译我的代码。但是 - 我想对该规则进行例外处理并使用 __int128。这让我收到警告:

warning: ISO C++ does not support ‘__int128’ for ‘hge’ [-Wpedantic]

我可以取消有关 __int128 的特定警告吗?或者,我可以在使用这种类型之前和之后暂时抑制 -Wpedantic 吗?

如果我们参考 documentation for -Wpedantic 我们可以注意到以下几点:

Pedantic warnings are also disabled in the expression that follows __extension__.

快速 bit of experimentation 表明这允许人们按预期定义变量,即使在标志下也是如此:

__extension__ __int128 hge{};

当然,如果我们打算经常使用这种类型,那当然会很麻烦。使这不那么棘手的方法是使用类型别名。尽管我们在这里需要小心,__extension__ 属性必须在 整个 声明之前:

__extension__ typedef __int128 int128;

您可以看到它正在运行 here


另一种方法,也是遵循您最初思路的方法,是在类型别名周围使用诊断编译指示:

namespace my_gcc_ints {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
    using int128 = __int128;
#pragma GCC diagnostic pop
}

其中还有works rather well.