如何使用 boost::tuple 作为 boost::spirit 规则中的属性
How to use boost::tuple as attribute in a boost::spirit rule?
我在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;
}
为了让 Spirit 将boost::tuple<>
识别为有效的 Fusion 序列,您需要包含一个额外的标头:
#include <boost/fusion/include/boost_tuple.hpp>
这在这里的文档中有些松散地暗示。
相关文章:
- 如何使用 boost 属性树来解析 boost 中 json 字符串中的数组中的元素?
- 如何将Boost属性树的子树序列化为XML
- 如何使用Boost C 属性映射从配置文件中提取值
- boost属性树解析json后无法检索记录
- 使用 Boost 属性树解析 SVG (XML)
- Boost属性树Json读取包含LPWSTR的文件
- 如何将标准::字符串格式的 JSON 解码为 Boost 属性树
- 更改boost属性树的数据类型
- boost属性树xml编写器输出没有结束
- boost属性树值(反)序列化
- 从json文件构造boost属性树的性能非常差
- 如果有任何注释附加到键值,Boost属性树ini解析会出错
- 当INI文件不存在时,使用Boost属性树读取INI文件
- c++ boost属性树获取值
- c++ Boost属性树通过属性限定符更新现有节点
- Boost属性树:如何在其中存储指针
- 使用boost属性树迭代json属性键和值
- Boost属性树,迭代器插入元素
- Boost属性树不能在一个文件中读取多个json数据
- 提高Boost属性树的性能