1

I'm trying to create a parser using boost::spirit::x3 and stumbled upon a strange issue. Despite that this particular use case is presented in the "Using X3" document by Joel de Guzman and Michael Caisse (https://ciere.com/cppnow15/), not all advertised features seem to work.

The problem is that Spirit X3 apparently has trouble synthesizing tuple attribute like std::pair. So, the following construction claimed to produce an attribute with type of pair of strings:

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

Trivial example:

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

Live on coliru: http://coliru.stacked-crooked.com/a/8980ba6215ae0ce7

This code fails to compile, compiler complains about trying to assign an int to a pair. Why does Spirit do that instead of extracting two values (string and int, respectively) and putting them to a pair? Not sure if it is a bug in boost::spirit::x3, perhaps I am doing something wrong.

Compilers tried: apple-clang, GCC, MSVC17. Boost version is 1.66 or 1.69.

r0mko
  • 11
  • 2

1 Answers1

0

You are missing the include:

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

That include contains the templates needed to adapt a std::pair into a boost fusion sequence.

mhhollomon
  • 965
  • 7
  • 15