You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何捕获cpp_int抛出的boost::wrapexcept异常?

Boost 1.79 cpp_int::convert_to 异常捕获问题

问题背景

  • Boost版本从1.78升级到1.79后,cpp_int的convert_to<double>方法行为发生变更:转换超出double范围的超大数字时,新版本会抛出boost::wrapexcept<std::overflow_error>或std::domain_error异常;而旧版本会直接返回无穷大或std::numeric_limits<double>::max()。
  • 尝试捕获异常以维持旧代码行为,但示例代码无法捕获异常,程序直接终止,报错信息如下:

terminate called after throwing an instance of 'boost::wrapexceptstd::overflow_error' what(): Error in function float_next(double): Overflow Error

示例代码

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

int main() {
    std::string strNumber = "179769313486231590617005494896502488139538923424507473845653439431848569886227202866765261632299351819569917639009010788373365912036255753178371299382143631760131695224907130882552454362167933328609537509415576609030163673758148226168953269623548572115351901405836315903312675793605327103910016259918212890625";
    boost::multiprecision::cpp_int number(strNumber);

    try {
        std::cout << number.convert_to<double>() << "\n";
    } catch (boost::wrapexcept<std::overflow_error> &e) {
        std::cout << "boost exception caught" << "\n";
    } catch (...) {
        std::cout << "some exception caught" << "\n";
    }
    
    std::cout << "finished" << "\n";
}

排查结果

经确认,该问题属于Boost框架的bug:异常在被标记为noexcept的方法内部抛出,违反了C++异常处理规则,导致外层无法捕获异常,目前该问题已上报。

核心需求

捕获cpp_int转换为double时的溢出异常,返回std::numeric_limits<double>::max(),保持与Boost 1.78版本一致的行为逻辑。

内容的提问来源于stack exchange,提问作者MartinD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 15:50:40