Question

This doesn't work: http://ideone.com/mUL5Y

Figured I helped it a little with deducting that type: http://ideone.com/mGfUj

Doesn't work either! I don't understand why not.

How should I do this?

Was it helpful?

Solution

The problem seems to be that std::bind is internally passing the evaluated results of the Callable (it's actually a std::bind<> type) object c when you pass it for a second time here:

part2(std::bind(&Quux::part3<Callable>, this, 3, c))

It's not passing an un-evaluated version of the std::bind object like you're assuming.

Therefore since your f function returns a void type, the result of the c expression is internally passed as a void type, and not an un-evaluated callable function object. Thus when you attempt to call the Callable object c in Quux::part2, which in-turn attempts to evaluate the call c in Quux::part3, it cannot pass a callable object as the second argument to Quux::part3, since the type it is passing is actually a void type, and not a callable type.

For further reference, see here: http://en.cppreference.com/w/cpp/utility/functional/bind

Note particularly that:

If std::is_bind_expression<T>::value==true (i.e., another std::bind() subexpression was used as an argument in the initial call to bind), then that bind-subexpression is invoked immediately and its result is passed to the function.

If you want to delay the evaluation so that it happens when you want it to, and not when you pass a std::bind sub-expression, you will have to look for another method, such as a lambda, or std::function<> object, a functor, or some other class object type that is both callable and can store a state which can be evaluated later.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top