Open In App

Binders in C++ STL

Last Updated : 05 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In the C++ Standard Template Library (STL), binders are a type of functors that bind or associate some of the arguments of a function to a fixed value. This fixed value is stored inside the functor and the rest of the arguments can be passed dynamically at the time of calling the functor. The most commonly used binders in STL are std::bind1st and std::bind2nd, which are used to bind the first or second argument of a binary function, respectively.

Starting from C++11, the C++ Standard Library provides std::bind, which is a more general binder that can bind any number of arguments to any values and returns a new function object. The new function object can then be used as a regular function, taking the remaining unbound arguments.

Syntax of std::bind in C++

std::bind(function, arg1, arg2, ..., argN);

Parameters in std::bind in C++

  • function is the function or function object that you want to bind some of its arguments.
  • arg1, arg2, …, argN are the arguments to bind. These arguments can be any type of value or expression, including references, pointers, and placeholder values (e.g., std::placeholders::_1, std::placeholders::_2, etc.)

Errors Caused using std::bind in C++

There are certain errors that can occur while using std::bind in C++ as mentioned below:

  • Incorrect number of arguments: If you bind more or fewer arguments than the target function takes, the program will result in a compile-time error.
  • Incorrect argument types: If the types of arguments passed to std::bind do not match the types expected by the target function, the program will result in a compile-time error.
  • Incorrect use of placeholders: If you use the wrong placeholder or use a placeholder in a way that is inconsistent with the target function, the program will result in a run-time error.
  • Incorrect object type: If you pass an object of the wrong type as the first argument to std::bind when binding a member function, the program will result in a compile-time error.
  • Incorrect use of member function pointer: If you pass an incorrect member function pointer to std::bind when binding a member function, the program will result in a compile-time error.

To avoid these errors, it is important to carefully check the arguments passed to std::bind, ensure that the correct placeholders are used, and make sure that the correct object type and member function pointer are passed.

Examples to show the Implementation of the bind function

Example 1:

C++




#include <bits/stdc++.h>
using namespace std;
// function which has to be binded.
int add(int a, int b) { return a + b; }
  
int main()
{
    auto addf = bind(add, 10, 20);
    // binding the Function add() to values 10 and 20.
    cout << "The output is " << addf() << endl;
    // the Functor addf is called.
    return 0;
}


Output

The output is 30

Example 2:

C++




#include <bits/stdc++.h>
using namespace std;
// The function which has to be binded.
int multiply(int a, int b, int c) { return a * b * c; }
  
int main()
{
    auto multiplyf = bind(multiply, 10, 20, 30);
    // binding the Function multiply() to the 10,20,30
    // values.
    cout << "The output is " << multiplyf() << endl;
    // The functor multiplyf is called.
    return 0;
}


Output

The output is 6000

Incorrect use of placeholders Examples

If you use the wrong placeholder or use a placeholder in a way that is inconsistent with the target function, the program will result in a run-time error.

Example 1:

C++




#include <bits/stdc++.h>
using namespace std;
// The Function which has to be binded.
int subtract(int a, int b) { return a - b; }
  
int main()
{
    auto subtractf = bind(subtract, 10, placeholders::_1);
    // binding the function subtract() to the argument 10.
    cout << "The output is " << subtractf(5) << endl;
    // the functor subtractf  is called with argument 5
    return 0;
}


Output

The output is 5

Example 2:

C++




#include <bits/stdc++.h>
using namespace std;
// The function which has to be binded.
int multiply(int a, int b, int c) { return a * b * c; }
  
int main()
{
    auto multiplyf = bind(multiply, 10, placeholders::_1,
                          placeholders::_2);
    // binding the function multiply() to argument 10.
    cout << "The output is " << multiplyf(2, 3)
         << endl; // calling the functor multiplyf with
                  // argument 2 and 3.
    return 0;
}


Output

The output is 60


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads