如何将boost :: spirit :: qi :: lexeme的属性转换为std :: string?

Bra*_*den 6 c++ boost boost-spirit boost-phoenix boost-spirit-qi

考虑:

struct s {
  AttrType f(const std::string &);
};
Run Code Online (Sandbox Code Playgroud)

...以及r具有属性的规则AttrType:

template <typename Signature> using rule_t =
  boost::spirit::qi::rule<Iterator,
                          Signature,
                          boost::spirit::qi::standard::space_type>;

rule_t<AttrType()> r;

r = lexeme[alnum >> +(alnum | char_('.') | char_('_'))][
      _val = boost::phoenix::bind(&s::f, s_inst, _1)
    ];
Run Code Online (Sandbox Code Playgroud)

编译时(使用clang),我收到此错误消息:

boost/phoenix/bind/detail/preprocessed/member_function_ptr_10.hpp:28:72: error: no viable conversion from
      'boost::fusion::vector2<char, std::__1::vector<char, std::__1::allocator<char> > >' to 'const std::__1::basic_string<char>'
                return (BOOST_PROTO_GET_POINTER(class_type, obj)->*fp)(a0);
                                                                       ^~
Run Code Online (Sandbox Code Playgroud)

我的印象是问题是占位符变量的类型_1.是否有简洁的方法将lexeme属性转换std::string为此目的?

如果我插入属性类型为的附加规则std::string,则编译:

rule_t<std::string()> r_str;

r = r_str[boost::phoenix::bind(&s::f, s_inst, _1)];
r_str = lexeme[alnum >> +(alnum | char_('.') | char_('_'))];
Run Code Online (Sandbox Code Playgroud)

......但这看起来有点尴尬.有没有更好的办法?

seh*_*ehe 4

您可以使用qi::as_string[](如果存在合适的自动转换,这会将属性强制转换字符串)。

或者,您可以使用qi::raw[]它公开源迭代器范围。这会自动转化为std::string属性。这里的好处是输入可以不改变地反映(例如qi::raw[ qi::int_ >> ';' >> qi::double_ ]将工作

在你的情况下,你可能可以使用as_string[]. 但您也可以修复参数以采取std::vector<char> const&


最后,您可以使用它attr_cast<>来实现与单独规则完全相同的效果qi::rule<>(但不使用单独规则:)),但为了提高效率,我不推荐它,因为旧版本的 boost 在这个设施中存在错误。