c++ - 当我使用 boost::bind 时,为什么 boost::spirit::qi 语义操作不能使用两个参数?

标签 c++ boost boost-spirit-qi

我尝试使用 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;
}

我尝试扩展我的代码。在第二种情况下,我想将两个参数传递给绑定(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& 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;
}

最佳答案

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

关于c++ - 当我使用 boost::bind 时,为什么 boost::spirit::qi 语义操作不能使用两个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9761879/

相关文章:

c++ - 如何在 OpenGL 窗口模式下扩展鼠标空间

c++ - 在类中初始化 ofstream

c++ - QByteArray:如何在其中搜索字符

c++ - (boost.python) 暴露重载的 operator+() 时出错。 "TypeError: No to_python (by-value) converter found"

c++ - 如何使用 boost 条件变量来等待线程完成处理?

c++ - 支持具有固定数组的对象的 BOOST_FUSION_ADAPT_STRUCT?

c++ - 我可以重复调用 WSASend() 吗?

使用 BOOST 将 python float 转换为 c++ double

c++ - 使用qi::double_和qi::uint_的组合即时=字符串| float | int规则时,std::cout输出损坏

c++ - 提升精神报告语义错误