Это дополнительный вопрос к моему Предыдущая относительно boost::spirit::x3
а также boost::string_view
,
Пока я могу разобрать в std::vector<std::string>
(живой пример), разбирая в std::vector<boost::string_view>
завершается со следующими ошибками компиляции:
#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_vec = *(raw[ +~char_('_')] >> '_');
}
int main()
{
std::string input = "hello_world_";
std::vector<boost::string_view> strVec;
parse(input.data(), input.data()+input.size(), parser::str_vec, strVec);
for(auto& x : strVec) { std::cout << x << std::endl; }
}
In file included from /usr/local/include/c++/6.1.0/bits/stl_tempbuf.h:60:0,
from /usr/local/include/c++/6.1.0/bits/stl_algo.h:62,
from /usr/local/include/c++/6.1.0/algorithm:62,
from /usr/local/include/boost/utility/string_view.hpp:27,
from main.cpp:4:
/usr/local/include/c++/6.1.0/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = boost::basic_string_view<char, std::char_traits<char> >; _Args = {const char&}]':
/usr/local/include/c++/6.1.0/bits/stl_uninitialized.h:75:18: required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const char*; _ForwardIterator = boost::basic_string_view<char, std::char_traits<char> >*; bool _TrivialValueTypes = false]'
/usr/local/include/c++/6.1.0/bits/stl_uninitialized.h:126:15: required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const char*; _ForwardIterator = boost::basic_string_view<char, std::char_traits<char> >*]'
/usr/local/include/c++/6.1.0/bits/stl_uninitialized.h:281:37: required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = const char*; _ForwardIterator = boost::basic_string_view<char, std::char_traits<char> >*; _Tp = boost::basic_string_view<char, std::char_traits<char> >]'
/usr/local/include/c++/6.1.0/bits/stl_vector.h:1288:33: required from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const char*; _Tp = boost::basic_string_view<char, std::char_traits<char> >; _Alloc = std::allocator<boost::basic_string_view<char, std::char_traits<char> > >]'
/usr/local/include/c++/6.1.0/bits/stl_vector.h:1261:4: required from 'void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_InputIterator, _InputIterator, std::__false_type) [with _InputIterator = const char*; _Tp = boost::basic_string_view<char, std::char_traits<char> >; _Alloc = std::allocator<boost::basic_string_view<char, std::char_traits<char> > >]'
/usr/local/include/c++/6.1.0/bits/stl_vector.h:406:11: [ skipping 9 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/local/include/boost/spirit/home/x3/operator/detail/sequence.hpp:496:24: required from 'static bool boost::spirit::x3::detail::parse_into_container_impl<boost::spirit::x3::sequence<L, R>, Context, RContext>::call(const parser_type&, Iterator&, const Iterator&, const Context&, RContext&, Attribute&) [with Iterator = const char*; Attribute = std::vector<boost::basic_string_view<char, std::char_traits<char> > >; Left = boost::spirit::x3::raw_directive<boost::spirit::x3::plus<boost::spirit::x3::negated_char_parser<boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, char> > > >; Right = boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, boost::spirit::x3::unused_type>; Context = boost::spirit::x3::unused_type; RContext = const boost::spirit::x3::unused_type; boost::spirit::x3::detail::parse_into_container_impl<boost::spirit::x3::sequence<L, R>, Context, RContext>::parser_type = boost::spirit::x3::sequence<boost::spirit::x3::raw_directive<boost::spirit::x3::plus<boost::spirit::x3::negated_char_parser<boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, char> > > >, boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, boost::spirit::x3::unused_type> >]'
/usr/local/include/boost/spirit/home/x3/core/detail/parse_into_container.hpp:287:74: required from 'bool boost::spirit::x3::detail::parse_into_container(const Parser&, Iterator&, const Iterator&, const Context&, RContext&, Attribute&) [with Parser = boost::spirit::x3::sequence<boost::spirit::x3::raw_directive<boost::spirit::x3::plus<boost::spirit::x3::negated_char_parser<boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, char> > > >, boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, boost::spirit::x3::unused_type> >; Iterator = const char*; Context = boost::spirit::x3::unused_type; RContext = const boost::spirit::x3::unused_type; Attribute = std::vector<boost::basic_string_view<char, std::char_traits<char> > >]'
/usr/local/include/boost/spirit/home/x3/operator/kleene.hpp:32:48: required from 'bool boost::spirit::x3::kleene<Subject>::parse(Iterator&, const Iterator&, const Context&, RContext&, Attribute&) const [with Iterator = const char*; Context = boost::spirit::x3::unused_type; RContext = const boost::spirit::x3::unused_type; Attribute = std::vector<boost::basic_string_view<char, std::char_traits<char> > >; Subject = boost::spirit::x3::sequence<boost::spirit::x3::raw_directive<boost::spirit::x3::plus<boost::spirit::x3::negated_char_parser<boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, char> > > >, boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, boost::spirit::x3::unused_type> >]'
/usr/local/include/boost/spirit/home/x3/core/parse.hpp:35:68: required from 'bool boost::spirit::x3::parse_main(Iterator&, Iterator, const Parser&, Attribute&) [with Iterator = const char*; Parser = boost::spirit::x3::kleene<boost::spirit::x3::sequence<boost::spirit::x3::raw_directive<boost::spirit::x3::plus<boost::spirit::x3::negated_char_parser<boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, char> > > >, boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, boost::spirit::x3::unused_type> > >; Attribute = std::vector<boost::basic_string_view<char, std::char_traits<char> > >]'
/usr/local/include/boost/spirit/home/x3/core/parse.hpp:60:26: required from 'bool boost::spirit::x3::parse(const Iterator&, Iterator, const Parser&, Attribute&) [with Iterator = const char*; Parser = boost::spirit::x3::kleene<boost::spirit::x3::sequence<boost::spirit::x3::raw_directive<boost::spirit::x3::plus<boost::spirit::x3::negated_char_parser<boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, char> > > >, boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, boost::spirit::x3::unused_type> > >; Attribute = std::vector<boost::basic_string_view<char, std::char_traits<char> > >]'
main.cpp:36:75: required from here
/usr/local/include/c++/6.1.0/bits/stl_construct.h:75:7: error: no matching function for call to 'boost::basic_string_view<char, std::char_traits<char> >::basic_string_view(const char&)'
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from main.cpp:4:0:
/usr/local/include/boost/utility/string_view.hpp:82:23: note: candidate: constexpr boost::basic_string_view<charT, traits>::basic_string_view(const charT*) [with charT = char; traits = std::char_traits<char>] <near match>
BOOST_CONSTEXPR basic_string_view(const charT* str)
^~~~~~~~~~~~~~~~~
/usr/local/include/boost/utility/string_view.hpp:82:23: note: conversion of argument 1 would be ill-formed:
In file included from /usr/local/include/c++/6.1.0/bits/stl_tempbuf.h:60:0,
from /usr/local/include/c++/6.1.0/bits/stl_algo.h:62,
from /usr/local/include/c++/6.1.0/algorithm:62,
from /usr/local/include/boost/utility/string_view.hpp:27,
from main.cpp:4:
/usr/local/include/c++/6.1.0/bits/stl_construct.h:75:7: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from main.cpp:4:0:
/usr/local/include/boost/utility/string_view.hpp:85:23: note: candidate: constexpr boost::basic_string_view<charT, traits>::basic_string_view(const charT*, boost::basic_string_view<charT, traits>::size_type) [with charT = char; traits = std::char_traits<char>; boost::basic_string_view<charT, traits>::size_type = long unsigned int]
BOOST_CONSTEXPR basic_string_view(const charT* str, size_type len)
^~~~~~~~~~~~~~~~~
/usr/local/include/boost/utility/string_view.hpp:85:23: note: candidate expects 2 arguments, 1 provided
/usr/local/include/boost/utility/string_view.hpp:78:9: note: candidate: template<class Allocator> boost::basic_string_view<charT, traits>::basic_string_view(const std::__cxx11::basic_string<charT, traits, Allocator>&)
basic_string_view(const std::basic_string<charT, traits,
^~~~~~~~~~~~~~~~~
/usr/local/include/boost/utility/string_view.hpp:78:9: note: template argument deduction/substitution failed:
In file included from /usr/local/include/c++/6.1.0/bits/stl_tempbuf.h:60:0,
from /usr/local/include/c++/6.1.0/bits/stl_algo.h:62,
from /usr/local/include/c++/6.1.0/algorithm:62,
from /usr/local/include/boost/utility/string_view.hpp:27,
from main.cpp:4:
/usr/local/include/c++/6.1.0/bits/stl_construct.h:75:7: note: mismatched types 'const std::__cxx11::basic_string<char, std::char_traits<char>, Allocator>' and 'const char'
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from main.cpp:4:0:
/usr/local/include/boost/utility/string_view.hpp:68:23: note: candidate: constexpr boost::basic_string_view<charT, traits>::basic_string_view(const boost::basic_string_view<charT, traits>&) [with charT = char; traits = std::char_traits<char>]
BOOST_CONSTEXPR basic_string_view(const basic_string_view &rhs) BOOST_NOEXCEPT
^~~~~~~~~~~~~~~~~
/usr/local/include/boost/utility/string_view.hpp:68:23: note: no known conversion for argument 1 from 'const char' to 'const boost::basic_string_view<char, std::char_traits<char> >&'
/usr/local/include/boost/utility/string_view.hpp:65:23: note: candidate: constexpr boost::basic_string_view<charT, traits>::basic_string_view() [with charT = char; traits = std::char_traits<char>]
BOOST_CONSTEXPR basic_string_view() BOOST_NOEXCEPT
^~~~~~~~~~~~~~~~~
/usr/local/include/boost/utility/string_view.hpp:65:23: note: candidate expects 0 arguments, 1 provided
In file included from /usr/local/include/c++/6.1.0/bits/char_traits.h:39:0,
from /usr/local/include/c++/6.1.0/ios:40,
from /usr/local/include/c++/6.1.0/ostream:38,
from /usr/local/include/c++/6.1.0/iostream:39,
from main.cpp:1:
/usr/local/include/c++/6.1.0/bits/stl_algobase.h: In instantiation of 'static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = const char*; _OI = boost::basic_string_view<char, std::char_traits<char> >*]':
/usr/local/include/c++/6.1.0/bits/stl_algobase.h:386:44: required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = const char*; _OI = boost::basic_string_view<char, std::char_traits<char> >*]'
/usr/local/include/c++/6.1.0/bits/stl_algobase.h:422:45: required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = const char*; _OI = __gnu_cxx::__normal_iterator<boost::basic_string_view<char, std::char_traits<char> >*, std::vector<boost::basic_string_view<char, std::char_traits<char> > > >]'
/usr/local/include/c++/6.1.0/bits/stl_algobase.h:455:8: required from '_OI std::copy(_II, _II, _OI) [with _II = const char*; _OI = __gnu_cxx::__normal_iterator<boost::basic_string_view<char, std::char_traits<char> >*, std::vector<boost::basic_string_view<char, std::char_traits<char> > > >]'
/usr/local/include/c++/6.1.0/bits/vector.tcc:637:16: required from 'void std::vector<_Tp, _Alloc>::_M_range_insert(std::vector<_Tp, _Alloc>::iterator, _ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const char*; _Tp = boost::basic_string_view<char, std::char_traits<char> >; _Alloc = std::allocator<boost::basic_string_view<char, std::char_traits<char> > >; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<boost::basic_string_view<char, std::char_traits<char> >*, std::vector<boost::basic_string_view<char, std::char_traits<char> > > >; typename std::_Vector_base<_Tp, _Alloc>::pointer = boost::basic_string_view<char, std::char_traits<char> >*]'
/usr/local/include/c++/6.1.0/bits/stl_vector.h:1375:4: required from 'void std::vector<_Tp, _Alloc>::_M_insert_dispatch(std::vector<_Tp, _Alloc>::iterator, _InputIterator, _InputIterator, std::__false_type) [with _InputIterator = const char*; _Tp = boost::basic_string_view<char, std::char_traits<char> >; _Alloc = std::allocator<boost::basic_string_view<char, std::char_traits<char> > >; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<boost::basic_string_view<char, std::char_traits<char> >*, std::vector<boost::basic_string_view<char, std::char_traits<char> > > >; typename std::_Vector_base<_Tp, _Alloc>::pointer = boost::basic_string_view<char, std::char_traits<char> >*]'
/usr/local/include/c++/6.1.0/bits/stl_vector.h:1100:4: [ skipping 12 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/local/include/boost/spirit/home/x3/operator/detail/sequence.hpp:496:24: required from 'static bool boost::spirit::x3::detail::parse_into_container_impl<boost::spirit::x3::sequence<L, R>, Context, RContext>::call(const parser_type&, Iterator&, const Iterator&, const Context&, RContext&, Attribute&) [with Iterator = const char*; Attribute = std::vector<boost::basic_string_view<char, std::char_traits<char> > >; Left = boost::spirit::x3::raw_directive<boost::spirit::x3::plus<boost::spirit::x3::negated_char_parser<boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, char> > > >; Right = boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, boost::spirit::x3::unused_type>; Context = boost::spirit::x3::unused_type; RContext = const boost::spirit::x3::unused_type; boost::spirit::x3::detail::parse_into_container_impl<boost::spirit::x3::sequence<L, R>, Context, RContext>::parser_type = boost::spirit::x3::sequence<boost::spirit::x3::raw_directive<boost::spirit::x3::plus<boost::spirit::x3::negated_char_parser<boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, char> > > >, boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, boost::spirit::x3::unused_type> >]'
/usr/local/include/boost/spirit/home/x3/core/detail/parse_into_container.hpp:287:74: required from 'bool boost::spirit::x3::detail::parse_into_container(const Parser&, Iterator&, const Iterator&, const Context&, RContext&, Attribute&) [with Parser = boost::spirit::x3::sequence<boost::spirit::x3::raw_directive<boost::spirit::x3::plus<boost::spirit::x3::negated_char_parser<boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, char> > > >, boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, boost::spirit::x3::unused_type> >; Iterator = const char*; Context = boost::spirit::x3::unused_type; RContext = const boost::spirit::x3::unused_type; Attribute = std::vector<boost::basic_string_view<char, std::char_traits<char> > >]'
/usr/local/include/boost/spirit/home/x3/operator/kleene.hpp:32:48: required from 'bool boost::spirit::x3::kleene<Subject>::parse(Iterator&, const Iterator&, const Context&, RContext&, Attribute&) const [with Iterator = const char*; Context = boost::spirit::x3::unused_type; RContext = const boost::spirit::x3::unused_type; Attribute = std::vector<boost::basic_string_view<char, std::char_traits<char> > >; Subject = boost::spirit::x3::sequence<boost::spirit::x3::raw_directive<boost::spirit::x3::plus<boost::spirit::x3::negated_char_parser<boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, char> > > >, boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, boost::spirit::x3::unused_type> >]'
/usr/local/include/boost/spirit/home/x3/core/parse.hpp:35:68: required from 'bool boost::spirit::x3::parse_main(Iterator&, Iterator, const Parser&, Attribute&) [with Iterator = const char*; Parser = boost::spirit::x3::kleene<boost::spirit::x3::sequence<boost::spirit::x3::raw_directive<boost::spirit::x3::plus<boost::spirit::x3::negated_char_parser<boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, char> > > >, boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, boost::spirit::x3::unused_type> > >; Attribute = std::vector<boost::basic_string_view<char, std::char_traits<char> > >]'
/usr/local/include/boost/spirit/home/x3/core/parse.hpp:60:26: required from 'bool boost::spirit::x3::parse(const Iterator&, Iterator, const Parser&, Attribute&) [with Iterator = const char*; Parser = boost::spirit::x3::kleene<boost::spirit::x3::sequence<boost::spirit::x3::raw_directive<boost::spirit::x3::plus<boost::spirit::x3::negated_char_parser<boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, char> > > >, boost::spirit::x3::literal_char<boost::spirit::char_encoding::standard, boost::spirit::x3::unused_type> > >; Attribute = std::vector<boost::basic_string_view<char, std::char_traits<char> > >]'
main.cpp:36:75: required from here
/usr/local/include/c++/6.1.0/bits/stl_algobase.h:324:18: error: no match for 'operator=' (operand types are 'boost::basic_string_view<char, std::char_traits<char> >' and 'const char')
*__result = *__first;
~~~~~~~~~~^~~~~~~~~~
In file included from main.cpp:4:0:
/usr/local/include/boost/utility/string_view.hpp:71:26: note: candidate: boost::basic_string_view<charT, traits>& boost::basic_string_view<charT, traits>::operator=(const boost::basic_string_view<charT, traits>&) [with charT = char; traits = std::char_traits<char>]
basic_string_view& operator=(const basic_string_view &rhs) BOOST_NOEXCEPT {
^~~~~~~~
/usr/local/include/boost/utility/string_view.hpp:71:26: note: no known conversion for argument 1 from 'const char' to 'const boost::basic_string_view<char, std::char_traits<char> >&'
Что мне здесь не хватает?
Перегрузкой / специализацией move_to
Вы в основном говорите X3, как конвертировать из boost::iterator_range
(на самом деле просто пара итераторов) для boost::string_view
, К сожалению, он не знает, что оба «эквивалентны» (один substitute
другой, кажется, номенклатура, используемая в Духе). Итак, ваш оригинальный пример работает, потому что вы явно запрашиваете преобразование из итераторов в string_view
но этот сбой, так как X3 не хватает необходимой информации для вывода соответствующего типа атрибута.
В частности, проблема, кажется, Вот, где проверяется ли атрибут парсера (в данном случае raw_attribute_type
) совместим с value_type ожидаемого атрибута (boost::string_view
). Этот тест не пройден, и затем выбирается путь к коду, где атрибут не «передается как есть», вызывая полученную ошибку.
Имея все это в виду, кажется, есть два возможных подхода: либо явно запросить преобразование, либо предоставить Spirit информацию о совместимости атрибутов.
Пропали некоторые из помощников у нас были в Ци (attr_cast
, as[]
) кажется, что способ заставить преобразование атрибута в X3 — создать дополнительное правило с нужным вам типом:
x3::rule<class str,boost::string_view> str= x3::raw[ +~char_('_')] >> '_';
auto const str_vec = *str;
Вы также можете (особенно если вам нужно сделать это в нескольких правилах) использовать подход, который я видел, он использовал несколько раз (например, Вот), который создает переменную шаблон лямбда, которая может быть использована как as<T>(parser)
который скрывает создание этого дополнительного правила и заставляет атрибут parser
превратить в T
,
template<typename T>
auto as = [](auto p) { return rule<struct _, T>{} = as_parser(p); };
auto const str_vec = *as<boost::string_view>(x3::raw[ +~char_('_')] >> '_');
Способ предоставить Духу информацию о совместимости атрибутов заключается в том, чтобы специализироваться x3::traits::is_substitute
:
namespace boost {
namespace spirit { namespace x3 { namespace traits {
template <>
struct is_substitute<raw_attribute_type,boost::string_view> : boost::mpl::true_
{};
}}}
}
Но, как вы упомянули в комментариях, это не похоже на документированную точку настройки, и поэтому я не уверен, стоит ли ее использовать. В идеале это было бы специализировано по умолчанию в X3, но, как он отметил в предыдущий вопрос, тот факт, что память, указанная итераторами, должна быть смежной, усложняет ситуацию.
Других решений пока нет …