std::placeholders
is a tool in the C++ standard library for creating placeholders when function objects are bound. It usually has a relationship withstd::bind
Used in conjunction, allowing you to specify certain parameters when binding a function and then provide them when called.
1. Basic concepts
std::placeholders
Define a set of placeholders (_1
, _2
, _3
, …), these placeholders represent the parameters passed to the bound function object when calling it._1
Indicates the first parameter,_2
Represents the second parameter, and so on.
2. Usage scenarios
std::placeholders
Mainly 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::bind
Bindedprint_sum
The first parameter is10
, and the second parameter uses placeholders_1
, indicating that it is being calledf
The first parameter passed at the time will be used asprint_sum
The 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::bind
Rearrangedprint_values
The order of parameters._2
Indicates a callf
The second parameter will be used asprint_values
The first parameter,_1
Indicates a callf
The first parameter will be used asprint_values
The 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::placeholders
Provides a flexible way to partially bind function parameters or reorder parameters. It is withstd::bind
Used in conjunction, you can create more flexible and reusable function objects. Understand and masterstd::placeholders
The 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!