__uint128_t 不适用于 Clang 和 libstdc++

__uint128_t not working with Clang and libstdc++

考虑以下用于 __uint128_t

的 GNU 扩展类型特征的小测试程序
#include <limits>
#include <type_traits>

using uint128_t = __uint128_t;

int main()
{
    static_assert(std::is_unsigned<uint128_t>{}, ""); 
    static_assert(std::is_integral<uint128_t>{}, "");
    static_assert(std::numeric_limits<uint128_t>::digits == 128, "");
}

这适用于 g++ 和 libstdc++ (working example) and for clang++ and libc++ (working example), but not for the combination clang++ and libstdc++ (failing example)。

请注意,在所有 3 种情况下,我都使用了 -std=gnu++1z 标志。

问题:哪种命令行参数组合可以成功编译我的 clang++ 测试程序和 libstdc++?

在非标准 -std=gnu++* 模式下,GCC 预定义了两个宏,libstdc++ 使用它们来提供额外的类型特征:

$ g++ -std=gnu++1z -c -E -dM -xc++ /dev/null | grep INT_N
#define __GLIBCXX_BITSIZE_INT_N_0 128
#define __GLIBCXX_TYPE_INT_N_0 __int128

clang 没有定义这些。通过在命令行上传递 -D__GLIBCXX_TYPE_INT_N_0=__int128-D__GLIBCXX_BITSIZE_INT_N_0=128 手动定义它们,使您的测试用例与 clang 一起工作。

在clang中添加这些本身就是https://llvm.org/bugs/show_bug.cgi?id=23156,它被标记为已修复,但我不确定在哪个版本中。我在本地安装了3.8.1,但它不起作用,但我没有检查3.9.0.