Passing function as argument to an other function

What is the right and most up-to-date way of passing a class member function to an other function as an argument?
Suppose, I have a function with this signature:
void OtherClass::do_it(function<void(void)> f);

I know I can use it like this:
other_class_instance->do_it(bind(&MyClass::do_what, this));

Is this the way to do it these days?
Last edited on
Lately I've been using lambdas to do this.
other_class_instance->do_it([this](){ do_what(); });

I do it this way because the std::bind call relies on the marginally arcane fact that the first parameter to a member function pointer is an implicit this-pointer. Plus, from what I've seen, partial function application is almost invariably explained nicely using closures (whereas bind() is essentially template witchery).

partial function application is almost invariably explained nicely using closures


Could you please tell me what you mean by partial function application?

bind() is essentially template witchery


Does that mean that one might run into issues using bind() that wouldn't happen using lambdas (like in your example)?
Sure... bind() itself provides partial application.
Without getting into any serious functional programming stuff, std::bind will "fix" (technically bind) specific arguments of any function to a value chosen before that function is called. Those parameters that aren't bound before the call are correspondingly free.
1
2
3
4
int add(int a, int b) { return a + b; }
...
auto add5 = std::bind(add, 5, std::placeholders::_1);
assert(add5(10) == 15);

add5 is a partially-applied version of add -- a version of add whose first parameter is fixed to the value 5.

The equivalent code using lambdas looks like this:
auto add5 = [] (int a) { return add (5, a); }

Does that mean that one might run into issues using bind() that wouldn't happen using lambdas (like in your example)?

I can think of one, although in practice you're not likely to encounter a problem. std::bind is a construction which relies on objects in the namespace std::placeholders. There's only a finite number of them (at least some standard lower limit), and so if you have a function which accepts some large number of arguments, it may be impossible to use std::bind for lack of sufficient placeholder objects.

Granted, you can define your own placeholders by specializing std::is_placeholder, but this is error prone at best.

bind() can't do anything that closures cannot.
Last edited on
Topic archived. No new replies allowed.