Accessing Standard Library Facilities
Every standard library facility is provided through some standard header.
A Program That Reaches Past the Core Language
Imagine writing a program using only the rules of the core language, with no prebuilt pieces to draw on. Every task that involves text, groups of values, output, or running work at the same time would have to be built from the ground up. Experienced C++ programmers almost never work this way. Instead, a body of libraries is developed first, and that body becomes the foundation for everything built afterward. The difference in effort is large: most programs are tedious to write in the bare language, while good libraries make nearly any task simple.
The C++ standard library is that body of libraries. It is not a small add-on. Its specification takes up almost two thirds of the ISO C++ standard, and every complete C++ implementation includes the standard library facilities described in this tour. The question this article answers is practical: when your code uses one of these facilities, how does the facility actually get into your program? The short answer is that you do not receive it just because your program is compiled. You receive it by including the standard header that is responsible for it.
As you read, keep one question in mind: at which line does a facility become available to the code? The rest of the article traces that moment, explains why it happens, and shows how to recognize when it has been skipped.
Tracing a Program Line by Line
Consider a small program that stores a piece of text and a list of values. Read it once, and then trace what the compiler is allowed to rely on at each line. The goal at this stage is only to observe the moment a facility becomes available, not to master the details of either facility.
- Line 1 includes the string header, which makes the standard string available
- Line 2 includes the vector header, which makes the standard vector available
- Lines 6 and 7 use the two facilities; each one arrives through its own include line
Notice that the two include lines are independent. The string header delivers the string facility, and the vector header delivers the vector facility. The source describes exactly this pattern: each header supplies its own facility, such as string for the standard string and list for the standard list, and each include line makes one facility available. The state of the program changes at the top of the file, before any statement inside main runs. Before the includes, the program knows of no standard string and no standard vector. After them, both names are visible to the code that follows.
Why Access Comes Through Headers
A standard header is a named unit of the standard library. Including a header makes the corresponding standard library facility available to your program. The include directive names the header inside angle brackets.
- The placeholder stands for the name of whichever standard header you need
- The header name is written exactly as the standard defines it, not as a variable or function name
- The directive must appear so that the header is in effect for the code that needs the facility
The key idea is a one-to-one way of thinking about access. You do not get a standard facility just because your program is compiled. You get it by including the header that is responsible for it. If you want a facility and you have not included its header, that facility is not made available by any other line in the file, because the include line is the step that brings it in. The two routes are separate. You can include one header without the other, and only the facility tied to the included header becomes available.
int main() {
std::string greeting = "hello";
return 0;
}No include line, so the string facility is not made available to the code
#include <string>
int main() {
std::string greeting = "hello";
return 0;
}The string header is included, so the standard string facility is available
Headers and the Facilities They Unlock
The tour introduces eight named standard library types: string, ostream, vector, map, unique_ptr, thread, regex, and complex. Each name stands for a family of facilities that the standard library provides. The goal is recognition rather than full mastery: you should be able to say which facility handles which kind of job and recognize the typical shape of each use, without yet relying on a complete reference for any of them.
| Facility | Broad job it supports |
|---|---|
| string | Handling text as a value a program can store and manipulate |
| ostream | Sending output to a destination such as the console |
| vector | Holding a group of values that can be accessed in order |
| map | Associating keys with values |
| unique_ptr | Managing ownership of an object through a smart pointer |
| thread | Running work at the same time as other work |
| regex | Describing and matching patterns in text |
| complex | Working with complex numbers |
The eight facilities named in this tour, each at a high level
The relationship between these facilities is not a ranking. They sit side by side as separate families of tools. The broad areas they cover are text, output, groups of values, associations, ownership, concurrency, pattern matching, and numbers. Each one arrives through its own route, which is why a program that uses a map needs the header for it even if it already includes the header for a string.
Tracing which facility a new include unlocks
A program already includes the string header. The author now adds the vector header. Which facility does the program gain, and which facility did it already have?
- 1Identify the existing include
The string header was already included, so the standard string facility is already available.
- 2Identify the new include
The vector header is now added. Including a header makes its own facility available.
- 3Trace the new facility
The program now gains the standard vector facility, a group of values accessed in order.
- 4Check what did not change
The string facility is still available, because its header is still included. Adding vector did not remove it.
The program gains the vector facility. It keeps the string facility it already had. Each include line contributes only the facility tied to its own header.
Diagnosing a Missing or Wrong Header
When a program using standard facilities does not behave as you expect, the cause is often in the include lines at the top of the file. Locating the failure means checking whether each facility the code uses has a matching include line for its header. Because each facility is tied to a header, this check is direct: list the facilities your code names, then confirm that each one has its header included.
Common mistake Assuming a facility is available because the program compiles elsewhere in the file
What it looks likeUsing std::vector in the body of main while the file has no include line for the vector header
Why it failsA facility is made available by including its header. Other lines in the file do not bring it in.
Do this insteadAdd the include line for the header that is responsible for the facility
Common mistake Including a header that does not match the facility being used
What it looks likeIncluding the string header and then expecting the vector facility to be available
Why it failsEach header supplies its own facility, and the routes are separate.
Do this insteadInclude the header for each facility the code actually uses
Common mistake Treating an unknown name error as a logic bug
What it looks likeRewriting the body of a function after the compiler reports an unknown name for a standard facility
Why it failsAn unknown name often means the include step was skipped, not that the algorithm is wrong.
Do this insteadCheck the include lines against the facilities the code uses before changing the logic
Common mistake Writing the header name without angle brackets or with a misspelled name, so the directive does not name the intended header
The Specification and Its Implementations
The standard defines what the library must provide. Implementations are the concrete versions that supply those facilities on real systems. A single specification can therefore be supplied by different implementations, and a program written against the standard is meant to work across conforming implementations. The source describes the examples in this tour as portable for exactly this reason: the aim is to describe C++ as the standard defines it.
The advice in the source is to explore the standard library and prefer it over home-made alternatives. A home-made version has no shared specification behind it, so other programmers and other implementations have no common definition to rely on. The standard facilities carry the weight of a large specification, a great deal of design work, and continuing maintenance and extension.
Where the Standard Library Ends
Beyond the core, most implementations also offer graphical user interface systems, web interfaces, and database interfaces, and most application-development environments provide foundation libraries for their own corporate or industrial use. These are real and useful, but they are supplied by particular systems and environments rather than by the standard facilities. This course keeps them separate and does not cover them in this part.
Practicing Facility Selection
Choose a plain task, such as reading a list of scores, storing them, and printing the average. For each step, name the kind of data involved and the standard facility that fits. Then write the include line each facility needs. Finally, check the file: does every facility you named have its header included?
Hints
- Start by listing the kinds of data: text, a group of values, and output
- Match each kind of data to one of the eight facilities in this tour
- Write one include line per facility, using the header name for that facility
- Check the include lines against the names used in the body of the program
Notice how the program's state grows as you move through the steps. At the start nothing is stored, and each step adds one more piece of information the program has to keep track of. This exercise shows how facilities match to jobs. It does not explain how any of them store data internally, which is a topic for the more detailed parts of the course.
Key Takeaways
- Every standard library facility is provided through a standard header, and including that header is what makes the facility available to your program
- The include directive names the header inside angle brackets, and each header supplies its own facility, so each include line brings in one facility
- A missing or wrong include is a common cause of unknown name errors, so check the include lines against the facilities the code uses before changing the logic
- The standard specification is supplied by many implementations, and the standard facilities are preferred over home-made alternatives, while GUI, web, and database interfaces lie outside the standard library