std::bind and Its Alternatives
Lesson Introduction
Welcome! In modern C++ development, crafting flexible and reusable code is key to building highly maintainable applications. One powerful tool to aid in this endeavor is std::bind, which allows you to create function objects by binding specific arguments to functions. By the end of this lesson, you will understand std::bind, learn its syntax, explore its usage, and become familiar with lambda expressions as an alternative.
Introduction to std::bind
std::bind is part of the <functional> library in C++. It allows you to bind one or more arguments to a function, creating new callable objects. Callable objects are entities that can be called as if they are functions, including normal functions, function objects, and lambda expressions. Binding defines the values for the arguments, but it doesn’t invoke the function. The function is invoked only when someone calls the function object returned by std::bind. We have created such functions manually in the previous lesson to practice; std::bind can help you achieve the same result faster and easier!
Example Using std::bind
Here's an example to understand std::bind:
In this example:
addis a function that takes twoints and returns their sum.std::bindcreatesadd_fiveby binding the second argument ofaddto 5.- Calling
add_five(3)results inadd(3, 5), producing 8.
Placeholders are special objects used in std::bind to represent arguments provided later. By using a placeholder, we say: "Hey, there will be an argument, but it is not present right now!"
In this example, std::placeholders::_1 means that the first parameter to add_five will become the first parameter to add.
If you had a need for additional parameters, you could use std::placeholders::_2 for the second parameter, std::placeholders::_3 for the third, and so on.
Binding by reference
Binding by reference can be useful when you need the bound parameter to reflect any changes made to the original variable. This means that the bound function will use the current value of the variable when invoked, not the value it had when the function was created.
To bind by reference, use std::ref for non-const references and std::cref for const references:
In this example:
-
std::ref(x)bindsxby reference which meansxretains its original memory address. Therefore, changes toxwill be reflected in the bound function. Initially,xis 5, soadd_ref(3)results inadd(5, 3). Whenxis changed to 10,add_ref(2)results inadd(10, 2). -
std::cref(y)bindsyby const reference. This means the bound function can only read, not modify,y. The example bindsy, which is 10, andadd_const_ref(2)results inadd(10, 2). Note that attempts to modifyywould result in compilation errors due to its const qualification.
Binding by reference is particularly useful for working with large data structures where copying them would be inefficient.
