The following:
#include <functional>
struct Foo
{
void bar1() {}
void bar2(int) {}
void bar3(int, int) {}
void bar4(int, int, int) {}
};
int main()
{
Foo foo;
auto f1 = std::bind(&Foo::bar1, &foo);
auto f2 = std::bind(&Foo::bar2, &foo);
auto f3 = std::bind(&Foo::bar3, &foo);
auto f4 = std::bind(&Foo::bar4, &foo);
f1(1, 2, 3, 4); // success
f2(1, 2, 3); // error
f3(1, 2); // error
f4(1); // error
return 0;
}
f1(1, 2, 3, 4) compiles and executes bar1(). f2(1, 2, 3), doesn't compile, f3(1, 2) doesn't compile (yet bar3 has the correct prototype) and f4(1) doesn't compile. The error I get with Visual Studio 2013 for those 3 cases is
"class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments"
I have limited understanding of templates and the Standard Library, but this doesn't seem to make any logical sense to me. Is there a reasonably simple explanation?