【问题标题】:Why doesn't boost::spirit::qi semantic action work with two arguments when I use boost::bind?当我使用 boost::bind 时,为什么 boost::spirit::qi 语义动作不能与两个参数一起工作?
【发布时间】:2012-03-18 20:15:45
【问题描述】:

我尝试使用增强语义动作。就我而言,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;
}

【问题讨论】:

    标签: c++ boost boost-spirit-qi


    【解决方案1】:

    您无法编译此代码,因为只有一个输入参数 - boost::fusion::vector - 由 (int_ >> "," >> 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多