如何使用 boost::xpressive static 引用语义操作中的可选子匹配?

发布于 2024-12-29 03:34:34 字数 1307 浏览 0 评论 0原文

我有一个 boost xpressive sregex 和语义操作,相当于以下内容:

Rule = ('[' >> (s1=!(set=')',']','>')))[some_op(as<std::string>(s1))];

以前我在 boost 1.43 上使用它,没有任何问题。我最近不得不升级到更新的版本,现在遇到了以下问题。 在 boost 1.48 上,当子匹配不匹配任何内容(因为它是可选的)时,当 as 在空 s1< 上执行时,语义操作会抛出 bad_lexical_cast 异常。 /代码> 对象。

我怎样才能解决这个问题,它之前工作过只是巧合吗?我应该使用一些更好更安全的方法来做到这一点?或者这只是例如 lexical_cast 代码中的一些更改现在破坏了 xpressive

其他信息

我已经通过更改 regex_actions.hpp 中的以下内容来暂时解决实际问题:

template<typename T>
struct as
{
    BOOST_PROTO_CALLABLE()
    typedef T result_type;

    template<typename Value>
    T operator()(Value const &val) const
    {
        return lexical_cast<T>(val);
    }
};

Into:

template<typename T>
struct as
{
    BOOST_PROTO_CALLABLE()
    typedef T result_type;

    template<typename Value>
    T operator()(Value const &val) const
    {
        if(val.first==val.second)
        {
            return T();
        }
        else
        {
            return lexical_cast<T>(val);
        }
    }
};

这让我相信也许这是需要在 xpressive 本身中修复的问题。然而,我还没有 100% 确信这不是我做错的事情,任何对 xpressive 有更多了解的人对此有一些见解吗?

I have a boost xpressive sregex and semantic action that is equivalent to the following:

Rule = ('[' >> (s1=!(set=')',']','>')))[some_op(as<std::string>(s1))];

Previously I was using this on boost 1.43 without any problems. I have had to upgrade to a newer boost recently and now I run into the following issue.
On boost 1.48 when the submatch does not match anything (as it is optional) a bad_lexical_cast exception is thrown by the semantic action when as executes on the empty s1 object.

How can I fix this problem, is it just a coincidence that it worked before and there is some better safer way I should be using to do this? Or is this just e.g. some change in the lexical_cast code that now breaks xpressive?

Additional information

I have managed to temporarily solve the actual problem by changing the following in regex_actions.hpp:

template<typename T>
struct as
{
    BOOST_PROTO_CALLABLE()
    typedef T result_type;

    template<typename Value>
    T operator()(Value const &val) const
    {
        return lexical_cast<T>(val);
    }
};

Into:

template<typename T>
struct as
{
    BOOST_PROTO_CALLABLE()
    typedef T result_type;

    template<typename Value>
    T operator()(Value const &val) const
    {
        if(val.first==val.second)
        {
            return T();
        }
        else
        {
            return lexical_cast<T>(val);
        }
    }
};

This leads me to believe that perhaps this is something that needs to be fixed in xpressive itself. However I am not 100% convinced yet that it's not something I am doing wrong on my side, anyone with a bit more knowledge on xpressive have some insight into this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丿*梦醉红颜 2025-01-05 03:34:34

在进行了更多调查并与 Xpressive 的作者交谈后,我得出的结论是,这要么是 lexical_cast 行为的回归,要么是lexical_cast 方式中的错误< code>xpressive 期望 lexical_cast 能够正常工作。

After investigating a bit more and speaking with the Author of Xpressive I have come to the conclusion that this is either a regression in the behaviour of lexical_cast or a bug in the way xpressive expected lexical_cast to behave.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文