2

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

user1738687
  • 457
  • 3
  • 12

1 Answers1

5

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

auto f2 = std::bind(&A::g, &a, _1, _2);
EFrank
  • 1,880
  • 1
  • 25
  • 33
  • Thanks EFrank! This means that I cannot use std::bind() with a variadic template member function. Is that true? – user1738687 Jul 17 '14 at 08:49
  • I guess you cannot use std::bind with a variadic template member function directly . But look at http://stackoverflow.com/questions/11902840/binding-member-functions-in-a-variadic-fashion and http://stackoverflow.com/questions/18380820/how-to-combine-stdbind-variadic-templates-and-perfect-forwarding for more hints – EFrank Jul 18 '14 at 11:35