Apple Clang 和 numeric_limits<unsigned __int128>::max() 是 0?
Apple Clang and numeric_limits<unsigned __int128>::max() is 0?
我正试图找出解决numeric_limits<T>::max()
返回0而不是最大值的明显错误的最佳方法。
首先,测试程序:
$ cat test.cxx
#include <iostream>
#include <limits>
int main(int argc, char* argv[])
{
#if (__SIZEOF_INT128__ >= 16)
std::cout << "__int128 is available" << std::endl;
#else
std::cout << "__int128 is not available" << std::endl;
#endif
unsigned __int128 m = std::numeric_limits<unsigned __int128>::max();
if (m == 0)
std::cout << "numeric_limits<unsigned __int128>::max() is 0" << std::endl;
else
std::cout << "numeric_limits<unsigned __int128>::max() is not 0" << std::endl;
return 0;
}
__SIZEOF_INT128__ >= 16
测试来自于对128位整数的GCC邮件列表的讨论——无意义的文档?。
结果:
$ c++ -Wall test.cxx -o test.exe
$ ./test.exe
__int128 is available
numeric_limits<unsigned __int128>::max() is 0
苹果公司也放弃了该平台和工具,因此错误报告无法解决问题。
我们如何解决这个问题?
我不知道如何继续。为了解决代码中的问题,与上面的最小示例相反,我们确实需要覆盖std
命名空间中的函数。但是不允许重写std
中的函数。
下面是一个示例,说明为什么它在实际代码中存在问题:
template<class T1, class T2>
T1 Add(const T1& t1, const T2& t2)
{
if (std::numeric_limits<T1>::max() - t2 > t1)
throw std::runtime_error("overflow");
return t1 + t2;
}
在上面的代码中,我们必须为T1 = __int128
和T2
的每一个组合提供完全的专业化。这是不现实的。
问题机器上的编译器版本:
$ c++ --version
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
然而,跳到非苹果的测试机上会产生预期的结果:
$ clang++-3.5 --version
Debian clang version 3.5.0-10 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
$ clang++-3.5 -Wall test.cxx -o test.exe
$ ./test.exe
__int128 is available
numeric_limits<unsigned __int128>::max() is not 0
写入notstd::numeric_limits<T>:std::numeric_limits<T>
为有错误的T
专门化if,用正确的行为重载static max()
(以及其他任何东西)。
在Add
中使用notstd::numeric_limits
。
或者使用更新的编译器和/或标准库。
相关文章:
- <streamsize>C++ 中 numeric_limits::max() 的值
- 黑客级别的Mini-Max Sum
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- EASTL矢量<向量<int>>连续的
- 关于 std::min, std::max 中的比较运算符的混淆
- 找到一种有效的方法,在 2 个巨大的缓冲区上执行 MAX,每字节字节
- std::max() 函数与定点实现的比较中的问题
- 使用 CImg 库的 std::min 和 std::max 的编译问题
- 在 3ds Max 中更新进度条后,环境和效果 UI 不刷新
- 从自定义类获取对象向量中的 max 元素
- 如何在C++中递归地找到max元素的索引?
- SFINAE 与 numeric_limits<T>::max() 在 MSVC2017 上
- 这句话是什么意思 - " vector<long long> distance(n, std::numeric_limits<long long>::max()); "?
- 在f2c.h文件中定义min()max()宏时出错
- c++curl返回413请求实体过大,但是post大小远小于max大小
- 在枚举类型上使用std::max是不是一种糟糕的做法
- 为什么Visual Studio 2019不支持用于减少Openmp的关键字"max"?
- 没有函数模板的实例"max"与参数列表参数类型匹配(int、int)
- 如何在 C++11 中将 std::max 与自定义比较器一起使用?
- 为什么在这种情况下不能将 Max 用作函数?