Using Placeholders with bind()
Placeholders for bind() live in the subnamespace std::placeholders.
A Marker You Cannot Use Yet
When you work with bind(), you will often need placeholders. A placeholder is not a value you compute or store. It is a named marker that bind() understands. Before you can use one, you need to know where it lives, because placeholders are not available just by writing their names on their own. This article answers that first question: where to find placeholders, and how that location connects them to bind().
What do you think happens?
If you write _1 by itself in your program, with no other qualification, will the name be available for use with bind()?
Reveal answer
No, the name must be reached through its namespace.
Placeholders are not available just by writing their names on their own. They are grouped in a dedicated namespace, and you reach them through that location.
Tracing the Name from the Outside In
The answer to the location question is a dedicated namespace. Placeholders are grouped together rather than scattered across the standard library, which keeps them separate from ordinary functions and values. Understanding this grouping is the foundation for every later use of placeholders in bind().
The full name is built in two parts. The outer namespace is std, the namespace of the C++ standard library. The inner namespace is placeholders, which sits inside std. The two parts are joined with the scope operator, giving std::placeholders. This is why the source calls it a subnamespace: it is nested within std rather than standing alone. Read the name from left to right to trace the nesting: std, then placeholders, then the individual placeholder such as _1.
Read the diagram from top to bottom. The header provides the placeholders namespace, the standard library namespace holds it, and the placeholders inside it are the items that bind() uses. The diagram shows structure only. The source does not describe the internal details of how each placeholder works, so those details are not shown here.
What a Placeholder Stands For
The source states that placeholders are used in the bind() function. Their purpose is to be the items you supply to bind() as stand-ins. That is why they must be reachable from the std::placeholders namespace: bind() needs to recognize them when it sees them.
A placeholder, in the context of bind(), is a named marker from std::placeholders that bind() treats specially. It identifies a position rather than a concrete value you pass in at that moment.
The contrast between a placeholder and an ordinary value is the key idea. An ordinary argument such as the number 10 is a concrete value you hand to bind() now. A placeholder is a stand-in: it marks a spot that is not filled in when you write the bind() call. Because it is a marker rather than a value, you do not compute it or store it in a variable. This article establishes location and purpose; how numbered placeholders map to the arguments of a later call is covered in the full reference.
Locating Placeholders Step by Step
The source gives the header as the entry point. Both the placeholders and bind() are provided through <functional>. If the header is not included in your program, the placeholders and bind() are not available to your code through this route. Work through the locating process below to see how the pieces fit together.
Finding the placeholder _1 for use with bind()
You want to use the placeholder _1 with bind(). Determine what your program needs in order to reach it.
- 1Step 1: Include the entry header
Placeholders and
bind()are provided through the <functional> header, so the program must include it. - 2Step 2: Identify the outer namespace
The placeholders live inside std, the namespace of the C++ standard library.
- 3Step 3: Identify the inner namespace
The placeholders namespace sits inside std, forming the subnamespace std::placeholders.
- 4Step 4: Reach the individual placeholder
The placeholder _1 is reached through the subnamespace, written std::placeholders::_1, so that
bind()can recognize it.
To use _1 with bind(), the program includes <functional> and refers to the placeholder as std::placeholders::_1.
- The header <functional> is the entry point that provides the placeholders and
bind(). - std::placeholders::_1 uses the full path: outer namespace std, inner subnamespace placeholders, then the placeholder _1.
Mistakes Beginners Make with Placeholder Names
Most errors in this area come from locating the placeholder incorrectly. The two requirements the source gives are that the placeholders live in std::placeholders and that the <functional> header provides that namespace. The comparison below sets an incorrect approach beside a correct one.
Common mistake Using the placeholder name without its namespace
What it looks likeWriting _1 alone in the bind() callWhy it failsPlaceholders are not available just by writing their names on their own. They are grouped in std::placeholders.
Do this insteadRefer to the placeholder through its full path, such as std::placeholders::_1.
Common mistake Assuming the placeholders come from std directly
What it looks likeWriting std::_1Why it failsThe placeholders sit in the subnamespace placeholders, which is nested inside std. They are not declared directly in std.
Do this insteadInsert the placeholders subnamespace: std::placeholders::_1.
Common mistake Forgetting the entry header
What it looks likeUsing placeholders and bind() without including <functional>
Why it failsThe placeholders and
bind()are provided through <functional>. Without that header they are not available to your code through this route.Do this insteadInclude <functional> before using std::placeholders or bind().
Locating Placeholders in Your Own Code
Without writing a full program, describe the three things a file must contain or refer to before it can use the placeholder _2 with bind(): the header, the outer namespace, and the inner namespace. Then write the exact name you would use to refer to _2.
Hints
- Start from the header: which standard header provides std::placeholders and
bind()? - The outer namespace is std and the inner one is placeholders, joined by the scope operator.
Key Takeaways on Placeholder Location
- 1Placeholders for
bind()live in the subnamespace std::placeholders, nested inside the std namespace. - 2The <functional> header provides std::placeholders, and it is the entry point for both the placeholders and
bind(). - 3A placeholder is a named marker that
bind()understands. It stands in for a position rather than holding a value you compute or store. - 4Refer to placeholders by their full path, such as std::placeholders::_1, and make sure <functional> is included.
At a glance
- Placeholders for
bind()live in the subnamespace std::placeholders, nested inside std. - The <functional> header provides std::placeholders and
bind(). - A placeholder is a named marker that
bind()treats specially, not a value you compute or store. - Use the full path, such as std::placeholders::_1, and include <functional>.