I'm trying to pass semantic action in a grammar's inherited argument.
In the very basic example below the grammar parses two numbers and I pass semantic action (in a form of c++ lambda) into it and I'd like this action to be called on parsing of the first number. However it does not called but silently ignored and I'd like to know why is it so and what is the proper way to do such things.
#include <iostream>
#include <boost/spirit/include/qi.hpp>
using namespace std;
using namespace boost;
namespace qi = spirit::qi;
namespace phx = phoenix;
template <typename Iterator, typename Action>
struct two_numbers : qi::grammar<Iterator, void (Action const&)>
{
two_numbers() : two_numbers::base_type(start)
{
using namespace qi;
start = int_ [ _r1 ] >> ' ' >> int_;
}
qi::rule<Iterator, void (Action const&)> start;
};
int main ()
{
string input { "42 21" };
auto first=std::begin (input), last=std::end(input);
static const auto my_action = [] (auto&& p) {
cout << "the meaning of life is " << p << "\n";
};
static const two_numbers <decltype(first), decltype (my_action)> p;
if (qi::parse (first, last, p(phx::ref(my_action))))
cout << "parse ok\n";
}
The expected output is:
the meaning of life is 42
parse ok
And the real output is:
parse ok