boost :: spirit :: x3中的connathesting std ::对属性属性

Synthesizing std::pair attributes in boost::spirit::x3

本文关键字:属性 std connathesting 中的 spirit x3 boost      更新时间:2023-10-16

我正在尝试使用boost :: spirit :: x3创建一个解析器,并偶然发现了一个奇怪的问题。尽管如此,乔尔·德·古兹曼(Joel de Guzman)和迈克尔·凯斯(Michael Caisse)(https://ciere.com/cppnow15/)在"使用X3"文档中介绍了这种特殊用例,但并不是所有的广告功能似乎都起作用。

问题是Spirit X3显然难以合成元组属性,例如std :: paip。因此,以下构造声称产生了具有一对字符串类型的属性:

auto item = rule<class item, std::pair<std::string, std::string>>() 
          = name >> ’:’ >> ( quote | name );

琐碎的示例:

#include <iostream>
#include <vector>
#include <boost/spirit/home/x3.hpp>
using namespace std;
namespace x3 = boost::spirit::x3;
using x3::int_;
int main()
{
    string input = "foo: 146                n"
                   "the_answer: 42          n"
                   "freeze_point: 0         n";
    cout << input << endl << endl;
    auto identifier = x3::rule<class identifier, string>()
            = x3::lexeme[(x3::alpha | '_') >> *(x3::alnum | '_')];
    auto key_value_pair = x3::rule<class key_value_pair, pair<string, int>>()
            = identifier >> ':' >> int_;
    auto first = input.begin();
    auto last = input.end();
    vector<pair<string, int>> output;
    bool result = x3::phrase_parse(first, last, *(key_value_pair), x3::space, output);
    cout << endl << "Result:" << result << endl << (first - input.begin()) << " of " << (last - input.begin());
    return 0;
}

生活在科里鲁: http://coliru.stacked-crooked.com/a/8980ba6215ae0ce7

此代码无法编译,编译器抱怨尝试将INT分配给一对。精神为什么这样做是这样做的,而不是提取两个值(分别是字符串和int)并将它们放在一对中?不确定这是否是BOOST :: Spirit :: X3中的错误,也许我做错了什么。

编译器尝试:Apple-Clang,GCC,MSVC17。Boost版本为1.66或1.69。

您缺少:

#include <boost/fusion/adapted/std_pair.hpp>

包含包含STD ::配对所需的模板中的模板。