Matching a sequence of two integers in `std :: pair <int, int>`

I am trying to use Boost.Sprit x3 to match a sequence of two integers in std::pair<int, int>. Judging by the documentation, the following code should compile:


#include <string>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>

int main()
{
    using namespace boost::spirit::x3;

    std::string input("1 2");
    std::pair<int, int> result;
    parse(input.begin(), input.end(), int_ >> int_, result);
}

link melpon.org


However, it corresponds only to the first whole. If I change std::pair<int, int> result;to int result;and then print result, I get 1as my output.

Why is this happening? Is the int_ >> int_correct way to define a parser that matches (and sets as attributes) two integers?

+4
source share
1

, @T.C. <boost/fusion/adapted/std_pair.hpp> , , . x3::parse() a x3::phrase_parse(), :

#include <iostream>
#include <string>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/adapted/std_pair.hpp>

int main()
{
    namespace x3 = boost::spirit::x3;

    std::string input("1 2");
    std::pair<int, int> result;
    auto ok = x3::phrase_parse(input.begin(), input.end(), x3::int_ >> x3::int_, x3::space, result);
    std::cout << std::boolalpha << ok << ": ";
    std::cout << result.first << ", " << result.second;
}

Live

, using namespace boost::spirit::x3 x3. , Boost.Spirit.

+5

Source: https://habr.com/ru/post/1650470/


All Articles