std::bind
std::bind
is a utility in the C++ Standard Library that allows you to create a function object by binding a specific set of arguments to a function or member function. This function object can then be called with fewer arguments than the original function, or even with none at all, depending on how many arguments you bind. std::bind
is particularly useful when working with callback functions, event handlers, or any situation where you need to adapt a function's signature.
std::bind
_1,
_2,
etc., are used to indicate the positions of arguments that will be provided later when the function object is called.#include <iostream>
#include <functional>
int add(int a, int b) {
return a + b;
}
int main() {
// Bind the first argument to 10
auto addTen = std::bind(add, 10, std::placeholders::_1);
std::cout << "10 + 5 = " << addTen(5) << std::endl; // Outputs: 10 + 5 = 15
return 0;
}
std::bind(add, 10, std::placeholders::_1)
creates a function object where the first argument is bound to 10, and the second argument is left as a placeholder to be provided later.addTen(5)
calls the function with 5 as the second argument, resulting in 10 + 5.std::bind
can also be used to bind member functions to objects, allowing you to call member functions with reduced arguments or even with no arguments.
#include <iostream>
#include <functional>
class MyClass {
public:
void printSum(int a, int b) const {
std::cout << "Sum: " << a + b << std::endl;
}
};
int main() {
MyClass obj;
// Bind the member function with the object and the first argument fixed to 10
auto boundFunc = std::bind(&MyClass::printSum, &obj, 10, std::placeholders::_1);
boundFunc(5); // Outputs: Sum: 15
return 0;
}
std::bind(&MyClass::printSum, &obj, 10, std::placeholders::_1)
binds the printSum member function to the object obj, fixing the first argument to 10 and leaving the second argument as a placeholder.boundFunc(5)
calls printSum on obj with arguments 10 and 5, resulting in the output Sum: 15
.std::bind
with std::function
std::bind
is often used in conjunction with std::function,
which is a general-purpose polymorphic function wrapper. This combination allows you to store the result of std::bind
in a std::function
object, making it easier to pass around and call.
#include <iostream>
#include <functional>
void multiply(int a, int b) {
std::cout << a * b << std::endl;
}
int main() {
// Bind the first argument to 2
std::function<void(int)> multiplyByTwo = std::bind(multiply, 2, std::placeholders::_1);
multiplyByTwo(5); // Outputs: 10
return 0;
}
std::function<void(int)> multiplyByTwo
stores the result of `std::bind(multiply, 2, std::placeholders::_1).multiplyByTwo(5)
calls the bound function with 5 as the second argument, resulting in 2 * 5
.std::bind
can bind more complex functions with multiple arguments, using placeholders to specify the order and positions of arguments that will be supplied later.std::bind
can bind more complex functions with multiple arguments, using placeholders to specify the order and positions of arguments that will be supplied later.
std::bind
can bind more complex functions with multiple arguments, using placeholders to specify the order and positions of arguments that will be supplied later.
#include <iostream>
#include <functional>
void print(int a, int b, int c) {
std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;
}
int main() {
// Bind the first argument to 10, and reorder the rest
auto printReordered = std::bind(print, std::placeholders::_3, std::placeholders::_1, std::placeholders::_2);
printReordered(20, 30, 40); // Outputs: a = 40, b = 20, c = 30
return 0;
}
std::bind(print, std::placeholders::_3, std::placeholders::_1, std::placeholders::_2)
reorders the arguments so that the third argument passed to printReordered becomes the first argument to print, the first argument becomes the second, and the second becomes the third.std::thread
.std::bind
creates a function object by binding arguments to a function or member function, allowing for partial application of function arguments._1
, _2
, etc.) to specify which arguments will be provided later when the function object is called.std::bind
supports binding both free functions and member functions, as well as reordering and partially applying arguments.std::function:
std::bind works well with std::function,
making it easy to store and pass around the resulting function objects.std::bind
is a versatile tool that simplifies the handling of functions and their arguments, particularly in scenarios where functions need to be adapted to different contexts.