本文介绍了如何在boost :: spirit规则中将boost :: tuple用作属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在boost::spirit中具有以下规则:

typedef boost::tuple<int, int> Entry;
qi::rule<Iterator, Entry(), Skipper> entry;
entry = qi::int_ >> qi::int_;

但是第二个int没有写入元组.有没有一种方法可以使它工作而不必使用boost::fusion::tuple?

But the second int is not written into the tuple. Is there a way to make it work without having to use boost::fusion::tuple?

如果我使用std::pair,它会起作用,那么为什么我不能使用boost::tuple?

It works if I use std::pair, so why can't I use boost::tuple?

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

Here is a full compiling example:

#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;
}

推荐答案

为了使Spirit能够将boost::tuple<>识别为有效的Fusion序列,您需要添加一个附加标头:

In order for Spirit to recognize boost::tuple<> as a valid Fusion sequence, you need to include an additional header:

#include <boost/fusion/include/boost_tuple.hpp>

此处的文档中有点松散地暗示了这一点. .

这篇关于如何在boost :: spirit规则中将boost :: tuple用作属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:08