cpp/standard library

The C++ standard library provides a wide range of facilities that are usable in standard C++.

Category
The language support library provides components that are required by certain parts of the C++ language, such as memory allocation (new/delete) and exception processing.

The diagnostics library provides a consistent framework for reporting errors in a C++ program, including predefined exception classes.

The memory management library provides components for memory management, including smart pointers.

The general utilities library includes components used by other library elements, such as a predefined storage allocator for dynamic storage management, and components used as infrastructure in C++ programs, such as function wrappers.

The strings library provides support for manipulating text represented as homogeneous sequences of following types:, , and any other character-like types.

The containers, iterators, and algorithms libraries provide a C++ program with access to a subset of the most widely used algorithms and data structures.

The numerics library provides numeric algorithms and complex number components that extend support for numeric processing. The valarray component provides support for n-at-a-time processing, potentially implemented as parallel operations on platforms that support such processing.

The time library provides generally useful time utilities.

The localization library provides extended internationalization support for text processing.

The input/output library provides the iostream components that are the primary mechanism for C++ program input and output. They can be used with other elements of the library, particularly strings, locales, and iterators.

Library contents
The C++ standard library provides definitions for the entities and macros described in the synopses of the C++ standard library headers, unless otherwise specified.

All library entities except operator new and operator delete are defined within the namespace or namespaces nested within namespace  (except the entities for the C standard library facilities, see below).

Headers
Each element of the C++ standard library is declared or defined (as appropriate) in a header. A header is not necessarily a source file, nor are the sequences delimited by in header names necessarily valid source file names.

The C++ standard library provides the C++ library headers and additional C++ headers for C library facilities (see 'headers' page for descriptions):

A freestanding implementation has an implementation-defined set of headers, see here for the minimal requirement on the set of headers.

C standard library
The C++ standard library also makes available the facilities of the C standard library, suitably adjusted to ensure static type safety. The descriptions of many library functions rely on the C standard library for the semantics of those functions.

In some cases, the signatures specified in standard C++ may be different from the signatures in the C standard library, and additional overloads may be declared, but the behavior and the preconditions are the same unless otherwise stated.

For compatibility with the C standard library, the C++ standard library provides the C headers listed below. The intended use of these headers is for interoperability only. It is possible that C++ source files need to include one of these headers in order to be valid ISO C. Source files that are not intended to also be valid ISO C should not use any of the C headers. See here for descriptions.

Except otherwise noted, the contents of each header is the same as that of the corresponding header  as specified in the C standard library. In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope of the namespace. It is unspecified whether these names (including any overloads added) are first declared within the global namespace scope and are then injected into namespace by explicit using-declarations.

Names which are defined as macros in C (,, , , and ) must be defined as macros in the C++ standard library, even if C grants license for implementation as functions.

Names that are defined as functions in C must be defined as functions in the C++ standard library. This disallows the practice, allowed in C, of providing a masking macro in addition to the function prototype. The only way to achieve equivalent inline behavior in C++ is to provide a definition as an extern inline function.

Identifiers that are keywords or operators in C++ cannot be defined as macros in C++ standard library headers. In particular, including the standard header has no effect.

Names associated with safe functions in standard C
If any C++ header is included, it is implementation-defined whether any of the following C standard Annex K names is declared in the global namespace (none of them is declared in namespace ):

Including headers
The entities in the C++ standard library are defined in headers, whose contents are made available to a translation unit when it contains the appropriate preprocessing directive.

A translation unit may include library headers in any order. Each may be included more than once, with no effect different from being included exactly once, except that the effect of including either or  depends each time on the lexically current definition of.

A translation unit can only include a header outside of any declaration or definition, and lexically before the first reference in that translation unit to any of the entities declared in that header. No diagnostic is required.

{{rrev|since=c++20|

Importing headers
The C++ library headers, or, for a freestanding implementation, the subset of such headers that are provided by the implementation, are collectively known as the importable C++ library headers.

The contents of importable C++ library headers are made available to a translation unit when it contains the appropriate import declaration. }}

{{rrev|since=c++23|

Importing modules
The C++ standard library provides the following C++ library modules:
 * The named module exports declarations in namespace  that are provided by the importable C++ library headers (e.g. std from ) and the C++ headers for C library facilities (e.g. std from ). It additionally exports declarations in the global namespace for the storage allocation and deallocation functions that are provided by  (e.g. operator new).
 * The named module exports the same declarations as the named module, and additionally exports declarations in the global namespace corresponding to the declarations in namespace  that are provided by the C++ headers for C library facilities (e.g. std).

For each declaration in the standard library,
 * the module it attaches to is unspecified, and
 * it denotes the same entity regardless of whether it was made reachable through including a header, importing a header unit, or importing a C++ library module.

}}

Linkage
Entities in the C++ standard library have external linkage. Unless otherwise specified, objects and functions have the default linkage.

Whether a name from the C standard library declared with external linkage has or  linkage is implementation-defined. The C++ standard recommends using in this case.

Objects and functions defined in the library and required by a C++ program are included in the program prior to program startup.

Guarantees
A C++ header must provide declarations and definitions that appear in
 * the synopsis of that header, or
 * the synopsis of another header which is appeared to be included in the synopsis of that header.

For types and macros defined in multiple headers (such as ), including any number of these headers in any order never violates the one definition rule.

Unless otherwise specified, all object-like macros defined by the C standard library that expand to integral constant expressions can be used in cpp/preprocessor/conditional preprocessing directives.

Calling a standard library non-member function signature always results in actually calling that function. Therefore a conforming standard library implementation cannot define additional non-member functions that may be called by a valid C++ program.

Non-member function signatures are never declared with additional default arguments.

Unless otherwise specified, calls made by functions in the standard library to non-operator, non-member functions do not use functions from another namespace which are found through argument-dependent name lookup.

For each friend declaration of a function (template) within a class (template) definition, no other declaration is provided for that function (template).

For each class defined in the C++ standard library required to be derived from another class defined in the C++ standard library,
 * the base class must be virtual if it is specified as ,
 * the base class cannot be virtual if it is not specified as, and
 * unless otherwise specified, types with distinct names shall be distinct types.

If a function defined in the C++ standard library is specified to throw an exception (in a particular situation) of a given type, the exception thrown can only have that type or a type derived from that type so that an exception handler for the base type can catch it.

Functions from the C standard library can only throw exceptions when such a function calls a program-supplied function that throws an exception ( and meet this condition).

Destructor operations defined in the C++ standard library never throw exceptions. Every destructor in the C++ standard library behaves as if it had a non-throwing exception specification.

Implementation freedom
It is unspecified whether any member or non-member functions in the C++ standard library are defined as inline.

For a non-virtual C++ standard library member function, a different set of member function signatures can be declared, provided that any call to that member function that would select an overload from the given set of declarations behaves as if that overload was selected. This allows, for instance:
 * adding parameters with default arguments,
 * replacing a member function with default arguments with two or more member functions with equivalent behavior, or
 * adding additional signatures for a member function name.

Unless otherwise specified, it is implementation-defined which functions in the C++ standard library may be recursively reentered.

It is unspecified whether any function signature or class in the C++ standard library is a friend of another class in the C++ standard library.

The names and global function signatures described here are reserved to the implementation.

Any class in the C++ standard library can be derived from a class with a name reserved to the implementation. If a class defined in the C++ standard library is required to be derived from other classes in the C++ standard library, that class can be derived directly from the required base or indirectly through a hierarchy of base classes with names reserved to the implementation.

If a function defined in the C++ standard library is not specified to throw an exception but does not have a non-throwing exception specification, the exception thrown is implementation-defined, but its type should be std or any type derived from std.

The exception specification for a non-virtual function can be strengthened by adding a non-throwing exception specification.