Apple Clang和numeric_limits< unsigned __int128> :: max()为0? [英] Apple Clang and numeric_limits<unsigned __int128>::max() is 0?

查看:115
本文介绍了Apple Clang和numeric_limits< unsigned __int128> :: max()为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出解决明显错误的最佳方法,其中 numeric_limits< T> :: max()返回0而不是最大值。

I'm trying to figure out the best way to work around an apparent bug where numeric_limits<T>::max() returns 0 rather than the maximum value.

首先是测试程序:

$ 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 测试来自GCC邮件列表上 128位整数-荒谬的文档吗?

The __SIZEOF_INT128__ >= 16 test came from a discussion on the GCC mailing list at 128-bit integer - nonsensical documentation?.

结果是:

$ c++ -Wall test.cxx -o test.exe
$ ./test.exe
__int128 is available
numeric_limits<unsigned __int128>::max() is 0

Apple还放弃了平台和工具,因此出现了错误报告不会解决问题。

Apple has also abandoned the platform and tools, so a bug report won't get the problem fixed.

我们如何解决该问题?

我不确定如何继续。为了解决代码中的问题,与上面的最小示例相反,我们确实需要覆盖 std 名称空间中的函数。但是不允许在 std 中覆盖函数

I'm not sure how to proceed. To fix the problem in the code, as opposed to the minimal example above, we really need to override the function in the std namespace. But overriding a function in std is not allowed.

以下是在实际代码中为什么会出现问题的示例:

Here's an example of why its a problem in the real code:

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 是可以想象的。

In the code above, we have to provide a full specialization for every combination of T1 = __int128 and T2 imaginable. Its not realistic.

问题机器上的编译器版本:

The compiler version on the problematic machine:

$ 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

但是,跳跃到非Apple测试机上会产生预期的结果:

However, jumping over to a non-Apple test machine produces expected results:

$ 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 带有错误,超载 static具有正确行为的max()(以及其他任何东西)。

Specialize if for T which have a bug, overloading static max() (and whatever else) with the right behaviour.

使用 notstd :: numeric_limits 添加中的$ c>。

Use notstd::numeric_limits in Add.

或使用较新的编译器和/或标准库。

Or use a newer compiler and/or standard library.

这篇关于Apple Clang和numeric_limits&lt; unsigned __int128&gt; :: max()为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆