C++ Standard Library
C++标准库提供了可在标准C++中使用的广泛功能设施。
目录分类语言支持库 提供了C++语言某些部分所需的组件,例如内存分配( new / delete )和 异常处理 。
诊断库 为C++程序中的错误报告提供了一致的框架,包括 预定义的异常类 。 内存管理库 提供了内存管理的相关组件,包括 智能指针 和 作用域分配器 (自C++11起) 。
通用工具库 包含被其他库组件所使用的元素,例如用于动态存储管理的 预定义存储分配器 ,以及作为C++程序基础设施的组件,例如 元组 和 (自C++11起) 函数包装器 。 容器 、 迭代器 、 范围 (C++20 起) 以及 算法 库为 C++ 程序提供了对最常用算法和数据结构子集的访问能力。 字符串库 提供了对以同构序列形式表示的文本进行操作的支持,这些序列包含以下类型: char , char8_t (自 C++20 起) , char16_t , char32_t (自 C++11 起) , wchar_t 以及其他任何类字符类型。 文本处理库 提供 正则表达式 匹配与搜索 (自 C++11 起) 、 文本格式化 工具 (自 C++20 起) 、 文本编码识别 (自 C++26 起) 以及 本地化设施 。 数值库 提供了 数值算法 和 复数 组件,用于扩展数值处理的支持。 valarray 组件支持批量处理,在支持并行操作的平台上可能实现为并行操作。 随机数组件 提供了生成伪随机数的功能。 (C++11 起) 时间库 提供了通用的时间工具。 输入/输出库 提供了 基于流的输入/输出组件 ,这些是C++程序进行输入输出的主要机制。它们可以与库中的其他元素结合使用,特别是字符串、区域设置和迭代器。
Library contentsThe 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 std or namespaces nested within namespace std (except the entities for the C standard library facilities, see below). It is unspecified whether names declared in a specific namespace are declared directly in that namespace or in an inline namespace inside that namespace. (since C++11) 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
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 libraryThe 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 (including those implied by C's restrict ) (since C++17) 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
Names which are defined as macros in C ( assert , offsetof , setjmp , va_arg , va_end and va_start ) 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 <iso646.h> has no effect. Names associated with safe functions in standard C (since C++17)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 std ): Using the libraryIncluding headersThe entities in the C++ standard library are defined in headers, whose contents are made available to a translation unit when it contains the appropriate #include 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 <cassert> or <assert.h> depends each time on the lexically current definition of NDEBUG . 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.
LinkageEntities in the C++ standard library have storage duration#external linkage . Unless otherwise specified, objects and functions have the default extern "C++" linkage . Whether a name from the C standard library declared with external linkage has extern "C" or extern "C++" linkage is implementation-defined. The C++ standard recommends using extern "C++" 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. Requirements on standard library implementationsGuaranteesA C++ header must provide declarations and definitions that appear in
For types and macros defined in multiple headers (such as NULL ), 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
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,
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 ( qsort() and bsearch() 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 freedomIt 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:
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::exception or any type derived from std::exception . The exception specification for a non-virtual function can be strengthened by adding a non-throwing exception specification.
Noteslibstdc++ , libc++ , and STL all support using standard library modules in C++20 mode. 示例运行此代码 import std;
struct Str : std::string // OK, std::string cannot be final
{
~Str(); // Guaranteed to be noexcept
};
int main()
{
std::puts("Hello stdlib!");
// ::puts("Hello stdlib!"); // Requires std.compat module or stdio.h header
// constexpr auto& void_info = std::any().type(); // std::any::type cannot be constexpr
std::string t;
t = std::move(t); // OK, t is left in a valid (but unspecified) state
}
输出: Hello stdlib!
Defect reportsThe following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||