Boost :: spirit属性类型不会折叠

时间:2014-10-22 12:54:31

标签: c++ boost boost-spirit boost-spirit-qi

问题描述

我有一个简单的结构,适用于boost::fusion

struct Node
{
  std::string text;
};

BOOST_FUSION_ADAPT_STRUCT(Node, (std::string, text))

我的语法有一套规则:

boost::spirit::qi::rule<Iterator, Node()> node_line;
boost::spirit::qi::rule<Iterator, Node()> node;

有一条规则在我脸上爆炸:

node_line %= "  * " >> node;

我希望这是有效的,因为文字的属性是unused,而node_linenode具有相同的属性类型。相反,编译器抱怨:

/usr/include/boost/spirit/home/support/container.hpp:262:13: error: no matching function for call to ‘std::basic_string<char>::insert(std::basic_string<char>::iterator, const Node&)’

即。编译器尝试将struct Node附加到std::string。我完全混淆为什么会这样做。有什么想法吗?

完整代码

#include <boost/config/warning_disable.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>

struct Node
{
  std::string text;
};

BOOST_FUSION_ADAPT_STRUCT(Node, (std::string, text))

template <typename Iterator>
struct My_parser : boost::spirit::qi::grammar<Iterator, Node()>
{
  My_parser()
    : My_parser::base_type(node_line)
  {
    name %= +boost::spirit::ascii::char_;
    node %= name;

    node_line %= "  * " >> node;
    // node_line %= node;
  }

  boost::spirit::qi::rule<Iterator, std::string()> name;
  boost::spirit::qi::rule<Iterator, Node()> node_line;
  boost::spirit::qi::rule<Iterator, Node()> node;
};

int main()
{
  const std::string input("  * Some text");
  Node parsed;
  My_parser<std::string::const_iterator> my_parser;

  bool r = parse(input.begin(), input.end(), my_parser, parsed);

  if(r)
    std::cout << "Match: " << parsed.text << std::endl;
  else
    std::cout << "No match" << std::endl;

  return 0;
}

完整错误消息

In file included from /usr/include/boost/spirit/home/support/string_traits.hpp:16:0,
                 from /usr/include/boost/spirit/home/support/nonterminal/expand_arg.hpp:20,
                 from /usr/include/boost/spirit/home/support/context.hpp:15,
                 from /usr/include/boost/spirit/home/qi/domain.hpp:18,
                 from /usr/include/boost/spirit/home/qi/meta_compiler.hpp:15,
                 from /usr/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /usr/include/boost/spirit/home/qi/action.hpp:14,
                 from /usr/include/boost/spirit/home/qi.hpp:14,
                 from /usr/include/boost/spirit/include/qi.hpp:16,
                 from tt.cc:3:
/usr/include/boost/spirit/home/support/container.hpp: In static member function ‘static bool boost::spirit::traits::push_back_container<Container, T, Enable>::call(Container&, const T&) [with Container = std::basic_string<char>, T = Node, Enable = void]’:
/usr/include/boost/spirit/home/support/container.hpp:324:62:   instantiated from ‘bool boost::spirit::traits::push_back(Container&, const T&) [with Container = std::basic_string<char>, T = Node]’
/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:168:13:   instantiated from ‘static void boost::spirit::traits::assign_to_container_from_value<Attribute, T, Enable>::call(const T_&, Attribute&, mpl_::false_, mpl_::false_) [with T_ = Node, Attribute = std::basic_string<char>, T = Node, Enable = void, mpl_::false_ = mpl_::bool_<false>]’
/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:203:13:   instantiated from ‘static void boost::spirit::traits::assign_to_container_from_value<Attribute, T, Enable>::call(const T&, Attribute&) [with Attribute = std::basic_string<char>, T = Node, Enable = void]’
/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:243:13:   instantiated from ‘void boost::spirit::traits::detail::assign_to(const T&, Attribute&, mpl_::true_, mpl_::true_) [with T = Node, Attribute = std::basic_string<char>, mpl_::true_ = mpl_::bool_<true>]’
/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:257:9:   instantiated from ‘void boost::spirit::traits::assign_to(const T&, Attribute&) [with T = Node, Attribute = std::basic_string<char>]’
/usr/include/boost/spirit/home/qi/detail/attributes.hpp:26:13:   [ skipping 12 instantiation contexts ]
/usr/include/boost/function/function_template.hpp:913:60:   instantiated from ‘void boost::function4<R, T1, T2, T3, T4>::assign_to(Functor) [with Functor = boost::spirit::qi::detail::parser_binder<boost::spirit::qi::sequence<boost::fusion::cons<boost::spirit::qi::literal_string<const char (&)[5], true>, boost::fusion::cons<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, Node(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >, boost::fusion::nil> > >, mpl_::bool_<true> >, R = bool, T0 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T1 = const __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T2 = boost::spirit::context<boost::fusion::cons<Node&, boost::fusion::nil>, boost::fusion::vector0<> >&, T3 = const boost::spirit::unused_type&]’
/usr/include/boost/function/function_template.hpp:722:7:   instantiated from ‘boost::function4<R, T1, T2, T3, T4>::function4(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = boost::spirit::qi::detail::parser_binder<boost::spirit::qi::sequence<boost::fusion::cons<boost::spirit::qi::literal_string<const char (&)[5], true>, boost::fusion::cons<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, Node(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >, boost::fusion::nil> > >, mpl_::bool_<true> >, R = bool, T0 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T1 = const __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T2 = boost::spirit::context<boost::fusion::cons<Node&, boost::fusion::nil>, boost::fusion::vector0<> >&, T3 = const boost::spirit::unused_type&, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/usr/include/boost/function/function_template.hpp:1064:16:   instantiated from ‘boost::function<R(T0, T1, T2, T3)>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = boost::spirit::qi::detail::parser_binder<boost::spirit::qi::sequence<boost::fusion::cons<boost::spirit::qi::literal_string<const char (&)[5], true>, boost::fusion::cons<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, Node(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >, boost::fusion::nil> > >, mpl_::bool_<true> >, R = bool, T0 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T1 = const __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T2 = boost::spirit::context<boost::fusion::cons<Node&, boost::fusion::nil>, boost::fusion::vector0<> >&, T3 = const boost::spirit::unused_type&, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]’
/usr/include/boost/function/function_template.hpp:1105:5:   instantiated from ‘typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, boost::function<R(T0, T1, T2, T3)>&>::type boost::function<R(T0, T1, T2, T3)>::operator=(Functor) [with Functor = boost::spirit::qi::detail::parser_binder<boost::spirit::qi::sequence<boost::fusion::cons<boost::spirit::qi::literal_string<const char (&)[5], true>, boost::fusion::cons<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, Node(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> >, boost::fusion::nil> > >, mpl_::bool_<true> >, R = bool, T0 = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T1 = const __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, T2 = boost::spirit::context<boost::fusion::cons<Node&, boost::fusion::nil>, boost::fusion::vector0<> >&, T3 = const boost::spirit::unused_type&, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, boost::function<R(T0, T1, T2, T3)>&>::type = boost::function<bool(__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, const __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >&, boost::spirit::context<boost::fusion::cons<Node&, boost::fusion::nil>, boost::fusion::vector0<> >&, const boost::spirit::unused_type&)>&]’
/usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:214:13:   instantiated from ‘boost::spirit::qi::rule<Iterator, T1, T2, T3, T4>& boost::spirit::qi::operator%=(boost::spirit::qi::rule<Iterator, T1, T2, T3, T4>&, const Expr&) [with Expr = boost::proto::exprns_::expr<boost::proto::tag::shift_right, boost::proto::argsns_::list2<boost::proto::exprns_::expr<boost::proto::tag::terminal, boost::proto::argsns_::term<const char (&)[5]>, 0l>, boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, Node(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>&>, 2l>, Iterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, T1 = Node(), T2 = boost::spirit::unused_type, T3 = boost::spirit::unused_type, T4 = boost::spirit::unused_type, boost::spirit::qi::rule<Iterator, T1, T2, T3, T4> = boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, Node(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type>]’
tt.cc:21:5:   instantiated from ‘My_parser<Iterator>::My_parser() [with Iterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >]’
tt.cc:34:42:   instantiated from here
/usr/include/boost/spirit/home/support/container.hpp:262:13: error: no matching function for call to ‘std::basic_string<char>::insert(std::basic_string<char>::iterator, const Node&)’
/usr/include/boost/spirit/home/support/container.hpp:262:13: note: candidates are:
/usr/include/c++/4.6/bits/basic_string.h:1174:7: note: void std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, typename _Alloc::rebind<_CharT>::other::pointer = char*, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
/usr/include/c++/4.6/bits/basic_string.h:1174:7: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/4.6/bits/basic_string.h:1190:9: note: template<class _InputIterator> void std::basic_string::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _InputIterator, _InputIterator) [with _InputIterator = _InputIterator, _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, typename _Alloc::rebind<_CharT>::other::pointer = char*]
/usr/include/c++/4.6/bits/basic_string.h:1220:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
/usr/include/c++/4.6/bits/basic_string.h:1220:7: note:   no known conversion for argument 1 from ‘std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}’ to ‘long unsigned int’
/usr/include/c++/4.6/bits/basic_string.h:1242:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
/usr/include/c++/4.6/bits/basic_string.h:1242:7: note:   candidate expects 4 arguments, 2 provided
/usr/include/c++/4.6/bits/basic_string.tcc:361:6: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
/usr/include/c++/4.6/bits/basic_string.tcc:361:6: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/4.6/bits/basic_string.h:1283:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
/usr/include/c++/4.6/bits/basic_string.h:1283:7: note:   no known conversion for argument 1 from ‘std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}’ to ‘long unsigned int’
/usr/include/c++/4.6/bits/basic_string.h:1306:7: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>, std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]
/usr/include/c++/4.6/bits/basic_string.h:1306:7: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/4.6/bits/basic_string.h:1323:7: note: std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::insert(std::basic_string<_CharT, _Traits, _Alloc>::iterator, _CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, typename _Alloc::rebind<_CharT>::other::pointer = char*]
/usr/include/c++/4.6/bits/basic_string.h:1323:7: note:   no known conversion for argument 2 from ‘const Node’ to ‘char’

编译器/升级版

这是ubuntu 12.04

  • gcc(Ubuntu / Linaro 4.6.3-1ubuntu5)4.6.3
  • boost 1.46。

话虽如此,我设法在ubuntu 14.04上重现了我的问题(gcc 4.8.2,boost 1.54)

1 个答案:

答案 0 :(得分:2)

这让我想起了很多这个臭名昭着的限制:

但是,我无法点击它,通常的解决方法似乎不适用。您可以在[spirit-general]邮件列表中报告。

与此同时,这是凤凰的解决方法:

node_line = "  * " >> node [qi::_val=qi::_1];

查看 Live On Coliru