SoFunction
Updated on 2025-04-07

How to use std::placeholders in c++

std::placeholdersis a tool in the C++ standard library for creating placeholders when function objects are bound. It usually has a relationship withstd::bindUsed in conjunction, allowing you to specify certain parameters when binding a function and then provide them when called.

1. Basic concepts

std::placeholdersDefine a set of placeholders (_1_2_3, …), these placeholders represent the parameters passed to the bound function object when calling it._1Indicates the first parameter,_2Represents the second parameter, and so on.

2. Usage scenarios

std::placeholdersMainly used in the following scenarios:

  • Some parameter binding: When you want to bind only some parameters of the function and leave the other parameters until the call is called before passing.
  • Parameter reordering: When you want to change the order of function parameters.

3. Example

Example 1: Partial parameter binding

#include <iostream>
#include <functional> // for std::bind and std::placeholders

void print_sum(int a, int b) {
    std::cout << a + b << std::endl;
}

int main() {
    // The first parameter to bind print_sum is 10, and the second parameter uses placeholder _1    auto f = std::bind(print_sum, 10, std::placeholders::_1);

    f(5);  // Output 15, equivalent to calling print_sum(10, 5)    f(20); // Output 30, equivalent to calling print_sum(10, 20)
    return 0;
}

In this example,std::bindBindedprint_sumThe first parameter is10, and the second parameter uses placeholders_1, indicating that it is being calledfThe first parameter passed at the time will be used asprint_sumThe second parameter.

Example 2: Parameter reordering

#include <iostream>
#include <functional> // for std::bind and std::placeholders

void print_values(int a, int b, int c) {
    std::cout << "a: " << a << ", b: " << b << ", c: " << c << std::endl;
}

int main() {
    // The parameter order of binding print_values ​​is _2, _1, _3    auto f = std::bind(print_values, std::placeholders::_2, std::placeholders::_1, std::placeholders::_3);

    f(1, 2, 3);  // Output a: 2, b: 1, c: 3
    return 0;
}

In this example,std::bindRearrangedprint_valuesThe order of parameters._2Indicates a callfThe second parameter will be used asprint_valuesThe first parameter,_1Indicates a callfThe first parameter will be used asprint_valuesThe second parameter of  and so on.

4. Things to note

  • Number of placeholders: The number of placeholders should match the number of parameters passed at the call. If the number of placeholders is more than the number of parameters actually passed, undefined behavior will result.
  • Order of placeholders: The order of placeholders determines the order of passing parameters when called. You can change the order of parameters passed by adjusting the order of placeholders.

5. Summary

std::placeholdersProvides a flexible way to partially bind function parameters or reorder parameters. It is withstd::bindUsed in conjunction, you can create more flexible and reusable function objects. Understand and masterstd::placeholdersThe use of   can help you write more general and efficient code in C++.

This is the article about how to use std::placeholders in c++. For more related content of std::placeholders, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!