2

在我之前的问题中,有人建议我的boost::spirit::x3解析器的性能可以通过解析为boost::string_view使用raw指令来提高。

但是,我很难编译它。这是我发现的:

  • 之前x3,必须专门assign_to_attribute_from_iterators(参见例如this SO answer)来处理raw指令。

  • x3现在改用move_tofree 函数(参见例如这个 SO answer)。

因此,我添加了一个move_to重载,如果我解析来自char*

#include <iostream>
#include <string>

#include <boost/utility/string_view.hpp>

namespace boost {
namespace spirit { namespace x3 { namespace traits {

template <typename It>
void move_to(It b, It e, boost::string_view& v)
{
    v = boost::string_view(b, std::size_t(std::distance(b,e)));
}

} } }

} // namespace boost

#include <boost/spirit/home/x3.hpp>

namespace parser
{
    namespace x3 = boost::spirit::x3;
    using x3::char_;
    using x3::raw;

    const auto str  = raw[ +~char_('_')] >> '_';
}

int main()
{
    std::string input = "hello world_";

    boost::string_view str; 
    parse(input.data(), input.data()+input.size(), parser::str, str);

    std::cout << str;
}

活生生的例子

但是,它不会编译:

1)如果我使用解析std::string::const_iterator

parse(input.cbegin(), input.cend(), parser::str, str);

的构造函数boost::string_view需要 aconst char*或 a std::string&

main.cpp:12:16: error: no matching function for call to 'boost::basic_string_view<char, std::char_traits<char> >::basic_string_view(__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >&, std::size_t)'
     v = boost::string_view(b, std::size_t(std::distance(b,e)));
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

活生生的例子

如何实例化boost::string_viewfrom std::string::const_iterator

2) Ifboost/spirit/home/x3.hpp包含在move_to重载之前

活生生的例子

为什么没有选择我的重载?它不是比中定义的任何一个更好的重载boost/spirit/home/x3/support/traits/move_to.hpp吗?无论包含顺序如何,我如何确保选择我的超载?

4

1 回答 1

2

I'd simply write what you want:

v = boost::string_view(&*b, std::distance(b,e));

You might want to check that storage is contiguous¹ as a concept check for your input range. In that respect, it might be clearer to also require the iterator to be random-access, and write:

v = boost::string_view(&*b, e-b);

¹ this is a requirement for string_view anyways

于 2016-08-30T11:13:01.913 回答