I'm learning Boost Spirit 2, and I can't figure out why I can't parse a sequence of two integers into an STL vector of int. From the documentation of the >> operator (here) I know the attribute of the grammar int_ >> int_
should be vector<int>
. And from the attribute notation documentation I know that "The notation of vector<A>
stands for any STL container holding elements of type A".
And yet, when I try to add an action to int_ >> int_
, which takes std::vector
as its attribute, compilation fails.
Among other error messages, it says: /usr/include/boost/bind/mem_fn_template.hpp:163:7: note: no known conversion for argument 2 from ‘boost::fusion::vector<int, int>’ to ‘const std::vector<int>&’
I'm aware of this question ("Getting boost::spirit::qi to use stl containers"), but the solution there - including std_tuple.hpp - did not change anything.
Why is this?
#include <iostream>
#include <string>
#include <vector>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/bind.hpp>
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
class ParserActions
{
public:
void printivec (std::vector<int> const& ivec)
{
std::cout << "We see " << ivec.size() << " integers" << std::endl;
}
};
template <typename Iterator>
bool parse_ivec(Iterator first, Iterator last)
{
using ascii::space;
ParserActions actionContainer;
auto two_ints = (qi::int_ >> qi::int_);
bool r = qi::parse(
first, /*< start iterator >*/
last, /*< end iterator >*/
two_ints[boost::bind(&ParserActions::printivec,&actionContainer,_1)]
);
return first == last && r;
}
}
int main()
{
std::string str = "12 13";
if (client::parse_ivec(str.begin(),str.end()))
std::cout << "Parsing succeeded\n";
else
std::cout << "Parsing failed!\n";
return 0;
}