cpp/standard library: Difference between revisions
From cppreference.com
< cpp
m Minor fix. |
D41D8CD98F (talk | contribs) →C standard library: stdchkint.h => stdckdint.h |
||
| (42 intermediate revisions by 11 users not shown) | |||
| Line 1: | Line 1: | ||
{{title|C++ Standard Library}} | {{title|C++ Standard Library}} | ||
{{cpp/navbar}} | {{cpp/navbar}} | ||
The C++ standard library provides a wide range of facilities that are usable in standard C++. | The C++ standard library provides a wide range of facilities that are usable in standard C++. | ||
| Line 13: | Line 13: | ||
The [[cpp/error|diagnostics library]] provides a consistent framework for reporting errors in a C++ program, including [[cpp/error#Exception categories|predefined exception classes]]. | The [[cpp/error|diagnostics library]] provides a consistent framework for reporting errors in a C++ program, including [[cpp/error#Exception categories|predefined exception classes]]. | ||
The [[cpp/memory|memory management library]] provides components for memory management, including | The [[cpp/memory|memory management library]] provides components for memory management, including cpp/memory#Smart pointers{{rev inl|since=c++11| and [[cpp/memory/scoped_allocator_adaptor|scoped allocator]]}}. | ||
{{rrev|since=c++11| | {{rrev|since=c++11| | ||
The [[cpp/meta|metaprogramming library]] describes facilities for use in [[cpp/language/templates|templates]] and during constant evaluation, including [[cpp/meta#Type traits|type traits]]{{rev inl|since=c++14|, | The [[cpp/meta|metaprogramming library]] describes facilities for use in [[cpp/language/templates|templates]] and during constant evaluation, including [[cpp/meta#Type traits|type traits]]{{rev inl|since=c++14|, cpp/utility/integer sequence,}} and [[cpp/numeric/ratio|rational arithmetic]]. | ||
}} | }} | ||
The [[cpp/utility#General-purpose utilities|general utilities library]] includes components used by other library elements, such as a [[cpp/memory#Allocators|predefined storage allocator]] for dynamic storage management, and components used as infrastructure in C++ programs, such as{{rev inl|since=c++11| | The [[cpp/utility#General-purpose utilities|general utilities library]] includes components used by other library elements, such as a [[cpp/memory#Allocators|predefined storage allocator]] for dynamic storage management, and components used as infrastructure in C++ programs, such as{{rev inl|since=c++11| cpp/utility/tupleand}} [[cpp/utility/functional|function wrappers]]. | ||
The | The {{|}}, {{|}}{{rev inl|since=c++|, {{|}}}}, {{|}}and . | ||
The [[cpp/ | The [[cpp/|]] , /|{{rev inl|since=c++|, /|}}, /|and . | ||
The [[cpp/numeric|numerics library]] provides | |||
The [[cpp/numeric|numerics library]] provides cpp/numeric#Numeric algorithmsand [[cpp/numeric/complex|complex number]] components that extend support for numeric processing. The cpp/numeric/valarraycomponent provides support for n-at-a-time processing, potentially implemented as parallel operations on platforms that support such processing.{{rev inl|since=c++11| The [[cpp/numeric/random|random number component]] provides facilities for generating pseudo-random numbers.}} | |||
The [[cpp/chrono|time library]] provides generally useful time utilities. | The [[cpp/chrono|time library]] provides generally useful time utilities. | ||
The [[cpp/io|input/output library]] provides the [[cpp/io#Stream-based I/O|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. | |||
The[[cpp/io|input/output library]] provides the [[cpp/io#Stream-based I/O|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. | |||
{{rrev|since=c++11| | {{rrev|since=c++11| | ||
The [[cpp/thread|thread support library]] provides components to create and manage threads, including [[cpp/atomic/atomic|atomic operations]], [[cpp/thread/mutex|mutual exclusion]], and inter-thread communication. | The [[cpp/thread|thread support library]] provides components to create and manage threads, including [[cpp/atomic/atomic|atomic operations]], [[cpp/thread/mutex|mutual exclusion]], and inter-thread communication. | ||
}} | }} | ||
| Line 42: | Line 44: | ||
The C++ standard library provides definitions for the [[cpp/language/basic concepts|entities]] and [[cpp/preprocessor/replace|macros]] described in the synopses of the [[cpp/headers|C++ standard library headers]], unless otherwise specified. | The C++ standard library provides definitions for the [[cpp/language/basic concepts|entities]] and [[cpp/preprocessor/replace|macros]] described in the synopses of the [[cpp/headers|C++ standard library headers]], unless otherwise specified. | ||
All library entities except {{ | All library entities except {{|operator new}} and {{|operator delete}} are defined within the namespace {{c|std}} or cpp/language/namespacenested within namespace {{c|std}} (except the entities for the C standard library facilities, see below).{{rev inl|since=c++11| It is unspecified whether names declared in a specific namespace are declared directly in that namespace or in an [[cpp/language/namespace#Inline namespaces|inline namespace]] inside that namespace.}} | ||
====Headers==== | ====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 | 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 and 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 | The C++ standard library provides the ''C++ library headers'' and additional C++ headers for C library facilities(see cpp/headerspage for descriptions): | ||
{|class="wikitable" | {|class="wikitable" | ||
|-style="text-align:center; font-size:16px; line-height:16px;" | |-style="text-align: center; font-size: 16px; line-height: 16px;" | ||
!colspan="5"|C++ library headers | !colspan="5"|C++ library headers | ||
|- | |- | ||
|{{ | |{{header|algorithm}} | ||
|{{ | |{{header|iomanip}} | ||
|{{ | |{{header|list}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|- | |- | ||
|{{ | |{{header|bitset}} | ||
|{{ | |{{header|ios}} | ||
|{{ | |{{header|locale}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|- | |- | ||
|{{ | |{{header|complex}} | ||
|{{ | |{{header|iosfwd}} | ||
|{{ | |{{header|map}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|typeinfo}} | ||
|- | |- | ||
|{{ | |{{header|deque}} | ||
|{{ | |{{header|iostream}} | ||
|{{ | |{{header|memory}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|utility}} | ||
|- | |- | ||
|{{ | |{{header|exception}} | ||
|{{ | |{{header|istream}} | ||
|{{ | |{{header|new}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|valarray}} | ||
|- | |- | ||
|{{ | |{{header|fstream}} | ||
|{{ | |{{header|iterator}} | ||
|{{ | |{{header|numeric}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|vector}} | ||
|- | |- | ||
|{{ | |{{header|functional}} | ||
|{{ | |{{header|limits}} | ||
| | | | ||
| | | | ||
| | | | ||
| Line 96: | Line 98: | ||
!colspan="5"|Headers added in C++11 | !colspan="5"|Headers added in C++11 | ||
|- | |- | ||
|{{ | |{{header|array}} | ||
|{{ | |{{header|condition_variable}} | ||
|{{ | |{{header|mutex}} | ||
|{{ | |{{header|scoped_allocator}} | ||
|{{ | |{{header|type_traits}} | ||
|- | |- | ||
|{{ | |{{header|atomic}} | ||
|{{ | |{{header|forward_list}} | ||
|{{ | |{{header|random}} | ||
|{{ | |{{header|system_error}} | ||
|{{ | |{{header|typeindex}} | ||
|- | |- | ||
|{{ | |{{ | ||
|{{ | |||
|{{ | |||
|{{ | | | ||
header|}} | |||
|{{ | |||
|header|}} | |||
|{{| | |||
header|}} | |||
|{{ | |||
| | |||
header|}} | |||
| | | | ||
| | | | ||
|-style="text-align:center" | |-style="text-align:center" | ||
!colspan="5"|Headers added in C++17 | !colspan="5"|Headers added in C++17 | ||
|- | |- | ||
|{{ | |{{header|any}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|string_view}} | ||
|{{ | |{{header|variant}} | ||
|- | |- | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
| | | | ||
| | | | ||
| Line 136: | Line 146: | ||
!colspan="5"|Headers added in C++20 | !colspan="5"|Headers added in C++20 | ||
|- | |- | ||
|{{ | |{{header|barrier}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|- | |- | ||
|{{ | |{{header|bit}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|- | |- | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|-style="text-align:center" | |-style="text-align:center" | ||
!colspan="5"|Headers added in C++23 | !colspan="5"|Headers added in C++23 | ||
|- | |- | ||
|{{ | |{{ | ||
|{{ | |||
|{{ | |||
|{{ | |||
|{{ | | | ||
header|}} | |||
|{{ | |||
|header|}} | |||
|{{ | |||
|header|}} | |||
|{{ | |||
|header|}} | |||
|{{| | |||
header|}} | |||
|- | |- | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
|{{ | |{{header|}} | ||
| | | | ||
|} | |} | ||
{|class="wikitable" | {|class="wikitable" | ||
|-style="text-align:center; font-size:16px; line-height:16px;" | |-style="text-align:center; font-size:16px; line-height:16px;" | ||
! colspan="4" | C++ headers for C library facilities | !colspan="4"|C++ headers for C library facilities | ||
|- | |- | ||
|{{ | |{{header|cassert}} | ||
|{{ | |{{header|clocale}} | ||
|{{ | |{{header|cstdarg}} | ||
|{{ | |{{header|cstring}} | ||
|- | |- | ||
|{{ | |{{header|cctype}} | ||
|{{ | |{{header|cmath}} | ||
|{{ | |{{header|cstddef}} | ||
|{{ | |{{header|ctime}} | ||
|- | |- | ||
|{{ | |{{header|cerrno}} | ||
|{{ | |{{header|csetjmp}} | ||
|{{ | |{{header|cstdio}} | ||
|{{ | |{{header|cwchar}} | ||
|- | |- | ||
|{{ | |{{header|cfloat}} | ||
|{{ | |{{header|csignal}} | ||
|{{ | |{{header|cstdlib}} | ||
|{{ | |{{header|cwctype}} | ||
|- | |- | ||
|{{ | |{{header|climits}} | ||
| | | | ||
| | | | ||
| | | | ||
|-style="text-align:center" | |-style="text-align:center" | ||
!colspan="4"|Headers | !colspan="4"|Headers in C++ | ||
|- | |- | ||
|{{ | |{{| | ||
| | header|}} | ||
| | | | ||
| | || | ||
|-style="text-align:center" | |-style="text-align:center" | ||
!colspan="4"| | ! | ||
colspan="4"|++11 | |||
|- | |- | ||
|{{ | |{{header|}} | ||
|colspan="4"|++11deprecated++17removed++20 | |||
| | |||
|- | |- | ||
|{{ | |{{header|}} | ||
| | ||{{|||}} | ||
|{{ | |||
|} | |} | ||
| Line 225: | Line 260: | ||
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. | 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. | 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 [[cpp/headers#C compatibility headers|here]] for descriptions. | 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 [[cpp/headers#C compatibility headers|here]] for descriptions. | ||
| Line 273: | Line 308: | ||
|{{ltt|cpp/header/stdatomic.h|<stdatomic.h>}} | |{{ltt|cpp/header/stdatomic.h|<stdatomic.h>}} | ||
| | | | ||
| | | | ||
| | | | ||
|} | |} | ||
Except otherwise noted, the contents of each header {{c | Except otherwise noted, the contents of each header {{c}} is the same as that of the corresponding header {{|.h}} as specified in the [[c/header|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 {{c|std}}. It is unspecified whether these names (including any overloads added) are first declared within the global namespace scope and are then injected into namespace {{c|std}} by explicit cpp/language/namespace#Using-declarations. | ||
Names which are defined as macros in C ({{ltt|cpp/error/ | Names which are defined as macros in C ({{ltt|cpp/error/assert}}, {{ltt|cpp/types/offsetof}}, {{ltt|cpp/utility/program/setjmp}}, {{ltt|cpp/utility/variadic/va_arg}}, {{ltt|cpp/utility/variadic/va_end}} and {{ltt|cpp/utility/variadic/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 [[cpp/language/inline|inline function]]. | 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 [[cpp/language/inline|inline function]]. | ||
| Line 292: | Line 334: | ||
!colspan="4"|C standard Annex K names | !colspan="4"|C standard Annex K names | ||
|- | |- | ||
|{{ | |{{|abort_handler_s}} | ||
|{{ | |{{|mbstowcs_s}} | ||
|{{ | |{{|strncat_s}} | ||
|{{ | |{{|vswscanf_s}} | ||
|- | |- | ||
|{{ | |{{|asctime_s}} | ||
|{{ | |{{|memcpy_s}} | ||
|{{ | |{{|strncpy_s}} | ||
|{{ | |{{|vwprintf_s}} | ||
|- | |- | ||
|{{ | |{{|bsearch_s}} | ||
|{{ | |{{|memmove_s}} | ||
|{{ | |{{|strtok_s}} | ||
|{{ | |{{|vwscanf_s}} | ||
|- | |- | ||
|{{ | |{{|constraint_handler_t}} | ||
|{{ | |{{|memset_s}} | ||
|{{ | |{{|swprintf_s}} | ||
|{{ | |{{|wcrtomb_s}} | ||
|- | |- | ||
|{{ | |{{|ctime_s}} | ||
|{{ | |{{|printf_s}} | ||
|{{ | |{{|swscanf_s}} | ||
|{{ | |{{|wcscat_s}} | ||
|- | |- | ||
|{{ | |{{|errno_t}} | ||
|{{ | |{{|qsort_s}} | ||
|{{ | |{{|tmpfile_s}} | ||
|{{ | |{{|wcscpy_s}} | ||
|- | |- | ||
|{{ | |{{|fopen_s}} | ||
|{{ | |{{|RSIZE_MAX}} | ||
|{{ | |{{|TMP_MAX_S}} | ||
|{{ | |{{|wcsncat_s}} | ||
|- | |- | ||
|{{ | |{{|fprintf_s}} | ||
|{{ | |{{|rsize_t}} | ||
|{{ | |{{|tmpnam_s}} | ||
|{{ | |{{|wcsncpy_s}} | ||
|- | |- | ||
|{{ | |{{|freopen_s}} | ||
|{{ | |{{|scanf_s}} | ||
|{{ | |{{|vfprintf_s}} | ||
|{{ | |{{|wcsnlen_s}} | ||
|- | |- | ||
|{{ | |{{|fscanf_s}} | ||
|{{ | |{{|set_constraint_handler_s}} | ||
|{{ | |{{|vfscanf_s}} | ||
|{{ | |{{|wcsrtombs_s}} | ||
|- | |- | ||
|{{ | |{{|fwprintf_s}} | ||
|{{ | |{{|snprintf_s}} | ||
|{{ | |{{|vfwprintf_s}} | ||
|{{ | |{{|wcstok_s}} | ||
|- | |- | ||
|{{ | |{{|fwscanf_s}} | ||
|{{ | |{{|snwprintf_s}} | ||
|{{ | |{{|vfwscanf_s}} | ||
|{{ | |{{|wcstombs_s}} | ||
|- | |- | ||
|{{ | |{{|gets_s}} | ||
|{{ | |{{|sscanf_s}} | ||
|{{ | |{{|}} | ||
|{{ | |{{|wmemcpy_s}} | ||
|- | |- | ||
|{{ | |{{|gmtime_s}} | ||
|{{ | |{{|mbstowcs_s}} | ||
|{{ | |{{|}} | ||
|{{ | |{{|vswscanf_s}} | ||
|- | |- | ||
|{{ | |{{|abort_handler_s}} | ||
|{{ | |{{|strcat_s}} | ||
|{{ | |{{|vsnprintf_s}} | ||
|{{ | |{{|wmemmove_s}} | ||
|- | |- | ||
|{{ | |{{|ignore_handler_s}} | ||
|{{ | |{{|strcpy_s}} | ||
|{{ | |{{|vsnwprintf_s}} | ||
|{{ | |{{|wprintf_s}} | ||
|- | |- | ||
|{{ | |{{|localtime_s}} | ||
|{{ | |{{|strerrorlen_s}} | ||
|{{ | |{{|vsprintf_s}} | ||
|{{ | |{{|wscanf_s}} | ||
|- | |- | ||
|{{ | |{{|L_tmpnam_s}} | ||
|{{ | |{{|strerror_s}} | ||
|{{ | |{{|vsscanf_s}} | ||
| | | | ||
|- | |- | ||
|{{ | |{{|mbsrtowcs_s}} | ||
|{{ | |{{|strlen_s}} | ||
|{{ | |{{|vswprintf_s}} | ||
| | | | ||
|} | |} | ||
| Line 392: | Line 434: | ||
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 {{ltt|cpp/preprocessor/include|#include}} preprocessing directive. | 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 {{ltt|cpp/preprocessor/include|#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 {{ | 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 {{header|cassert}} or {{ltt|cpp/header/cassert|<assert.h>}} depends each time on the lexically current definition of {{c|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. | 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| | {{rrev|since=c++20| | ||
In [[cpp/language/modules|module units]], headers can only be included in | In [[cpp/language/modules|module units]], headers can only be included in cpp/language/modules#Global module fragment. | ||
}} | }} | ||
{{ | {{|since=c++20| | ||
====Importing headers==== | ====Importing headers==== | ||
The [[#Headers|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 [[#Headers|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 [[cpp/language/modules#Importing modules and headers|import declaration]]. | The contents of importable C++ library headers are made available to a translation unit when it contains the appropriate [[cpp/language/modules#Importing modules and headers|import declaration]]. | ||
}} | }} | ||
{{|since=c++23| | |||
{{ | |||
====Importing modules==== | ====Importing modules==== | ||
The C++ standard library provides the following ''C++ library modules'' : | The C++ standard library provides the following ''C++ library modules'': | ||
* The [[cpp/language/modules#Module declarations|named module]] {{c|std}} exports declarations in namespace {{tt|std}} that are provided by the importable C++ library headers and the [[#Headers|C++ headers for C library facilities]]. It additionally exports declarations in the global namespace for the storage [[cpp/memory/new/operator new|allocation]] and [[cpp/memory/new/operator delete|deallocation]] functions that are provided by {{ | * The [[cpp/language/modules#Module declarations|named module]] {{c|std}} exports declarations in namespace {{tt|std}} that are provided by the importable C++ library headers and the [[#Headers|C++ headers for C library facilities]] . It additionally exports declarations in the global namespace for the storage [[cpp/memory/new/operator new|allocation]] and [[cpp/memory/new/operator delete|deallocation]] functions that are provided by {{|new|new}}. | ||
* The named module {{c|std.compat}} exports the same declarations as the named module {{c|std}}, and additionally exports declarations in the global namespace corresponding to the declarations in namespace {{tt|std}} that are provided by the C++ headers for C library facilities. | * The named module {{c|std.compat}} exports the same declarations as the named module {{c|std}}, and additionally exports declarations in the global namespace corresponding to the declarations in namespace {{tt|std}} that are provided by the C++ headers for C library facilities . | ||
For each declaration in the standard library, | For each declaration in the standard library, | ||
* the module it [[cpp/language/modules#Module ownership|attaches to]] is unspecified, and | * the module it [[cpp/language/modules#Module ownership|attaches to]] is unspecified, and | ||
* it denotes the same [[cpp/language/basic concepts|entity]] regardless of whether it was made reachable through including a header, importing a header unit, or importing a C++ library module. | * it denotes the same [[cpp/language/basic concepts|entity]] regardless of whether it was made reachable through including a header, importing a header unit, or importing a C++ library module. | ||
}} | }} | ||
====Linkage==== | ====Linkage==== | ||
Entities in the C++ standard library have | Entities in the C++ standard library have cpp/language/storage duration#external linkage. Unless otherwise specified, objects and functions have the default {{c|extern "C++"}} [[cpp/language/language linkage|linkage]]. | ||
Whether a name from the C standard library declared with external linkage has {{c|extern "C"}} or {{c|extern "C++"}} linkage is implementation-defined. The C++ standard recommends using {{c|extern "C++"}} in this case. | Whether a name from the C standard library declared with external linkage has {{c|extern "C"}} or {{c|extern "C++"}} linkage is implementation-defined. The C++ standard recommends using {{c|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. | Objects and functions defined in the library and required by a C++ program are included in the program prior to program startup. | ||
| Line 430: | Line 470: | ||
===Requirements on standard library implementations=== | ===Requirements on standard library implementations=== | ||
====Guarantees==== | ====Guarantees==== | ||
A C++ header must provide | A C++ header must provide cpp/language/declarations|cpp/language/definitionthat appear in | ||
* the synopsis of that header, or | * the synopsis of that header, or | ||
* the synopsis of another header which is appeared to be included in the synopsis of that header. | * the synopsis of another header which is appeared to be included in the synopsis of that header. | ||
| Line 436: | Line 476: | ||
For types and macros defined in multiple headers (such as {{ltt|cpp/types/NULL}}), including any number of these headers in any order never violates the [[cpp/language/definition#One Definition Rule|one definition rule]]. | For types and macros defined in multiple headers (such as {{ltt|cpp/types/NULL}}), including any number of these headers in any order never violates the [[cpp/language/definition#One Definition Rule|one definition rule]]. | ||
Unless otherwise specified, all | Unless otherwise specified, all cpp/preprocessor/replace#Object-like macrosdefined by the C standard library that expand to integral cpp/language/constant expressioncan be used in [[cpp/preprocessor/conditional|{{tt|#if}}]] 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. | 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 | Non-member function signatures are never declared with additional cpp/language/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 | Unless otherwise specified, calls made by functions in the standard library to non-operator, non-member functions do not use functions from another cpp/language/namespacewhich are found through [[cpp/language/adl|argument-dependent name lookup]]. | ||
For each [[cpp/language/friend|friend declaration]] of a function (template) within a class (template) definition, no other declaration is provided for that function (template). | For each [[cpp/language/friend|friend declaration]] of a function (template) within a class (template) definition, no other declaration is provided for that function (template). | ||
{{rrev|since=c++11|1= | {{rrev|since=c++11|1= | ||
Standard library function signatures can only be declared as {{c|constexpr}} if they are required to be | Standard library function signatures can only be declared as {{c|constexpr}} if they are required to be cpp/language/constexpr(libstdc++ cmath [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102916 is notably non-conforming] here). If a header provides any non-defining declarations of constexpr functions or constructors, the corresponding definitions should also be provided within that header. | ||
Unless otherwise specified, each standard library function should meet each of the following requirements to prevent [[cpp/language/memory model#Threads and data races|data races]]: | Unless otherwise specified, each standard library function should meet each of the following requirements to prevent [[cpp/language/memory model#Threads and data races|data races]]: | ||
| Line 453: | Line 493: | ||
* A C++ standard library function cannot (directly or indirectly) modify objects accessible by threads other than the current thread unless the objects are accessed (directly or indirectly) via the function’s non-const arguments, including {{c|this}}. | * A C++ standard library function cannot (directly or indirectly) modify objects accessible by threads other than the current thread unless the objects are accessed (directly or indirectly) via the function’s non-const arguments, including {{c|this}}. | ||
** For example, an object with static storage duration cannot be used for internal purposes without synchronization because doing so can cause a data race even in programs that do not explicitly share objects between threads. | ** For example, an object with static storage duration cannot be used for internal purposes without synchronization because doing so can cause a data race even in programs that do not explicitly share objects between threads. | ||
* A C++ standard library function cannot access objects indirectly accessible via its arguments or via elements of its | * A C++ standard library function cannot access objects indirectly accessible via its arguments or via elements of its cpp/containerarguments except by invoking functions required by its specification on those container elements. | ||
* An operation on | * An operation on cpp/iteratorobtained by calling a standard library container or string member function can access, but not modify, the underlying container. | ||
** In particular, container operations that invalidate iterators conflict with operations on iterators associated with that container. | ** In particular, container operations that invalidate iterators conflict with operations on iterators associated with that container. | ||
* A C++ standard library function can only perform all operations solely within the current thread if those operations have effects that are [[cpp/language/memory model#Memory order|visible]] to users. | * A C++ standard library function can only perform all operations solely within the current thread if those operations have effects that are [[cpp/language/memory model#Memory order|visible]] to users. | ||
| Line 461: | Line 501: | ||
For each class defined in the C++ standard library required to be [[cpp/language/derived class|derived]] from another class defined in the C++ standard library, | For each class defined in the C++ standard library required to be [[cpp/language/derived class|derived]] from another class defined in the C++ standard library, | ||
* the base class must be [[cpp/language/derived class#Virtual base classes|virtual]] if it is specified as {{c|virtual}}, | * the base class must be [[cpp/language/derived class#Virtual base classes|virtual]] if it is specified as {{c|virtual}}, | ||
* the base class cannot be virtual if it is not specified as {{c|virtual}}, and | * the base class cannot be virtual if it is not specified as {{c|virtual}}, and | ||
* unless otherwise specified, types with distinct names shall be distinct types. | * unless otherwise specified, types with distinct names shall be distinct types. | ||
{{rrev|since=c++11| | {{rrev|since=c++11| | ||
Unless otherwise specified, all types specified in the C++ standard library are non- | Unless otherwise specified, all types specified in the C++ standard library are non-cpp/language/finaltypes. | ||
}} | }} | ||
If a function defined in the C++ standard library is specified to throw an [[cpp/language/exceptions|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. | If a function defined in the C++ standard library is specified to throw an [[cpp/language/exceptions|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 ({{ | Functions from the C standard library can only throw exceptions when such a function calls a program-supplied function that throws an exception ({{|cpp/algorithm/qsort}} and {{|cpp/algorithm/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 [[cpp/language/noexcept spec|non-throwing exception specification]]. | Destructor operations defined in the C++ standard library never throw exceptions. Every destructor in the C++ standard library behaves as if it had a [[cpp/language/noexcept spec|non-throwing exception specification]]. | ||
{{rrev|since=c++11| | {{rrev|since=c++11| | ||
If a function in the C++ standard library report errors via a {{lc|std::error_code}} object, that object's {{ | If a function in the C++ standard library report errors via a {{lc|std::error_code}} object, that object's {{|cpp/error/error_code/category}} member must return {{lc|std::system_category()}} for errors originating from the operating system, or a reference to an implementation-defined {{lc|std::error_category}} object for errors originating elsewhere. The possible values of {{|cpp/error/error_code/value}} for each of these error categories should be defined. | ||
Objects of types defined in the C++ standard library may be [[cpp/language/move constructor|moved from]]. Move operations can either be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects will be placed in a valid but unspecified state. | Objects of types defined in the C++ standard library may be [[cpp/language/move constructor|moved from]]. Move operations can either be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects will be placed in a valid but unspecified state. | ||
| Line 484: | Line 524: | ||
====Implementation freedom==== | ====Implementation freedom==== | ||
It is unspecified whether any member or non-member functions in the C++ standard library are defined as | It is unspecified whether any member or non-member functions in the C++ standard library are defined as cpp/language/inline. | ||
For a non- | For a non-cpp/language/virtualC++ 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, | * adding parameters with default arguments, | ||
* replacing a member function with default arguments with two or more member functions with equivalent behavior, or | * replacing a member function with default arguments with two or more member functions with equivalent behavior, or | ||
| Line 506: | Line 546: | ||
The exception specification for a non-virtual function can be strengthened by adding a non-throwing exception specification. | The exception specification for a non-virtual function can be strengthened by adding a non-throwing exception specification. | ||