boost :: multiprecision从integer转换为cpp_dec_float编译错误

时间:2014-09-10 14:31:34

标签: c++ boost

我在将某些代码移动到服务器时遇到问题。

问题的最小工作示例是以下代码(文件名mp2.cpp):

#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>

using namespace std;
using namespace boost::multiprecision;

int main() {
    uint128_t a = 1;
    uint128_t b = 2;
    cpp_dec_float_50 fa = static_cast<cpp_dec_float_50>(a);
    cpp_dec_float_50 fb = static_cast<cpp_dec_float_50>(b);
    cout << fa / fb << endl;
    return 0;
}

编译并按预期工作,在我的开发机器上的标准输出上写0.5(g ++ 4.6.3,boost 1.55,32位CPU)。

对于相同的代码,g ++在服务器上无法编译时出现此错误:

In file included from /usr/local/include/boost/multiprecision/number.hpp:22:0,
                 from /usr/local/include/boost/multiprecision/cpp_int.hpp:12,
                 from mp2.cpp:1:
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp: In instantiation of 'void boost::multiprecision::detail::generic_interconvert(To&, const From&, const mpl_::int_<1>&, const mpl_::int_<0>&) [with To = boost::multiprecision::backends::cpp_dec_float<50u>; From = boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>]':
/usr/local/include/boost/multiprecision/number.hpp:108:7:   required from 'boost::multiprecision::number<Backend, ExpressionTemplates>::number(const boost::multiprecision::number<Other, ET>&, typename boost::enable_if_c<(! boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>::value)>::type*) [with Other = boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>; boost::multiprecision::expression_template_option ET = (boost::multiprecision::expression_template_option)0u; Backend = boost::multiprecision::backends::cpp_dec_float<50u>; boost::multiprecision::expression_template_option ExpressionTemplates = (boost::multiprecision::expression_template_option)1u; typename boost::enable_if_c<(! boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>::value)>::type = void]'
mp2.cpp:11:55:   required from here
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp:43:4: error: no matching function for call to 'eval_is_zero(boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>&)'
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp:43:4: note: candidates are:
In file included from mp2.cpp:2:0:
/usr/local/include/boost/multiprecision/cpp_dec_float.hpp:2905:13: note: template<unsigned int Digits10, class ExponentType, class Allocator> bool boost::multiprecision::backends::eval_is_zero(const boost::multiprecision::backends::cpp_dec_float<Digits10, ExponentType, Allocator>&)
/usr/local/include/boost/multiprecision/cpp_dec_float.hpp:2905:13: note:   template argument deduction/substitution failed:
In file included from /usr/local/include/boost/multiprecision/number.hpp:22:0,
                 from /usr/local/include/boost/multiprecision/cpp_int.hpp:12,
                 from mp2.cpp:1:
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp:43:4: note:   'boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>' is not derived from 'const boost::multiprecision::backends::cpp_dec_float<Digits10, ExponentType, Allocator>'
In file included from /usr/local/include/boost/multiprecision/cpp_int.hpp:1787:0,
                 from mp2.cpp:1:
/usr/local/include/boost/multiprecision/cpp_int/misc.hpp:94:4: note: template<unsigned int MinBits1, unsigned int MaxBits1, boost::multiprecision::cpp_integer_type SignType1, boost::multiprecision::cpp_int_check_type Checked1, class Allocator1> typename boost::enable_if_c<(! boost::multiprecision::backends::is_trivial_cpp_int<boost::multiprecision::backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value), bool>::type boost::multiprecision::backends::eval_is_zero(const boost::multiprecision::backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>&)
/usr/local/include/boost/multiprecision/cpp_int/misc.hpp:94:4: note:   template argument deduction/substitution failed:
/usr/local/include/boost/multiprecision/cpp_int/misc.hpp: In substitution of 'template<unsigned int MinBits1, unsigned int MaxBits1, boost::multiprecision::cpp_integer_type SignType1, boost::multiprecision::cpp_int_check_type Checked1, class Allocator1> typename boost::enable_if_c<(! boost::multiprecision::backends::is_trivial_cpp_int<boost::multiprecision::backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value), bool>::type boost::multiprecision::backends::eval_is_zero(const boost::multiprecision::backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>&) [with unsigned int MinBits1 = 128u; unsigned int MaxBits1 = 128u; boost::multiprecision::cpp_integer_type SignType1 = (boost::multiprecision::cpp_integer_type)0u; boost::multiprecision::cpp_int_check_type Checked1 = (boost::multiprecision::cpp_int_check_type)0u; Allocator1 = void]':
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp:43:4:   required from 'void boost::multiprecision::detail::generic_interconvert(To&, const From&, const mpl_::int_<1>&, const mpl_::int_<0>&) [with To = boost::multiprecision::backends::cpp_dec_float<50u>; From = boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>]'
/usr/local/include/boost/multiprecision/number.hpp:108:7:   required from 'boost::multiprecision::number<Backend, ExpressionTemplates>::number(const boost::multiprecision::number<Other, ET>&, typename boost::enable_if_c<(! boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>::value)>::type*) [with Other = boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>; boost::multiprecision::expression_template_option ET = (boost::multiprecision::expression_template_option)0u; Backend = boost::multiprecision::backends::cpp_dec_float<50u>; boost::multiprecision::expression_template_option ExpressionTemplates = (boost::multiprecision::expression_template_option)1u; typename boost::enable_if_c<(! boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>::value)>::type = void]'
mp2.cpp:11:55:   required from here
/usr/local/include/boost/multiprecision/cpp_int/misc.hpp:94:4: error: no type named 'type' in 'struct boost::enable_if_c<false, bool>'
In file included from /usr/local/include/boost/multiprecision/cpp_int.hpp:18:0,
                 from mp2.cpp:1:
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp: In instantiation of 'void boost::multiprecision::detail::generic_interconvert(To&, const From&, const mpl_::int_<1>&, const mpl_::int_<0>&) [with To = boost::multiprecision::backends::cpp_dec_float<50u>; From = boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>]':
/usr/local/include/boost/multiprecision/number.hpp:108:7:   required from 'boost::multiprecision::number<Backend, ExpressionTemplates>::number(const boost::multiprecision::number<Other, ET>&, typename boost::enable_if_c<(! boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>::value)>::type*) [with Other = boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>; boost::multiprecision::expression_template_option ET = (boost::multiprecision::expression_template_option)0u; Backend = boost::multiprecision::backends::cpp_dec_float<50u>; boost::multiprecision::expression_template_option ExpressionTemplates = (boost::multiprecision::expression_template_option)1u; typename boost::enable_if_c<(! boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>::value)>::type = void]'
mp2.cpp:11:55:   required from here
/usr/local/include/boost/multiprecision/rational_adaptor.hpp:252:13: note: template<class IntBackend> bool boost::multiprecision::backends::eval_is_zero(const boost::multiprecision::backends::rational_adaptor<IntBackend>&)
/usr/local/include/boost/multiprecision/rational_adaptor.hpp:252:13: note:   template argument deduction/substitution failed:
In file included from /usr/local/include/boost/multiprecision/number.hpp:22:0,
                 from /usr/local/include/boost/multiprecision/cpp_int.hpp:12,
                 from mp2.cpp:1:
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp:43:4: note:   'boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>' is not derived from 'const boost::multiprecision::backends::rational_adaptor<IntBackend>'

服务器在64位CPU上运行g ++ 4.7.2和boost 1.56。我已经安装了相同的g ++和boost版本来查看这是否是问题,但事实并非如此。

通过对源的实验,每当我尝试将整数转换为浮点数时,就会出现问题。但是,如提到in the documentation,这是正确的方法。

有谁知道问题是什么,以及如何解决?

1 个答案:

答案 0 :(得分:0)

我不知道这个库,但似乎问题来自x86-64架构的bug(?)。

应该注意:

  1. 我的64位操作系统也有同样的问题(Linux Mint 16 - g ++ 4.8.1 - boost 1.53)。
  2. 作为一种变通方法,您可以使用-m32标志将代码交叉编译为32位二进制文​​件(已成功测试)。
  3. 使用uint128_t的cpp_int intead消失此问题。
  4. 奇怪的是,使用以下自定义类型没有编译错误:

    typedef number<cpp_int_backend<128, 128, signed_magnitude, unchecked, std::allocator<limb_type> > > t128_custom;
    

    将此分配器替换为void(由uint128_t定义)或使用usigned_magnitude时会出现问题。

  5. 也许您应该联系推广社区以获取更多信息。