[requirements] (original) (raw)
16 Library introduction [library]
16.5 Library-wide requirements [requirements]
This subclause specifies requirements that apply to the entire C++ standard library.
[support] through [thread] and [depr]specify the requirements of individual entities within the library.
Requirements specified in terms of interactions between threads do not apply to programs having only a single thread of execution.
Within this subclause, [organization] describes the library's contents and organization, [using] describes how well-formed C++ programs gain access to library entities,[utility.requirements] describes constraints on types and functions used with the C++ standard library,[constraints] describes constraints on well-formed C++ programs, and[conforming] describes constraints on conforming implementations.
16.5.1 Library contents and organization [organization]
[contents] describes the entities and macros defined in the C++ standard library.
[headers] lists the standard library headers and some constraints on those headers.
[compliance] lists requirements for a freestanding implementation of the C++ standard library.
16.5.1.1 Library contents [contents]
The C++ standard library provides definitions for the entities and macros described in the synopses of the C++ standard library headers ([headers]), unless otherwise specified.
All library entities exceptoperator newandoperator deleteare defined within the namespacestdor namespaces nested within namespacestd.162
It is unspecified whether names declared in a specific namespace are declared directly in that namespace or in an inline namespace inside that namespace.163
Whenever a name x defined in the standard library is mentioned, the name x is assumed to be fully qualified as::std::x, unless explicitly described otherwise.
For example, if the Effects: element for library function F is described as calling library function G, the function::std::Gis meant.
16.5.1.3 Freestanding implementations [compliance]
Two kinds of implementations are defined:hostedandfreestanding ([intro.compliance]); the kind of the implementation isimplementation-defined.
For a hosted implementation, this document describes the set of available headers.
A freestanding implementation has animplementation-defined set of headers.
This set shall include at least the headers shown in Table 24.
The supplied version of the header <cstdlib> ([cstdlib.syn]) shall declare at least the functionsabort,atexit,at_quick_exit,exit, andquick_exit ([support.start.term]).
The supplied version of the header <atomic> ([atomics.syn]) shall meet the same requirements as for a hosted implementation except that support for always lock-free integral atomic types ([atomics.lockfree]) is implementation-defined, and whether or not the type aliases atomic_signed_lock_free andatomic_unsigned_lock_free are defined ([atomics.alias]) is implementation-defined.
The other headers listed in this table shall meet the same requirements as for a hosted implementation.
16.5.2 Using the library [using]
16.5.2.1 Overview [using.overview]
Subclause [using] describes how a C++ program gains access to the facilities of the C++ standard library.
[using.headers] describes effects during translation phase 4, while [using.linkage] describes effects during phase 8.
16.5.2.3 Linkage [using.linkage]
Unless otherwise specified, objects and functions have the defaultextern "C++"linkage ([dcl.link]).
Whether a name from the C standard library declared with external linkage hasextern "C"orextern "C++"linkage is implementation-defined.
It is recommended that an implementation useextern "C++"linkage for this purpose.170
Objects and functions defined in the library and required by a C++ program are included in the program prior to program startup.
16.5.3 Requirements on types and expressions [utility.requirements]
[utility.arg.requirements]describes requirements on types and expressions used to instantiate templates defined in the C++ standard library.
[swappable.requirements] describes the requirements on swappable types and swappable expressions.
[nullablepointer.requirements] describes the requirements on pointer-like types that support null values.
[hash.requirements] describes the requirements on hash function objects.
[allocator.requirements] describes the requirements on storage allocators.
16.5.3.1 Template argument requirements [utility.arg.requirements]
The template definitions in the C++ standard library refer to various named requirements whose details are set out in Tables 25–32.
In these tables, T is an object or reference type to be supplied by a C++ program instantiating a template;a,b, andc are values of type (possibly const) T;s and t are modifiable lvalues of type T;u denotes an identifier;rv is an rvalue of type T; and v is an lvalue of type (possibly const) T or an rvalue of type const T.
In general, a default constructor is not required.
Certain container class member function signatures specify T() as a default argument.
T() shall be a well-defined expression ([dcl.init]) if one of those signatures is called using the default argument.
Table 25: Cpp17EqualityComparable requirements [tab:cpp17.equalitycomparable]
| Expression | Return type | Requirement |
|---|---|---|
| a == b | convertible to bool | == is an equivalence relation, that is, it has the following properties:For all a, a == a.If a == b, then b == a.If a == b and b == c, then a == c. |
Table 26: Cpp17LessThanComparable requirements [tab:cpp17.lessthancomparable]
| Expression | Return type | Requirement |
|---|---|---|
| a < b | convertible to bool |
Table 27: Cpp17DefaultConstructible requirements [tab:cpp17.defaultconstructible]
| Expression | Post-condition |
|---|---|
| T t; | object t is default-initialized |
| T u{}; | object u is value-initialized or aggregate-initialized |
| T()T{} | an object of type T is value-initialized or aggregate-initialized |
Table 28: Cpp17MoveConstructible requirements [tab:cpp17.moveconstructible]
| Expression | Post-condition |
|---|---|
| T u = rv; | u is equivalent to the value of rv before the construction |
| T(rv) | T(rv) is equivalent to the value of rv before the construction |
| rv's state is unspecified[ Note: rv must still meet the requirements of the library component that is using it. The operations listed in those requirements must work as specified whether rv has been moved from or not. — end note ] |
Table 29: Cpp17CopyConstructible requirements (in addition to Cpp17MoveConstructible) [tab:cpp17.copyconstructible]
| Expression | Post-condition |
|---|---|
| T u = v; | the value of v is unchanged and is equivalent to u |
| T(v) | the value of v is unchanged and is equivalent to T(v) |
Table 30: Cpp17MoveAssignable requirements [tab:cpp17.moveassignable]
| Expression | Return type | Return value | Post-condition |
|---|---|---|---|
| t = rv | T& | t | If t and rv do not refer to the same object,t is equivalent to the value of rv before the assignment |
| rv's state is unspecified. [ Note:rv must still meet the requirements of the library component that is using it, whether or not t and rv refer to the same object. The operations listed in those requirements must work as specified whether rv has been moved from or not. — end note ] |
Table 31: Cpp17CopyAssignable requirements (in addition to Cpp17MoveAssignable) [tab:cpp17.copyassignable]
| Expression | Return type | Return value | Post-condition |
|---|---|---|---|
| t = v | T& | t | t is equivalent to v, the value of v is unchanged |
Table 32: Cpp17Destructible requirements [tab:cpp17.destructible]
| Expression | Post-condition |
|---|---|
| u.~T() | All resources owned by u are reclaimed, no exception is propagated. |
| [ Note:Array types and non-object types are not Cpp17Destructible. — end note ] |
16.5.3.2 Swappable requirements [swappable.requirements]
This subclause provides definitions for swappable types and expressions.
In these definitions, let t denote an expression of type T, and let udenote an expression of type U.
An object t is swappable with an object u if and only if:
- the expressions swap(t, u) and swap(u, t) are valid when evaluated in the context described below, and
- these expressions have the following effects:
- the object referred to by t has the value originally held by u and
- the object referred to by u has the value originally held by t.
The context in which swap(t, u) and swap(u, t) are evaluated shall ensure that a binary non-member function named “swap” is selected via overload resolution on a candidate set that includes:
- the two swap function templates defined in<utility> ([utility.syn]) and
- the lookup set produced by argument-dependent lookup.
[ Note
:
If T and U are both fundamental types or arrays of fundamental types and the declarations from the header <utility> are in scope, the overall lookup set described above is equivalent to that of the qualified name lookup applied to the expression std::swap(t, u) orstd::swap(u, t) as appropriate.
— end note
]
[ Note
:
It is unspecified whether a library component that has a swappable requirement includes the header <utility> to ensure an appropriate evaluation context.
— end note
]
An rvalue or lvalue t is swappable if and only if t is swappable with any rvalue or lvalue, respectively, of type T.
A type X meeting any of the iterator requirements ([iterator.requirements]) meets the Cpp17ValueSwappable requirements if, for any dereferenceable objectx of type X,*x is swappable.
[ Example
:
User code can ensure that the evaluation of swap calls is performed in an appropriate context under the various conditions as follows:
#include
template<class T, class U> void value_swap(T&& t, U&& u) { using std::swap; swap(std::forward(t), std::forward(u));
}
template
void lv_swap(T& t1, T& t2) {
using std::swap;
swap(t1, t2);
}
namespace N { struct A { int m; }; struct Proxy { A* a; }; Proxy proxy(A& a) { return Proxy{ &a }; }
void swap(A& x, Proxy p) { std::swap(x.m, p.a->m);
}
void swap(Proxy p, A& x) { swap(x, p); }
}
int main() { int i = 1, j = 2; lv_swap(i, j); assert(i == 2 && j == 1);
N::A a1 = { 5 }, a2 = { -5 }; value_swap(a1, proxy(a2)); assert(a1.m == -5 && a2.m == 5); }
— end example
]
16.5.3.3 Cpp17NullablePointer requirements [nullablepointer.requirements]
A Cpp17NullablePointer type is a pointer-like type that supports null values.
A type P meets the Cpp17NullablePointer requirements if:
- P meets the Cpp17EqualityComparable,Cpp17DefaultConstructible, Cpp17CopyConstructible, Cpp17CopyAssignable, and Cpp17Destructible requirements,
- lvalues of type P are swappable,
- the expressions shown in Table 33 are valid and have the indicated semantics, and
- P meets all the other requirements of this subclause.
A value-initialized object of type P produces the null value of the type.
The null value shall be equivalent only to itself.
A default-initialized object of type P may have an indeterminate value.
[ Note
:
Operations involving indeterminate values may cause undefined behavior.
— end note
]
The effect shall be as if p != nullptrhad been evaluated in place of p.
No operation which is part of the Cpp17NullablePointer requirements shall exit via an exception.
In Table 33, u denotes an identifier, tdenotes a non-const lvalue of type P, a and bdenote values of type (possibly const) P, and np denotes a value of type (possibly const) std::nullptr_t.
Table 33: Cpp17NullablePointer requirements [tab:cpp17.nullablepointer]
| Expression | Return type | Operational semantics |
|---|---|---|
| P u(np); | Postconditions: u == nullptr | |
| P u = np; | ||
| P(np) | Postconditions: P(np) == nullptr | |
| t = np | P& | Postconditions: t == nullptr |
| a != b | contextually convertible to bool | !(a == b) |
| a == np | contextually convertible to bool | a == P() |
| np == a | ||
| a != np | contextually convertible to bool | !(a == np) |
| np != a |
16.5.3.4 Cpp17Hash requirements [hash.requirements]
A type H meets the Cpp17Hash requirements if:
- it is a function object type ([function.objects]),
- it meets the Cpp17CopyConstructible (Table 29) andCpp17Destructible (Table 32) requirements, and
- the expressions shown in Table 34are valid and have the indicated semantics.
Given Key is an argument type for function objects of type H, in Table 34 h is a value of type (possibly const) H,u is an lvalue of type Key, and k is a value of a type convertible to (possibly const) Key.
Table 34: Cpp17Hash requirements [tab:cpp17.hash]
| Expression | Return type | Requirement |
|---|---|---|
| h(k) | size_t | The value returned shall depend only on the argument k for the duration of the program. [ Note:Thus all evaluations of the expression h(k) with the same value for k yield the same result for a given execution of the program. — end note ] [ Note:For two different values t1 and t2, the probability that h(t1) and h(t2) compare equal should be very small, approaching 1.0 / numeric_limits<size_t>::max(). — end note ] |
| h(u) | size_t | Shall not modify u. |
16.5.3.5 Cpp17Allocator requirements [allocator.requirements]
The library describes a standard set of requirements for allocators, which are class-type objects that encapsulate the information about an allocation model.
This information includes the knowledge of pointer types, the type of their difference, the type of the size of objects in this allocation model, as well as the memory allocation and deallocation primitives for it.
Table 35: Descriptive variable definitions [tab:allocator.req.var]
| Variable | Definition |
|---|---|
| T, U, C | any cv-unqualified object type ([basic.types]) |
| X | an allocator class for type T |
| Y | the corresponding allocator class for type U |
| XX | the type allocator_traits<X> |
| YY | the type allocator_traits<Y> |
| a, a1, a2 | lvalues of type X |
| u | the name of a variable being declared |
| b | a value of type Y |
| c | a pointer of type C* through which indirection is valid |
| p | a value of type XX::pointer, obtained by calling a1.allocate, where a1 == a |
| q | a value of type XX::const_pointerobtained by conversion from a value p |
| r | a value of type T&obtained by the expression *p |
| w | a value of type XX::void_pointer obtained by conversion from a value p |
| x | a value of type XX::const_void_pointer obtained by conversion from a value q or a value w |
| y | a value of type XX::const_void_pointer obtained by conversion from a result value of YY::allocate, or else a value of type (possibly const) std::nullptr_t |
| n | a value of type XX::size_type |
| Args | a template parameter pack |
| args | a function parameter pack with the pattern Args&& |
The class template allocator_traits ([allocator.traits]) supplies a uniform interface to all allocator types.
Table 35 describes the types manipulated through allocators.
Table 36describes the requirements on allocator types and thus on types used to instantiate allocator_traits.
A requirement is optional if the last column of Table 36 specifies a default for a given expression.
Within the standard library allocator_traitstemplate, an optional requirement that is not supplied by an allocator is replaced by the specified default expression.
A user specialization ofallocator_traits may provide different defaults and may provide defaults for different requirements than the primary template.
Within Tables 35 and 36, the use of move and forward always refers to std::moveand std::forward, respectively.
Table 36: Cpp17Allocator requirements [tab:cpp17.allocator]
| Expression | Return type | Assertion/note | Default |
|---|---|---|---|
| pre-/post-condition | |||
| X::pointer | T* | ||
| X::const_pointer | X::pointer is convertible to X::const_pointer | pointer_traits<X::pointer>::rebind<const T> | |
| X::void_pointerY::void_pointer | X::pointer is convertible to X::void_pointer. X::void_pointer and Y::void_pointer are the same type. | pointer_traits<X::pointer>::rebind<void> | |
| X::const_void_pointerY::const_void_pointer | X::pointer, X::const_pointer, and X::void_pointer are convertible to X::const_void_pointer. X::const_void_pointer and Y::const_void_pointer are the same type. | pointer_traits<X::pointer>::rebind<const void> | |
| X::value_type | Identical to T | ||
| X::size_type | unsigned integer type | a type that can represent the size of the largest object in the allocation model | make_unsigned_t<X::difference_type> |
| X::difference_type | signed integer type | a type that can represent the difference between any two pointers in the allocation model | pointer_traits<X::pointer>::difference_type |
| typename X::template rebind<U>::other | Y | For all U (including T), Y::template rebind<T>::other is X. | See Note A, below. |
| *p | T& | ||
| *q | const T& | *q refers to the same object as *p. | |
| p->m | type of T::m | Preconditions: (*p).m is well-defined. equivalent to (*p).m | |
| q->m | type of T::m | Preconditions: (*q).m is well-defined. equivalent to (*q).m | |
| static_cast<X::pointer>(w) | X::pointer | static_cast<X::pointer>(w) == p | |
| static_cast<X::const_pointer>(x) | X::const_pointer | static_cast< X::const_pointer>(x) == q | |
| pointer_traits<X::pointer>::pointer_to(r) | X::pointer | same as p | |
| a.allocate(n) | X::pointer | Memory is allocated for an array of n Tand such an object is created but array elements are not constructed. [ Example:When reusing storage denoted by some pointer value p,launder(reinterpret_cast<T*>(new (p) byte[n * sizeof(T)]))can be used to implicitly create a suitable array object and obtain a pointer to it. — end example ] allocate may throw an appropriate exception.171 [ Note:If n == 0, the return value is unspecified. — end note ] | |
| a.allocate(n, y) | X::pointer | Same as a.allocate(n). The use of y is unspecified, but it is intended as an aid to locality. | a.allocate(n) |
| a.deallocate(p,n) | (not used) | Preconditions: p is a value returned by an earlier call to allocate that has not been invalidated by an intervening call to deallocate. n matches the value passed to allocate to obtain this memory. Throws: Nothing. | |
| a.max_size() | X::size_type | the largest value that can meaningfully be passed to X::allocate() | numeric_limits<size_type>::max() / sizeof(value_type) |
| a1 == a2 | bool | Returns true only if storage allocated from each can be deallocated via the other. operator== shall be reflexive, symmetric, and transitive, and shall not exit via an exception. | |
| a1 != a2 | bool | same as !(a1 == a2) | |
| a == b | bool | same as a == Y::rebind<T>::other(b) | |
| a != b | bool | same as !(a == b) | |
| X u(a); X u = a; | Shall not exit via an exception. Postconditions: u == a | ||
| X u(b); | Shall not exit via an exception. Postconditions: Y(u) == b, u == X(b) | ||
| X u(std::move(a)); X u = std::move(a); | Shall not exit via an exception. Postconditions: The value of a is unchanged and is equal to u. | ||
| X u(std::move(b)); | Shall not exit via an exception. Postconditions: u is equal to the prior value of X(b). | ||
| a.construct(c, args) | (not used) | Effects: Constructs an object of type C atc. | construct_at(c, std::forward<Args>(args)...) |
| a.destroy(c) | (not used) | Effects: Destroys the object at c | destroy_at(c) |
| a.select_on_container_copy_construction() | X | Typically returns either a or X(). | return a; |
| X::propagate_on_container_copy_assignment | Identical to or derived from true_type or false_type | true_type only if an allocator of type X should be copied when the client container is copy-assigned. See Note B, below. | false_type |
| X::propagate_on_container_move_assignment | Identical to or derived from true_type or false_type | true_type only if an allocator of type X should be moved when the client container is move-assigned. See Note B, below. | false_type |
| X::propagate_on_- container_swap | Identical to or derived from true_type or false_type | true_type only if an allocator of type X should be swapped when the client container is swapped. See Note B, below. | false_type |
| X::is_always_equal | Identical to or derived from true_type or false_type | true_type only if the expression a1 == a2 is guaranteed to be true for any two (possibly const) valuesa1, a2 of type X. | is_empty<X>::type |
Note A: The member class template rebind in the table above is effectively a typedef template.
[ Note
:
In general, if the name Allocator is bound to SomeAllocator<T>, thenAllocator::rebind<U>::other is the same type asSomeAllocator<U>, whereSomeAllocator<T>::value_type is T andSomeAllocator<U>::value_type is U.
— end note
]
IfAllocator is a class template instantiation of the formSomeAllocator<T, Args>, where Args is zero or more type arguments, and Allocator does not supply a rebind member template, the standard allocator_traits template usesSomeAllocator<U, Args> in place of Allocator::rebind<U>::otherby default.
For allocator types that are not template instantiations of the above form, no default is provided.
Note B: If X::propagate_on_container_copy_assignment::value is true,X shall meet theCpp17CopyAssignable requirements (Table 31) and the copy operation shall not throw exceptions.
If X::propagate_on_container_move_assignment::value is true,X shall meet theCpp17MoveAssignable requirements (Table 30) and the move operation shall not throw exceptions.
If X::propagate_on_container_swap::value is true, lvalues of type X shall be swappableand the swap operation shall not throw exceptions.
An allocator type X shall meet theCpp17CopyConstructible requirements (Table 29).
The X::pointer, X::const_pointer, X::void_pointer, andX::const_void_pointer types shall meet theCpp17NullablePointer requirements (Table 33).
No constructor, comparison function, copy operation, move operation, or swap operation on these pointer types shall exit via an exception.
X::pointer and X::const_pointer shall also meet the requirements for a Cpp17RandomAccessIterator ([random.access.iterators]) and the additional requirement that, when a and (a + n) are dereferenceable pointer values for some integral value n,
addressof(*(a + n)) == addressof(*a) + n
is true.
Let x1 and x2 denote objects of (possibly different) typesX::void_pointer, X::const_void_pointer, X::pointer, or X::const_pointer.
Then, x1 and x2 areequivalently-valued pointer values, if and only if both x1 and x2can be explicitly converted to the two corresponding objects px1 and px2of type X::const_pointer, using a sequence of static_casts using only these four types, and the expression px1 == px2evaluates to true.
Let w1 and w2 denote objects of type X::void_pointer.
Then for the expressions
w1 == w2 w1 != w2
either or both objects may be replaced by an equivalently-valued object of typeX::const_void_pointer with no change in semantics.
Let p1 and p2 denote objects of type X::pointer.
Then for the expressions
p1 == p2 p1 != p2 p1 < p2 p1 <= p2 p1 >= p2 p1 > p2 p1 - p2
either or both objects may be replaced by an equivalently-valued object of typeX::const_pointer with no change in semantics.
An allocator may constrain the types on which it can be instantiated and the arguments for which its construct or destroy members may be called.
If a type cannot be used with a particular allocator, the allocator class or the call to construct or destroy may fail to instantiate.
If the alignment associated with a specific over-aligned type is not supported by an allocator, instantiation of the allocator for that type may fail.
The allocator also may silently ignore the requested alignment.
[ Note
:
Additionally, the member function allocatefor that type may fail by throwing an object of typebad_alloc.
— end note
]
[ Example
:
The following is an allocator class template supporting the minimal interface that meets the requirements of Table 36:
template struct SimpleAllocator { typedef Tp value_type; SimpleAllocator(ctor args);
template SimpleAllocator(const SimpleAllocator& other);
[[nodiscard]] Tp* allocate(std::size_t n); void deallocate(Tp* p, std::size_t n); };
template<class T, class U> bool operator==(const SimpleAllocator&, const SimpleAllocator&); template<class T, class U> bool operator!=(const SimpleAllocator&, const SimpleAllocator&);
— end example
]
16.5.3.5.1 Allocator completeness requirements [allocator.requirements.completeness]
If X is an allocator class for type T,X additionally meets the allocator completeness requirements if, whether or not T is a complete type:
- X is a complete type, and
- all the member types of allocator_traits other than value_type are complete types.
16.5.4 Constraints on programs [constraints]
16.5.4.1 Overview [constraints.overview]
Subclause [constraints] describes restrictions on C++ programs that use the facilities of the C++ standard library.
The following subclauses specify constraints on the program's use of namespaces, its use of various reserved names, its use of headers, its use of standard library classes as base classes ([derived.classes]), its definitions of replacement functions, and its installation of handler functions during execution.
16.5.4.2 Namespace use [namespace.constraints]
16.5.4.2.1 Namespace std [namespace.std]
Unless otherwise specified, the behavior of a C++ program is undefined if it adds declarations or definitions to namespacestdor to a namespace within namespacestd.
Unless explicitly prohibited, a program may add a template specialization for any standard library class template to namespacestd provided that (a) the added declaration depends on at least one program-defined type and (b) the specialization meets the standard library requirements for the original template.172
The behavior of a C++ program is undefined if it declares an explicit or partial specialization of any standard library variable template, except where explicitly permitted by the specification of that variable template.
The behavior of a C++ program is undefined if it declares
- an explicit specialization of any member function of a standard library class template, or
- an explicit specialization of any member function template of a standard library class or class template, or
- an explicit or partial specialization of any member class template of a standard library class or class template, or
- a deduction guide for any standard library class template.
A program may explicitly instantiate a class template defined in the standard library only if the declaration (a) depends on the name of at least one program-defined type and (b) the instantiation meets the standard library requirements for the original template.
Let F denote a standard library function ([global.functions]), a standard library static member function, or an instantiation of a standard library function template.
Unless F is designated an addressable function, the behavior of a C++ program is unspecified (possibly ill-formed) if it explicitly or implicitly attempts to form a pointer to F.
Moreover, the behavior of a C++ program is unspecified (possibly ill-formed) if it attempts to form a reference to For if it attempts to form a pointer-to-member designating either a standard library non-static member function ([member.functions]) or an instantiation of a standard library member function template.
Other than in namespace stdor in a namespace within namespace std, a program may provide an overload for any library function template designated as a customization point, provided that (a) the overload's declaration depends on at least one user-defined type and (b) the overload meets the standard library requirements for the customization point.173
[ Note
:
This permits a (qualified or unqualified) call to the customization point to invoke the most appropriate overload for the given arguments.
— end note
]
A translation unit shall not declare namespace std to be an inline namespace ([namespace.def]).
16.5.4.2.2 Namespace posix [namespace.posix]
The behavior of a C++ program is undefined if it adds declarations or definitions to namespaceposixor to a namespace within namespaceposixunless otherwise specified.
The namespace posix is reserved for use by ISO/IEC 9945 and other POSIX standards.
16.5.4.2.3 Namespaces for future standardization [namespace.future]
Top-level namespaces whose namespace-name consists of stdfollowed by one or more digitsare reserved for future standardization.
The behavior of a C++ program is undefined if it adds declarations or definitions to such a namespace.
[ Example
:
The top-level namespace std2 is reserved for use by future revisions of this International Standard.
— end example
]
16.5.4.3 Reserved names [reserved.names]
The C++ standard library reserves the following kinds of names:
- macros
- global names
- names with external linkage
If a program declares or defines a name in a context where it is reserved, other than as explicitly allowed by this Clause, its behavior is undefined.
16.5.4.3.1 Zombie names [zombie.names]
In namespace std, the following names are reserved for previous standardization:
- auto_ptr,
- auto_ptr_ref,
- binary_function,
- binary_negate,
- bind1st,
- bind2nd,
- binder1st,
- binder2nd,
- const_mem_fun1_ref_t,
- const_mem_fun1_t,
- const_mem_fun_ref_t,
- const_mem_fun_t,
- get_temporary_buffer,
- get_unexpected,
- gets,
- is_literal_type,
- is_literal_type_v,
- mem_fun1_ref_t,
- mem_fun1_t,
- mem_fun_ref_t,
- mem_fun_ref,
- mem_fun_t,
- mem_fun,
- not1,
- not2,
- pointer_to_binary_function,
- pointer_to_unary_function,
- ptr_fun,
- random_shuffle,
- raw_storage_iterator,
- result_of,
- result_of_t,
- return_temporary_buffer,
- set_unexpected,
- unary_function,
- unary_negate,
- uncaught_exception,
- unexpected, and
- unexpected_handler.
The following names are reserved as member types for previous standardization, and may not be used as a name for object-like macros in portable code:
- argument_type,
- first_argument_type,
- io_state,
- open_mode,
- second_argument_type, and
- seek_dir.
The name stossc is reserved as a member function for previous standardization, and may not be used as a name for function-like macros in portable code.
The header names<ccomplex>,<ciso646>,<cstdalign>,<cstdbool>, and<ctgmath>are reserved for previous standardization.
16.5.4.3.2 Macro names [macro.names]
A translation unit that includes a standard library header shall not#define or #undef names declared in any standard library header.
A translation unit shall not #define or #undefnames lexically identical to keywords, to the identifiers listed in Table 4, or to the attribute-tokens described in [dcl.attr], except that the names likely and unlikely may be defined as function-like macros ([cpp.replace]).
16.5.4.3.3 External linkage [extern.names]
Each name declared as an object with external linkagein a header is reserved to the implementation to designate that library object with external linkage,174both in namespace std and in the global namespace.
Eachglobal function signature declared withexternal linkage in a header is reserved to the implementation to designate that function signature withexternal linkage.175
Each name from the C standard library declared with external linkageis reserved to the implementation for use as a name withextern "C"linkage, both in namespace std and in the global namespace.
Each function signature from the C standard library declared withexternal linkage is reserved to the implementation for use as a function signature with bothextern "C"andextern "C++"linkage,176or as a name of namespace scope in the global namespace.
16.5.4.3.4 Types [extern.types]
For each type T from the C standard library, the types::Tandstd::Tare reserved to the implementation and, when defined,::Tshall be identical tostd::T.
16.5.4.5 Derived classes [derived.classes]
Virtual member function signatures definedfor a base class in the C++ standardlibrary may be overridden in a derived class defined in the program ([class.virtual]).
16.5.4.6 Replacement functions [replacement.functions]
[support] through [thread] and [depr]describe the behavior of numerous functions defined by the C++ standard library.
Under some circumstances,however, certain of these function descriptions also apply to replacement functions defined in the program ([definitions]).
A C++ program may provide the definition for any of the following dynamic memory allocation function signatures declared in header<new> ([basic.stc.dynamic], [new.syn]):
operator new(std::size_t) operator new(std::size_t, std::align_val_t) operator new(std::size_t, const std::nothrow_t&) operator new(std::size_t, std::align_val_t, const std::nothrow_t&)
operator delete(void*) operator delete(void*, std::size_t) operator delete(void*, std::align_val_t) operator delete(void*, std::size_t, std::align_val_t) operator delete(void*, const std::nothrow_t&) operator delete(void*, std::align_val_t, const std::nothrow_t&)
operator new operator new[](std::size_t, std::align_val_t) operator new[](std::size_t, const std::nothrow_t&) operator new[](std::size_t, std::align_val_t, const std::nothrow_t&)
operator delete operator delete[](void*, std::size_t) operator delete[](void*, std::align_val_t) operator delete[](void*, std::size_t, std::align_val_t) operator delete[](void*, const std::nothrow_t&) operator delete[](void*, std::align_val_t, const std::nothrow_t&)
The program's definitions are used instead of the default versions supplied by the implementation ([new.delete]).
The program's declarations shall not be specified asinline.
No diagnostic is required.
16.5.4.7 Handler functions [handler.functions]
The C++ standard library provides a default version of the following handler function ([support]):
- terminate_handler
A C++ program may install different handler functions during execution, by supplying a pointer to a function defined in the program or the library as an argument to (respectively):
- set_new_handler
- set_terminate
See also subclauses [alloc.errors], Storage allocation errors, and [support.exception], Exception handling.
A C++ program can get a pointer to the current handler function by calling the following functions:
- get_new_handler
- get_terminate
Calling the set_* and get_* functions shall not incur a data race.
A call to any of the set_* functions shall synchronize with subsequent calls to the sameset_* function and to the corresponding get_* function.
16.5.4.8 Other functions [res.on.functions]
In certain cases (replacement functions, handler functions, operations on types used to instantiate standard library template components), the C++ standard library depends on components supplied by a C++ program.
If these components do not meet their requirements, this document places no requirements on the implementation.
In particular, the effects are undefined in the following cases:
- For replacement functions ([new.delete]), if the installed replacement function does not implement the semantics of the applicableRequired behavior:paragraph.
- For handler functions ([new.handler], [terminate.handler]), if the installed handler function does not implement the semantics of the applicableRequired behavior:paragraph.
- Operations on such types can report a failure by throwing an exception unless otherwise specified.
- If any replacement function or handler function or destructor operation exits via an exception, unless specifically allowed in the applicableRequired behavior:paragraph.
- If an incomplete type ([basic.types]) is used as a template argument when instantiating a template component or evaluating a concept, unless specifically allowed for that component.
16.5.4.9 Function arguments [res.on.arguments]
Each of the following applies to all argumentsto functions defined in the C++ standard library,unless explicitly stated otherwise.
- If an argument to a function has an invalid value (suchas a value outside the domain of the function or a pointer invalid for its intended use), the behavior is undefined.
- If a function argument is described as being an array,the pointer actually passed to the function shall have a value such that all address computations and accesses to objects (that would be valid if the pointer did point to the first element of such an array) are in fact valid.
- If a function argument binds to an rvalue reference parameter, the implementation may assume that this parameter is a unique reference to this argument.
[ Note
:
If the parameter is a generic parameter of the form T&& and an lvalue of typeA is bound, the argument binds to an lvalue reference ([temp.deduct.call]) and thus is not covered by the previous sentence.
— end note
]
[ Note
:
If a program casts an lvalue to an xvalue while passing that lvalue to a library function (e.g., by calling the function with the argument std::move(x)), the program is effectively asking that function to treat that lvalue as a temporary object.
The implementation is free to optimize away aliasing checks which might be needed if the argument was an lvalue.
— end note
]
16.5.4.10 Library object access [res.on.objects]
The behavior of a program is undefined if calls to standard library functions from different threads may introduce a data race.
The conditions under which this may occur are specified in [res.on.data.races].
[ Note
:
Modifying an object of a standard library type that is shared between threads risks undefined behavior unless objects of that type are explicitly specified as being shareable without data races or the user supplies a locking mechanism.
— end note
]
If an object of a standard library type is accessed, and the beginning of the object's lifetimedoes not happen before the access, or the access does not happen before the end of the object's lifetime, the behavior is undefined unless otherwise specified.
[ Note
:
This applies even to objects such as mutexes intended for thread synchronization.
— end note
]
16.5.4.11 Expects paragraph [res.on.expects]
Violation of any preconditions specified in a function's Preconditions: element results in undefined behavior.
16.5.4.12 Semantic requirements [res.on.requirements]
A sequence Args of template arguments is said tomodel a concept Cif Argssatisfies C ([temp.constr.decl]) and meets all semantic requirements (if any) given in the specification of C.
If the validity or meaning of a program depends on whether a sequence of template arguments models a concept, and the concept is satisfied but not modeled, the program is ill-formed, no diagnostic required.
If the semantic requirements of a declaration's constraints ([structure.requirements]) are not modeled at the point of use, the program is ill-formed, no diagnostic required.
16.5.5 Conforming implementations [conforming]
16.5.5.1 Overview [conforming.overview]
Subclause [conforming] describes the constraints upon, and latitude of, implementations of the C++ standard library.
An implementation's use of headers is discussed in [res.on.headers], its use of macros in [res.on.macro.definitions], non-member functions in [global.functions], member functions in [member.functions], data race avoidance in [res.on.data.races], access specifiers in [protection.within.classes], class derivation in [derivation], and exceptions in [res.on.exception.handling].
16.5.5.3 Restrictions on macro definitions [res.on.macro.definitions]
The names and global function signatures described in [contents] are reserved to the implementation.
All object-like macros defined by the C standard library and described in this Clause as expanding to integral constant expressions are also suitable for use in #if preprocessing directives, unless explicitly stated otherwise.
16.5.5.4 Non-member functions [global.functions]
It is unspecified whether any non-member functions in the C++ standard library are defined asinline.
A call to a non-member function signature described in [support] through [thread] and[depr] shall behave as if the implementation declared no additional non-member function signatures.177
An implementation shall not declare a non-member function signature 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 ([basic.lookup.argdep]).
[ Note
:
The phrase “unless otherwise specified” applies to cases such as the swappable with requirements ([swappable.requirements]).
The exception for overloaded operators allows argument-dependent lookup in cases like that ofostream_iterator::operator=:
Effects:
*out_stream << value; if (delim != 0) *out_stream << delim; return *this;
— end note
]
16.5.5.5 Member functions [member.functions]
It is unspecified whether any member functions in the C++ standard library are defined asinline.
For a non-virtual member function described in the C++ standard library, an implementation may declare a different set of member function signatures, provided that any call to the member function that would select an overload from the set of declarations described in this document behaves as if that overload were selected.
[ Note
:
For instance, an implementation may add parameters with default values, or replace a member function with default arguments with two or more member functions with equivalent behavior, or add additional signatures for a member function name.
— end note
]
16.5.5.7 Constexpr functions and constructors [constexpr.functions]
This document explicitly requires that certain standard library functions areconstexpr ([dcl.constexpr]).
An implementation shall not declare any standard library function signature as constexpr except for those where it is explicitly required.
Within any header that provides any non-defining declarations of constexpr functions or constructors an implementation shall provide corresponding definitions.
16.5.5.8 Requirements for stable algorithms [algorithm.stable]
When the requirements for an algorithm state that it is “stable” without further elaboration, it means:
- For the sort algorithms the relative order of equivalent elements is preserved.
- For the remove and copy algorithms the relative order of the elements that are not removed is preserved.
- For the merge algorithms, for equivalent elements in the original two ranges, the elements from the first range (preserving their original order) precede the elements from the second range (preserving their original order).
16.5.5.9 Reentrancy [reentrancy]
Except where explicitly specified in this document, it is implementation-defined which functions in the C++ standard library may be recursively reentered.
16.5.5.10 Data race avoidance [res.on.data.races]
This subclause specifies requirements that implementations shall meet to preventdata races.
Every standard library function shall meet each requirement unless otherwise specified.
Implementations may prevent data races in cases other than those specified below.
A C++ standard library function shall not directly or indirectly access objects ([intro.multithread]) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function's arguments, including this.
A C++ standard library function shall not directly or indirectly modify objects ([intro.multithread]) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function's non-const arguments, including this.
[ Note
:
This means, for example, that implementations can't use an object with static storage duration for internal purposes without synchronization because it could cause a data race even in programs that do not explicitly share objects between threads.
— end note
]
A C++ standard library function shall not access objects indirectly accessible via its arguments or via elements of its container arguments except by invoking functions required by its specification on those container elements.
Operations on iterators obtained by calling a standard library container or string member function may access the underlying container, but shall not modify it.
[ Note
:
In particular, container operations that invalidate iterators conflict with operations on iterators associated with that container.
— end note
]
Implementations may share their own internal objects between threads if the objects are not visible to users and are protected against data races.
Unless otherwise specified, C++ standard library functions shall perform all operations solely within the current thread if those operations have effects that arevisible to users.
[ Note
:
This allows implementations to parallelize operations if there are no visibleside effects.
— end note
]
16.5.5.12 Derived classes [derivation]
An implementation may derive any class in the C++ standard library from a class with a name reserved to the implementation.
Certain classes defined in the C++ standard library are required to be derived from other classes in the C++ standard library.
An implementation may derive such a class directly from the required base or indirectly through a hierarchy of base classes with names reserved to the implementation.
In any case:
- Every base class described asvirtualshall be virtual;
- Every base class not specified asvirtual shall not be virtual;
- Unless explicitly stated otherwise, types with distinct names shall be distinct types.178
All types specified in the C++ standard library shall be non-final types unless otherwise specified.
16.5.5.13 Restrictions on exception handling [res.on.exception.handling]
Any of the functions defined in the C++ standard librarycan report a failure by throwing an exception of a type described in its Throws: paragraph, or of a type derived from a type named in the Throws: paragraph that would be caught by an exception handler for the base type.
Functions from the C standard library shall not throw exceptions179except when such a function calls a program-supplied function that throws an exception.180
Destructor operations defined in the C++ standard library shall not throw exceptions.
Every destructor in the C++ standard library shall behave as if it had a non-throwing exception specification.
Functions defined in the C++ standard librarythat do not have a Throws: paragraph but do have a potentially-throwing exception specification may throw implementation-defined exceptions.181
An implementation may strengthen the exception specification for a non-virtual function by adding a non-throwing exception specification.
16.5.5.14 Restrictions on storage of pointers [res.on.pointer.storage]
Objects constructed by the standard library that may hold a user-supplied pointer value or an integer of type std::intptr_t shall store such values in a traceable pointer location ([basic.stc.dynamic.safety]).
[ Note
:
Other libraries are strongly encouraged to do the same, since not doing so may result in accidental use of pointers that are not safely derived.
Libraries that store pointers outside the user's address space should make it appear that they are stored and retrieved from a traceable pointer location.
— end note
]
16.5.5.15 Value of error codes [value.error.codes]
Certain functions in the C++ standard library report errors via astd::error_code object.
That object'scategory() member shall return std::system_category() for errors originating from the operating system, or a reference to animplementation-defined error_category object for errors originating elsewhere.
The implementation shall define the possible values of value() for each of these error categories.
[ Example
:
For operating systems that are based on POSIX, implementations should define the std::system_category() values as identical to the POSIX errno values, with additional values as defined by the operating system's documentation.
Implementations for operating systems that are not based on POSIX should define values identical to the operating system's values.
For errors that do not originate from the operating system, the implementation may provide enums for the associated values.
— end example
]
16.5.5.16 Moved-from state of library types [lib.types.movedfrom]
Objects of types defined in the C++ standard library may be moved from ([class.copy.ctor]).
Move operations may be explicitly specified or implicitly generated.
Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state.