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

use*_*687 2 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!
}
Run Code Online (Sandbox Code Playgroud)

我从编译器(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);
           ^  
Run Code Online (Sandbox Code Playgroud)

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

谢谢,

马西莫

EFr*_*ank 5

The call to bind needs to specify both parameters, like this:

auto f2 = std::bind(&A::g, &a, _1, _2);
Run Code Online (Sandbox Code Playgroud)