如何使用 boost::spirit::x3 测试字符串的有效双重内容?

How to test string for valid double content with boost::spirit::x3?

我正在尝试确定给定字符串是否是有效的双精度表示形式。 我使用的代码如下所示:

bool testNumeric(const std::string& s)
{
    try
    {
        const auto doubleParser = boost::spirit::x3::double_;
        auto iter = s.begin();
        auto end_iter = s.end();
        double result = 0.;
        boost::spirit::x3::parse(iter, end_iter, doubleParser, result);
        return iter == end_iter;
    }
    catch (...)
    {
        return false;
    }
}

我对生成的 double 不感兴趣(暂时)。 如果我给这个函数一个输入“1e10000000”,这对于双精度来说显然太大了,程序会失败并出现一个断言 (BOOST_ASSERT)。这可以以某种方式更改为通过 return 代码失败或抛出我可以捕获的异常吗? 还是我必须用 spirit::x3 编写自己的双重解析器?

最后,我创建了一个自定义解析方法,它首先在 double 的字符串表示的指数部分(如果存在)使用 boost::spirit::x3::int_,如果指数不满足,则使用 returns double 类型的边界。然后我在有效字符串上调用 boost::spirit::x3::double_ 解析器。