c++ - boost::bind 不编译

标签 c++ boost boost-spirit boost-bind

我是提振 spirit 的新手,遇到以下问题:

#include <string>
#include <vector>
#include <boost/spirit/include/qi.hpp>

#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_statement.hpp>

#include <boost/bind.hpp>

using namespace boost::spirit;
using namespace std;

struct MyGrammar
    : qi::grammar<string::const_iterator, string(), ascii::space_type> {
    MyGrammar();

    void myFun(const string& s);

    private:
    qi::rule<string::const_iterator, string(), ascii::space_type> myRule;
};



using namespace boost::spirit;
using namespace std;

MyGrammar::MyGrammar() : MyGrammar::base_type(myRule) {
    using qi::_1;

    myRule = int_ [boost::bind(&MyGrammar::myFun, this, _1)]; // fails
    myRule = int_ [_val = _1];  // fine
}

void MyGrammar::myFun(const string& s){
    cout << "read: " << s << endl;
}

int
main(){
}

myRule 的第一个赋值出现编译错误,而第二个赋值编译正常。

在第一种情况下,编译器会输出大量我无法理解的错误消息。 最后它说:

boost_1_49_0/include/boost/bind/bind.hpp:318:9: error: no match for call to '(const boost::_mfi::mf1<void, MyGrammar, const std::basic_string<char>&>) (MyGrammar* const&, const boost::phoenix::actor<boost::spirit::argument<0> >&)'
boost_1_49_0/include/boost/bind/mem_fn_template.hpp:163:7: note: candidates are: R boost::_mfi::mf1<R, T, A1>::operator()(T*, A1) const [with R = void, T = MyGrammar, A1 = const std::basic_string<char>&]
boost_1_49_0/include/boost/bind/mem_fn_template.hpp:184:7: note:                 R boost::_mfi::mf1<R, T, A1>::operator()(T&, A1) const [with R = void, T = MyGrammar, A1 = const std::basic_string<char>&]

有什么想法吗? 非常感谢您的帮助!

最佳答案

您不能使用来自不同 bind 实现的占位符。目前 Boost 中有三个 bind 函数:

  • boost::bind,由
  • 取代
  • boost::lambda::bind,由
  • 取代
  • boost::phoenix::bind,您应该将其用于 Boost.Spirit

boost::spirit::qi(和boost::spirit::karma)下的占位符与boost使用的相同: :phoenix::bind,所以就用它吧。

哦,还有专业提示:停止你的 using namespace std;,最好停止全局命名空间中的任何其他 using 指令。

关于c++ - boost::bind 不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10758078/

相关文章:

c++ - 使用 fstream 从文件中获取最后一个字节的最佳方法?

c++ - 如何将管道句柄转换为 Google protobuf 的 file_descriptor(int)

c++ - Boost:is_any_of 产生多个错误

c++ - Boost spirit : how to parse string of value pairs into map<string, string> 然后返回?

c++ - 将类对象作为参数传递给 boost::thread

c++ - 可以将花括号数字列表用作传递给可变参数函数(或构造函数)的参数吗?

c++ - 如何在同一单位中使用具有比率的 Boost.Unit

c++ - 为什么在使用 boost::split 时必须使用 boost::is_any_of?

c++ - Boost.Spirit SQL 语法/词法分析器失败

c++ - 具有 boost::spirit::karma::lit 的语义 Actor