我尝试使用增强语义动作。在我的情况下boost::bind
是最简单的解决方案。第一个例子运行良好;在这里,我在语义动作中只使用了一个参数。
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/bind.hpp>
#include <iostream>
namespace qi = boost::spirit::qi;
// A plain function
void print(int const& i)
{
std::cout << i << std::endl;
}
int main()
{
using boost::spirit::qi::int_;
using boost::spirit::qi::parse;
char const *first = "{44}", *last = first + std::strlen(first);
parse(first, last, '{' >> int_[boost::bind(&print, _1)] >> '}');
return 0;
}
我试图扩展我的代码。在第二种情况下,我想将两个参数传递给绑定函数,但编译器不会编译这段代码。什么是失败?我没有找到任何例子。第二个代码在这里:
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/bind.hpp>
#include <iostream>
namespace qi = boost::spirit::qi;
// A plain function
void print(int const& i1, int const& i2)
{
std::cout << i1 << "," << i2 << std::endl;
}
int main()
{
using boost::spirit::qi::int_;
using boost::spirit::qi::parse;
char const *first = "{44,55}", *last = first + std::strlen(first);
parse(first, last, '{' >> (int_ >> "," >> int_)[boost::bind(&print, _1,_2)] >> '}');
return 0;
}