我尝试过使用boost语义动作。就我而言,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;
}
我试图扩展我的代码。在第二种情况下,我想将两个参数传递给bound函数,但编译器不会编译此代码。什么失败了?我没有找到任何例子。 第二个代码在这里:
#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;
}
答案 0 :(得分:2)
您无法编译此代码,因为只有一个输入参数 - boost :: fusion :: vector&lt; int,int&gt; - 由(int_&gt;&gt;“,”&gt;&gt; int_)序列组成。试试这个
#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(boost::fusion::vector < int, int > arg_)
{
std::cout << boost::fusion::at_c < 0 > (arg_) << "," << boost::fusion::at_c < 1 > (arg_) << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
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)] >> '}');
return 0;
}