std :: bind在具有多个参数的成员函数上

时间:2014-07-16 08:00:00

标签: bind member stdbind

我有这段代码

struct A {
    void f(int) {}
    void g(int, double) {}
};

int main() {
    using std::placeholders;
    A a;

    auto f1 = std::bind(&A::f, &a, _1);
    f1(5);                                   //  <--- works fine

    auto f2 = std::bind(&A::g, &a, _1);
    f2(5, 7.1);                              //  <--- error!
}

我从编译器(gcc 4.8.1)中得到此错误:

error: no match for call to '(std::_Bind<std::_Mem_fn<void (A::*)(int, double)>(A*, std::_Placeholder<1>)>) (int, double)'
 f2(1, 1.1);
           ^  

你能告诉我错误在哪里吗?

谢谢,

的Massimo

1 个答案:

答案 0 :(得分:3)

对bind的调用需要指定两个参数,如下所示:

auto f2 = std::bind(&A::g, &a, _1, _2);
相关问题