C++

How to use std::bind in C++

There comes some situation when you feel a need to change some of the parametric arguments of any method according to your manipulation need. There are situations when defining the functions with some default arguments that may lead to less diversity or functionality of that particular method. It may lead us to use only those default arguments with the same specific values. Therefore, C++ comes up with the std::bind() function that will be used to manipulate the arguments, i.e., how many arguments you want to use and change their position as well. Thus, we will be looking at the std::bin() function in this C++ tutorial. Let’s check it now. We have to start it from scratch. Thus, we have opened the shell of Ubuntu 20.04 first after logging in via the “Ctrl+Alt+T”. After the opening of a shell console, we need a file to do coding. Thus, we will be utilizing the opened shell to create one. Using the simple “touch” keyword, we have made it so far. This filename is “bind.cc”. After this, we need to open this file in some editor, i.e., built-in nano, built-in text editor. So, we choose to open the file with nano.

Example 01:

So, we have come up with the simplest example to use the std::bind() function in C++. When using the std::bind() function in C++, the “placeholder” is a must in the code. We have started this code with header files required to make our code run. The “iostream” has been used for standard input-output, i.e., cout. The functional header file is utilized to make use of the std::bind() function in the code. We have initialized the namespace as “std” to avoid using the keyword “std” with every statement in the code.

After that, we have also declared the placeholder namespace. Then, a new user-defined method has been declared and defined, taking three integer arguments x, y, and z. Within this function, the cout statement uses these variable values to do some calculations and display them on the shell. The main() function is used to initiate the execution of this code, and it contains the auto keywords before the initializations of a function. So, we have used the f1, and f2 function objects to specify the function calls. Each object uses the “bind” function to manipulate the arguments of the “Call” function.

Within each, we have used three parameters. Two of the parameters are already set, while the “_” represents the first place holder in both the functions. This means, wherever you find “_1” in parameters, you have to add the value at this position passed by the function call. Within the first function, the value will be added to the first place, and in the second function, it will be placed at the second position of “Cal” function arguments. The cout statement has been showing that the first function call will be made. The ‘f1” has been used to call the “f1” object function. It takes 10 as a value that will be assigned to the first argument of the “Cal” function, i.e., “x”. Another cout is used to show that the second function call has been made. The “f2(10)” is showing that the value 10 has been assigned to the second argument “y” of “Cal” function to do calculations. The “Cal” function will do the calculations for the function call separately. The C++ code for the “bind” function has ended here. Let’s begin with saving a code with “Ctrl+S”. After that, you must quit the editor to execute the file in the terminal. Do it with “Ctrl+X”.

After coming back to the shell, you have to make sure that the g++ compiler must be installed on your end. So, we have used the g++ compiler to make our code error-free. After the successful compilation, we ran the file code with the “./a.out” everlasting command in the shell. In return, we have got 5 and -11 as values for two separate function calls using the “bind” function to manipulate the arguments with placeholders.

Let’s change this code a little bit. This time, we will be fixing only 1 argument for the “Cal” function call. We have been changing the positions for the arguments that are being passed to it using the placeholders. So, we are using two placeholders for each function call. The first one contains the “_2” at the first position while the “_1” at the second position. The function call will pass two arguments, and the first argument will be placed at the second position while the second argument will be placed at the 1st position. When we make a call to “f1”, it will do it like “13-1-4”. On the other hand, we used the placeholders at the 1st and third positions of the second function call while passing the same arguments to the “Cal” function. So, the calculation will be something like “1-6-13”.

Let’s just compile the code again after saving the update. In return for execution, we have got 8 and -18 as a result of the update we have just made to the code.

Example 02:

Let’s take a look at another example to see the working of the “bind()” function to bind the different functionality of placeholders in C++. So, we have defined the same header files as we did before in the above example after opening the same file. This program has initialized a user-defined function “Div” of double type, taking two double arguments, “a” and “b”. This function divides the variable value “a” with “b” and returns the calculated value to main(). Then a structure “struct” has been defined with the name “New”. It contains the declaration of two double type variables, “x” and “y”, and it also defines the double type user-defined function “Mult”. This function calculates the multiplication result of variables “x” and “y” and returns it to the main().

The main function starts with the initialization of the standard namespace for placeholders. We have defined the first 4 objects for using the “bind()” function to divide the values of the arguments with placeholders. The first call uses both values as arguments; the second uses a single value to place at position 1, and the 3rd and 4th pass two values at different positions. The “n” object of struct “New” has been defined to pass values to function calls. The first Call has been made to the “Mult” function to calculate multiplication results for 2 values while the other only pass value to single variable “x”. The cout statements used here will display the results at the shell separately for both function calls.

After executing this new code, we have got the results for each bind() method function calls using the placeholders separately.

Conclusion:

This article provides you with a quick look at a “bind()” function on using it in C++. Our examples illustrate its working while using placeholders in the function calls. We have also utilized the structure data variable in our code to enhance it a little more. We are sure that this will prove to be a helpful and informative guide for all users out there.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.