如何使用 boost::xpressive static 引用语义操作中的可选子匹配?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在进行了更多调查并与
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 oflexical_cast
or a bug in the wayxpressive
expectedlexical_cast
to behave.