Skip to content
Related Articles
Open in App
Not now

Related Articles

Bind Function and Placeholders in C++

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 03 Jun, 2022
Improve Article
Save Article

Sometimes we need to manipulate the operation of a function according to the need, i.e changing some arguments to default, etc. Predefining a function to have default arguments restricts the versatility of a function and forces us to use the default arguments and that too with similar values each time. From C++11 onwards, the introduction of the bind function has made this task easier. 

How does bind() work? 

Bind function with the help of placeholders helps to manipulate the position and number of values to be used by the function and modifies the function according to the desired output. 

What are placeholders? 

Placeholders are namespaces that direct the position of a value in a function. They are represented by _1, _2, _3… 

Example:

CPP




// C++ code to demonstrate bind() and
// placeholders
#include <iostream>
#include <functional> // for bind()
using namespace std;
 
// for placeholders
using namespace std::placeholders;
 
// Driver function to demonstrate bind()
void func(int a, int b, int c)
{
    cout << (a - b - c) << endl;
}
 
int main()
{
    // for placeholders
    using namespace std::placeholders;
 
    // Use of bind() to bind the function
    // _1 is for first parameter and assigned
    // to 'a' in above declaration.
    // 2 is assigned to b
    // 3 is assigned to c
    auto fn1 = bind(func, _1, 2, 3);
 
    // 2 is assigned to a.
    // _1 is for first parameter and assigned
    // to 'b' in above declaration.
    // 3 is assigned to c.
    auto fn2 = bind(func, 2, _1, 3);
 
    // calling of modified functions
    fn1(10);
    fn2(10);
 
    return 0;
}

Output:

5
-11

In the above code, bind() modified the call of a function to take 1 argument and returned the desired output. 

Properties of Placeholders

1. The position of the placeholder determines the value position in the function call statement 

CPP




// C++ code to demonstrate placeholder
// property 1
#include <iostream>
#include <functional> // for bind()
using namespace std;
 
// for placeholders
using namespace std::placeholders;
 
// Driver function to demonstrate bind()
void func(int a, int b, int c)
{
    cout << (a - b - c) << endl;
}
 
int main ()
{
    // for placeholders
    using namespace std::placeholders;
 
    // Second parameter to fn1() is assigned
    // to 'a' in fun().
    // 2 is assigned to 'b' in fun
    // First parameter to fn1() is assigned
    // to 'c' in fun().
    auto fn1 = bind(func, _2, 2, _1);
 
    // calling of function
    cout << "The value of function is : ";
    fn1(1, 13);
 
    // First parameter to fn2() is assigned
    // to 'a' in fun().
    // 2 is assigned to 'b' in fun
    // Second parameter to fn2() is assigned
    // to 'c' in fun().
    auto fn2 = bind(func, _1, 2, _2);
 
    // calling of same function
    cout << "The value of function after changing"
        " placeholder position is : ";
    fn2(1, 13);
 
    return 0;
}

Output:

The value of function is : 10
The value of function after changing placeholder position is : -14

In the above code, even though the position of 1 and 13 were the same in a function call, the change in the position of placeholders changed the way the function was called.   

2. The number of placeholders determines the number of arguments required to pass in the function.

We can use any no. of placeholders in the function call statement (obviously less than the maximum number of arguments). The rest values are replaced by the user-defined default values. 

CPP




// C++ code to demonstrate placeholder
// property 2
#include <functional> // for bind()
#include <iostream>
using namespace std;
 
// for placeholders
using namespace std::placeholders;
 
// Driver function to demonstrate bind()
void func(int a, int b, int c)
{
    cout << (a - b - c) << endl;
}
 
int main()
{
    // for placeholders
    using namespace std::placeholders;
 
    // 1 placeholder
    auto fn1 = bind(func, _1, 2, 4);
 
    // calling of function with 1 argument
    cout << "The value of function with 1 "
            "placeholder is : ";
    fn1(10);
 
    // 2 placeholders
    auto fn2 = bind(func, _1, 2, _2);
 
    // calling of function with 2 arguments
    cout << "The value of function with 2"
            " placeholders is : ";
    fn2(13, 1);
 
    // 3 placeholders
    auto fn3 = bind(func, _1, _3, _2);
 
    // calling of function with 3 arguments
    cout << "The value of function with 3 "
            "placeholders is : ";
    fn3(13, 1, 4);
 
    return 0;
}

Output:

The value of function with 1 placeholder is : 4
The value of function with 2 placeholders is : 10
The value of function with 3 placeholders is : 8

In the above code, clearly the no. of placeholders equated to the number of arguments required to call the function. The binding of function is directed by the number and position of placeholders. 

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!