16 Library introduction [library]

16.5 Library-wide requirements [requirements]

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.2 Headers [res.on.headers]

A C++ header may include other C++ headers.
A C++ header shall provide the declarations and definitions that appear in its synopsis.
A C++ header shown in its synopsis as including other C++ headers shall provide the declarations and definitions that appear in the synopses of those other headers.
Certain types and macros are defined in more than one header.
Every such entity shall be defined such that any header that defines it may be included after any other header that also defines it ([basic.def.odr]).
The C standard library headers shall include only their corresponding C++ standard library header, as described in [headers].

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 as inline.
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 of ostream_­iterator​::​operator=:
Effects:
*out_stream << value;
if (delim != 0)
  *out_stream << delim;
return *this;
— end note
 ]
A valid C++ program always calls the expected library non-member function.
An implementation may also define additional non-member functions that would otherwise not be called by a valid C++ program.

16.5.5.5 Member functions [member.functions]

It is unspecified whether any member functions in the C++ standard library are defined as inline.
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.6 Friend functions [hidden.friends]

Whenever this document specifies a friend declaration of a function or function template within a class or class template definition, that declaration shall be the only declaration of that function or function template provided by an implementation.
Note
:
In particular, an implementation is not allowed to provide an additional declaration of that function or function template at namespace scope.
— end note
 ]
Note
:
Such a friend function or function template declaration is known as a hidden friend, as it is visible neither to ordinary unqualified lookup ([basic.lookup.unqual]) nor to qualified lookup ([basic.lookup.qual]).
— end note
 ]

16.5.5.7 Constexpr functions and constructors [constexpr.functions]

This document explicitly requires that certain standard library functions are constexpr ([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 prevent data 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 are visible to users.
Note
:
This allows implementations to parallelize operations if there are no visible side effects.
— end note
 ]

16.5.5.11 Protection within classes [protection.within.classes]

It is unspecified whether any function signature or class described in [support] through [thread] and [depr] is a friend of another class in the C++ standard library.

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 as virtual shall be virtual;
  • Every base class not specified as virtual 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.
There is an implicit exception to this rule for types that are described as synonyms for basic integral types, such as size_­t and streamoff.

16.5.5.13 Restrictions on exception handling [res.on.exception.handling]

Any of the functions defined in the C++ standard library can 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 exceptions179 except 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 library that do not have a Throws: paragraph but do have a potentially-throwing exception specification may throw implementation-defined exceptions.181
Implementations should report errors by throwing exceptions of or derived from the standard exception classes ([bad.alloc], [support.exception], [std.exceptions]).
An implementation may strengthen the exception specification for a non-virtual function by adding a non-throwing exception specification.
That is, the C library functions can all be treated as if they are marked noexcept.
This allows implementations to make performance optimizations based on the absence of exceptions at runtime.
The functions qsort() and bsearch() ([alg.c.library]) meet this condition.
In particular, they can report a failure to allocate storage by throwing an exception of type bad_­alloc, or a class derived from bad_­alloc.

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 a std​::​error_­code object.
That object's category() member shall return std​::​system_­category() for errors originating from the operating system, or a reference to an implementation-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.