Search code examples
bindmemberstdbind

std::bind on member function with more than one argument


I have this code

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!
}

I get this error from the compiler (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);
           ^  

Can you tell me where the error is?

Thanks,

Massimo


Solution

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

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