Possible Duplicate:
std::bind a bound function
void foo0(int val) { std::cout << "val " << val << "\n"; }
void foo1(int val, std::function<void (int)> ftor) { ftor(val); }
void foo2(int val, std::function<void (int)> ftor) { ftor(val); }
int main(int argc, char* argv[]) {
auto applyWithFoo0 ( std::bind(foo0, std::placeholders::_1) );
//std::function<void (int)> applyWithFoo0 ( std::bind(foo0, std::placeholders::_1) ); // use this instead to make compile
auto applyFoo1 ( std::bind(foo1, std::placeholders::_1, applyWithFoo0) );
foo2(123, applyFoo1);
}
The sample above does not compile giving multiple errors like: Error 1 error C2780: '_Ret std::tr1::_Callable_fun<_Ty,_Indirect>::_ApplyX(_Arg0 &&,_Arg1 &&,_Arg2 &&,_Arg3 &&,_Arg4 &&,_Arg5 &&,_Arg6 &&,_Arg7 &&,_Arg8 &&,_Arg9 &&) const' : expects 10 arguments - 2 provided
.
Using the commented line with explicit type does compile. It seems that the type inferred by auto
is not correct. What is the problem with auto
in this case?
Platform: MSVC 10 SP 1, GCC 4.6.1
The issue is that std::bind
treats "bind expression" (like your applyWithFoo0
) differently from other types. Instead of calling foo1 with applyWithFoo0
as parameter it tries to invoke applyWithFoo0
and pass its return value to foo1. But applyWithFoo0
doesn't return anything that is convertible to std::function<void(int)>
. The intention of handling "bind expressions" like this is to make them easily composable. In most cases you probably don't want bind expression to be passed as function parameters but only their results. If you explicitly wrap the bind expression into a function<>
object, the function<>
object will simply be passed to foo1 directly since it is not a "bind expression" and therefore not handled specially by std::bind
.
Consider the following example:
#include <iostream>
#include <functional>
int twice(int x) { return x*2; }
int main()
{
using namespace std;
using namespace std::placeholders;
auto mul_by_2 = bind(twice,_1);
auto mul_by_4 = bind(twice,mul_by_2); // #2
auto mul_by_8 = bind(twice,mul_by_4); // #3
cout << mul_by_8(1) << endl;
}
This actually compiles and works because instead of passing a functor to twice like you might expect from the bind expressions #2 and #3, bind actually evaluates the passed bind expressions and uses its result as function parameter for twice. Here, it is intentional. But in your case, you tripped over this behaviour by accident because you actually want bind to pass the functor itself to the function instead of its evaluated value. Wrapping the functor into a function<> object is obviously a work-around for that.
In my opinion this design decision is a bit awkward because it introduces an irregularity people have to know about to be able to use bind correctly. Maybe, we'll get another more satisfying work around in the future like
auto applyFoo1 = bind( foo1, _1, noeval(applyWithFoo0) );
where noeval
tells bind not to evaluate the expression but to pass it directoy to the function. But maybe the other way around -- explicitly telling bind to pass the result of a functor to the function -- would have been a better design:
auto mul_by_8 = bind( twice, eval(mul_by_4) );
But I guess, now it's too late for that ...