【问题标题】:How to use boost::tuple as attribute in a boost::spirit rule?如何在 boost::spirit 规则中使用 boost::tuple 作为属性?
【发布时间】:2012-05-28 08:32:50
【问题描述】:

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

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

但是第二个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;
}

【问题讨论】:

  • 您缺少一个重要的包含 -- boost/fusion/include/boost_tuple.hpp
  • 啊,有道理。就像boost/fusion/include/std_pair.hpp。如果您将您的评论写成正确的答案,我将接受为正确答案。

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


【解决方案1】:

为了让 Spirit 将 boost::tuple&lt;&gt; 识别为有效的 Fusion 序列,您需要包含一个额外的标头:

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

这在文档 here 中有所暗示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多