std::bind
Bind arguments to callables.
What Is std::bind?
std::bind creates a new callable by fixing some arguments of an existing one. This is called partial application.
- Lives in
<functional>. - Returns an unspecified callable object.
#include <iostream>
#include <functional>
int add(int a, int b) { return a + b; }
int main() {
auto add10 = std::bind(add, 10, 5);
std::cout << add10() << '\n';
return 0;
}Placeholders
Placeholders like std::placeholders::_1 mark arguments to be supplied later when the bound object is called.
#include <iostream>
#include <functional>
int add(int a, int b) { return a + b; }
int main() {
using namespace std::placeholders;
auto add10 = std::bind(add, 10, _1);
std::cout << add10(7) << '\n';
return 0;
}