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.