如何在boost :: spirit规则中使用boost :: tuple作为属性?

Fra*_*ank 6 c++ boost boost-spirit boost-spirit-qi

我有以下规则boost::spirit:

typedef boost::tuple<int, int> Entry;
qi::rule<Iterator, Entry(), Skipper> entry;
entry = qi::int_ >> qi::int_;
Run Code Online (Sandbox Code Playgroud)

但第二个int没有写入元组.有没有办法让它工作不必使用boost::fusion::tuple

如果我使用它,它可以工作std::pair,为什么我不能使用boost::tuple

这是一个完整的编译示例:

#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/tuple.hpp>
#include <boost/tuple/tuple.hpp>
namespace qi = boost::spirit::qi;

// works:
// #include <boost/fusion/include/std_pair.hpp>
// typedef std::pair<int, int> Entry;

// doesn't work:
typedef boost::tuple<int, int> Entry;

template <typename Iterator, typename Skipper>
struct MyGrammar : qi::grammar<Iterator, Entry(), Skipper> {
  MyGrammar() : MyGrammar::base_type(entry) {
    entry = qi::int_ >> qi::int_;
  }
  qi::rule<Iterator, Entry(), Skipper> entry;
};

int main() {
  const std::string in = "1 3";
  typedef std::string::const_iterator It;
  It it = in.begin();

  Entry entry;
  MyGrammar<It, qi::space_type> gr;
  if (qi::phrase_parse(it, in.end(), gr, qi::space, entry)
      && it == in.end()) {
    std::cout << boost::get<0>(entry) << "," << boost::get<1>(entry) << std::endl;
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

ild*_*arn 10

为了将Spirit识别boost::tuple<>为有效的Fusion序列,您需要包含一个额外的标题:

#include <boost/fusion/include/boost_tuple.hpp>
Run Code Online (Sandbox Code Playgroud)

这有点松散在文档暗示这里.