Files
2025-10-25 03:02:53 +03:00

114 KiB
Raw Permalink Blame History

[memory]

20 Memory management library [mem]

20.2 Memory [memory]

20.2.1 General [memory.general]

1

#

Subclause [memory] describes the contents of the header and some of the contents of the header .

20.2.2 Header synopsis [memory.syn]

1

#

The header defines several types and function templates that describe properties of pointers and pointer-like types, manage memory for containers and other template types, destroy objects, and construct objects in uninitialized memory buffers ([pointer.traits]–[specialized.addressof] and [specialized.algorithms]).

The header also defines the templatesunique_ptr, shared_ptr, weak_ptr,out_ptr_t, inout_ptr_t, and various function templates that operate on objects of these types ([smartptr]).

2

#

Let POINTER_OF(T) denote a type that is

T::pointer if the qualified-id T::pointer is valid and denotes a type,

otherwise, T::element_type* if the qualified-id T::element_type is valid and denotes a type,

otherwise, pointer_traits::element_type*.

3

#

Let POINTER_OF_OR(T, U) denote a type that is:

POINTER_OF(T) if POINTER_OF(T) is valid and denotes a type,

otherwise, U.

#include // see [compare.syn]namespace std {// [pointer.traits], pointer traitstemplate struct pointer_traits; // freestandingtemplate struct pointer_traits<T*>; // freestanding// [pointer.conversion], pointer conversiontemplateconstexpr T* to_address(T* p) noexcept; // freestandingtemplateconstexpr auto to_address(const Ptr& p) noexcept; // freestanding// [ptr.align], pointer alignmentvoid* align(size_t alignment, size_t size, void*& ptr, size_t& space); // freestandingtemplate<size_t N, class T>constexpr T* assume_aligned(T* ptr); // freestandingtemplate<size_t Alignment, class T>bool is_sufficiently_aligned(T* ptr); // [obj.lifetime], explicit lifetime managementtemplate T* start_lifetime_as(void* p) noexcept; // freestandingtemplateconst T* start_lifetime_as(const void* p) noexcept; // freestandingtemplatevolatile T* start_lifetime_as(volatile void* p) noexcept; // freestandingtemplateconst volatile T* start_lifetime_as(const volatile void* p) noexcept; // freestandingtemplate T* start_lifetime_as_array(void* p, size_t n) noexcept; // freestandingtemplateconst T* start_lifetime_as_array(const void* p, size_t n) noexcept; // freestandingtemplatevolatile T* start_lifetime_as_array(volatile void* p, size_t n) noexcept; // freestandingtemplateconst volatile T* start_lifetime_as_array(const volatile void* p, // freestanding size_t n) noexcept; template T* trivially_relocate(T* first, T* last, T* result); // freestandingtemplateconstexpr T* relocate(T* first, T* last, T* result); // freestanding// [allocator.tag], allocator argument tagstruct allocator_arg_t { explicit allocator_arg_t() = default; }; // freestandinginline constexpr allocator_arg_t allocator_arg{}; // freestanding// [allocator.uses], uses_allocatortemplate<class T, class Alloc> struct uses_allocator; // freestanding// [allocator.uses.trait], uses_allocatortemplate<class T, class Alloc>constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value; // freestanding// [allocator.uses.construction], uses-allocator constructiontemplate<class T, class Alloc, class... Args>constexpr auto uses_allocator_construction_args(const Alloc& alloc, // freestanding Args&&... args) noexcept; template<class T, class Alloc, class Tuple1, class Tuple2>constexpr auto uses_allocator_construction_args(const Alloc& alloc, // freestanding piecewise_construct_t, Tuple1&& x, Tuple2&& y) noexcept; template<class T, class Alloc>constexpr auto uses_allocator_construction_args(const Alloc& alloc) noexcept; // freestandingtemplate<class T, class Alloc, class U, class V>constexpr auto uses_allocator_construction_args(const Alloc& alloc, // freestanding U&& u, V&& v) noexcept; template<class T, class Alloc, class U, class V>constexpr auto uses_allocator_construction_args(const Alloc& alloc, // freestanding pair<U, V>& pr) noexcept; template<class T, class Alloc, class U, class V>constexpr auto uses_allocator_construction_args(const Alloc& alloc, // freestandingconst pair<U, V>& pr) noexcept; template<class T, class Alloc, class U, class V>constexpr auto uses_allocator_construction_args(const Alloc& alloc, // freestanding pair<U, V>&& pr) noexcept; template<class T, class Alloc, class U, class V>constexpr auto uses_allocator_construction_args(const Alloc& alloc, // freestandingconst pair<U, V>&& pr) noexcept; template<class T, class Alloc, pair-like P>constexpr auto uses_allocator_construction_args(const Alloc& alloc, // freestanding P&& p) noexcept; template<class T, class Alloc, class U>constexpr auto uses_allocator_construction_args(const Alloc& alloc, // freestanding U&& u) noexcept; template<class T, class Alloc, class... Args>constexpr T make_obj_using_allocator(const Alloc& alloc, Args&&... args); // freestandingtemplate<class T, class Alloc, class... Args>constexpr T* uninitialized_construct_using_allocator(T* p, // freestandingconst Alloc& alloc, Args&&... args); // [allocator.traits], allocator traitstemplate struct allocator_traits; // freestandingtemplate<class Pointer, class SizeType = size_t>struct allocation_result { // freestanding Pointer ptr; SizeType count; }; // [default.allocator], the default allocatortemplate class allocator; template<class T, class U>constexpr bool operator==(const allocator&, const allocator&) noexcept; // [specialized.addressof], addressoftemplateconstexpr T* addressof(T& r) noexcept; // freestandingtemplateconst T* addressof(const T&&) = delete; // freestanding// [specialized.algorithms], specialized algorithms// [special.mem.concepts], special memory conceptstemplateconcept nothrow-input-iterator = see below; // exposition onlytemplateconcept nothrow-forward-iterator = see below; // exposition onlytemplateconcept nothrow-bidirectional-iterator = see below; // exposition onlytemplateconcept nothrow-random-access-iterator = see below; // exposition onlytemplate<class S, class I>concept nothrow-sentinel-for = see below; // exposition onlytemplate<class S, class I>concept nothrow-sized-sentinel-for = see below; // exposition onlytemplateconcept nothrow-input-range = see below; // exposition onlytemplateconcept nothrow-forward-range = see below; // exposition onlytemplateconcept nothrow-bidirectional-range = see below; // exposition onlytemplateconcept nothrow-random-access-range = see below; // exposition onlytemplateconcept nothrow-sized-random-access-range = see below; // exposition onlytemplateconstexpr void uninitialized_default_construct(NoThrowForwardIterator first, // freestanding NoThrowForwardIterator last); template<class ExecutionPolicy, class NoThrowForwardIterator>void uninitialized_default_construct(ExecutionPolicy&& exec, // freestanding-deleted, NoThrowForwardIterator first, // see [algorithms.parallel.overloads] NoThrowForwardIterator last); template<class NoThrowForwardIterator, class Size>constexpr NoThrowForwardIterator uninitialized_default_construct_n(NoThrowForwardIterator first, Size n); // freestandingtemplate<class ExecutionPolicy, class NoThrowForwardIterator, class Size> NoThrowForwardIterator uninitialized_default_construct_n(ExecutionPolicy&& exec, // freestanding-deleted, NoThrowForwardIterator first, // see [algorithms.parallel.overloads] Size n); namespace ranges {template<nothrow-forward-iterator I, nothrow-sentinel-for S>requires default_initializable<iter_value_t>constexpr I uninitialized_default_construct(I first, S last); // freestandingtemplate<nothrow-forward-range R>requires default_initializable<range_value_t>constexpr borrowed_iterator_t uninitialized_default_construct(R&& r); // freestandingtemplate<nothrow-forward-iterator I>requires default_initializable<iter_value_t>constexpr I uninitialized_default_construct_n(I first, // freestanding iter_difference_t n); template<execution-policy Ep, nothrow-random-access-iterator I, nothrow-sized-sentinel-for S>requires default_initializable<iter_value_t> I uninitialized_default_construct(Ep&& exec, I first, S last); // freestanding-deleted,// see [algorithms.parallel.overloads]template<execution-policy Ep, nothrow-sized-random-access-range R>requires default_initializable<range_value_t> borrowed_iterator_t uninitialized_default_construct(Ep&& exec, // freestanding-deleted, R&& r); // see [algorithms.parallel.overloads]template<execution-policy Ep, nothrow-random-access-iterator I>requires default_initializable<iter_value_t> I uninitialized_default_construct_n(Ep&& exec, I first, // freestanding-deleted, iter_difference_t n); // see [algorithms.parallel.overloads]}templateconstexpr void uninitialized_value_construct(NoThrowForwardIterator first, // freestanding NoThrowForwardIterator last); template<class ExecutionPolicy, class NoThrowForwardIterator>void uninitialized_value_construct(ExecutionPolicy&& exec, // freestanding-deleted, NoThrowForwardIterator first, // see [algorithms.parallel.overloads] NoThrowForwardIterator last); template<class NoThrowForwardIterator, class Size>constexpr NoThrowForwardIterator uninitialized_value_construct_n(NoThrowForwardIterator first, Size n); // freestandingtemplate<class ExecutionPolicy, class NoThrowForwardIterator, class Size> NoThrowForwardIterator uninitialized_value_construct_n(ExecutionPolicy&& exec, // freestanding-deleted, NoThrowForwardIterator first, // see [algorithms.parallel.overloads] Size n); namespace ranges {template<nothrow-forward-iterator I, nothrow-sentinel-for S>requires default_initializable<iter_value_t>constexpr I uninitialized_value_construct(I first, S last); // freestandingtemplate<nothrow-forward-range R>requires default_initializable<range_value_t>constexpr borrowed_iterator_t uninitialized_value_construct(R&& r); // freestandingtemplate<nothrow-forward-iterator I>requires default_initializable<iter_value_t>constexpr I uninitialized_value_construct_n(I first, // freestanding iter_difference_t n); template<execution-policy Ep, nothrow-random-access-iterator I, nothrow-sized-sentinel-for S>requires default_initializable<iter_value_t> I uninitialized_value_construct(Ep&& exec, I first, S last); // freestanding-deleted,// see [algorithms.parallel.overloads]template<execution-policy Ep, nothrow-sized-random-access-range R>requires default_initializable<range_value_t> borrowed_iterator_t uninitialized_value_construct(Ep&& exec, // freestanding-deleted, R&& r); // see [algorithms.parallel.overloads]template<execution-policy Ep, nothrow-random-access-iterator I>requires default_initializable<iter_value_t> I uninitialized_value_construct_n(Ep&& exec, I first, // freestanding-deleted, iter_difference_t n); // see [algorithms.parallel.overloads]}template<class InputIterator, class NoThrowForwardIterator>constexpr NoThrowForwardIterator uninitialized_copy(InputIterator first, // freestanding InputIterator last, NoThrowForwardIterator result); template<class ExecutionPolicy, class ForwardIterator, class NoThrowForwardIterator> NoThrowForwardIterator uninitialized_copy(ExecutionPolicy&& exec, // freestanding-deleted, ForwardIterator first, // see [algorithms.parallel.overloads] ForwardIterator last, NoThrowForwardIterator result); template<class InputIterator, class Size, class NoThrowForwardIterator>constexpr NoThrowForwardIterator uninitialized_copy_n(InputIterator first, // freestanding Size n, NoThrowForwardIterator result); template<class ExecutionPolicy, class ForwardIterator, class Size, class NoThrowForwardIterator> NoThrowForwardIterator uninitialized_copy_n(ExecutionPolicy&& exec, // freestanding-deleted, ForwardIterator first, // see [algorithms.parallel.overloads] Size n, NoThrowForwardIterator result); namespace ranges {template<class I, class O>using uninitialized_copy_result = in_out_result<I, O>; // freestandingtemplate<input_iterator I, sentinel_for S1, nothrow-forward-iterator O, nothrow-sentinel-for S2>requires constructible_from<iter_value_t, iter_reference_t>constexpr uninitialized_copy_result<I, O> uninitialized_copy(I ifirst, S1 ilast, O ofirst, S2 olast); // freestandingtemplate<input_range IR, nothrow-forward-range OR>requires constructible_from<range_value_t, range_reference_t>constexpr uninitialized_copy_result<borrowed_iterator_t, borrowed_iterator_t> uninitialized_copy(IR&& in_range, OR&& out_range); // freestandingtemplate<class I, class O>using uninitialized_copy_n_result = in_out_result<I, O>; // freestandingtemplate<input_iterator I, nothrow-forward-iterator O, nothrow-sentinel-for S>requires constructible_from<iter_value_t, iter_reference_t>constexpr uninitialized_copy_n_result<I, O> uninitialized_copy_n(I ifirst, iter_difference_t n, // freestanding O ofirst, S olast); template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S1, nothrow-random-access-iterator O, nothrow-sized-sentinel-for S2>requires constructible_from<iter_value_t, iter_reference_t> uninitialized_copy_result<I, O> uninitialized_copy(Ep&& exec, I ifirst, S1 ilast, // freestanding-deleted, O ofirst, S2 olast); // see [algorithms.parallel.overloads]template<execution-policy Ep, sized-random-access-range IR, nothrow-sized-random-access-range OR>requires constructible_from<range_value_t, range_reference_t> uninitialized_copy_result<borrowed_iterator_t, borrowed_iterator_t> uninitialized_copy(Ep&& exec, IR&& in_range, OR&& out_range); // freestanding-deleted,// see [algorithms.parallel.overloads]template<execution-policy Ep, random_access_iterator I, nothrow-random-access-iterator O, nothrow-sized-sentinel-for S>requires constructible_from<iter_value_t, iter_reference_t> uninitialized_copy_n_result<I, O> uninitialized_copy_n(Ep&& exec, I ifirst, iter_difference_t n, // freestanding-deleted, O ofirst, S olast); // see [algorithms.parallel.overloads]}template<class InputIterator, class NoThrowForwardIterator>constexpr NoThrowForwardIterator uninitialized_move(InputIterator first, // freestanding InputIterator last, NoThrowForwardIterator result); template<class ExecutionPolicy, class ForwardIterator, class NoThrowForwardIterator> NoThrowForwardIterator uninitialized_move(ExecutionPolicy&& exec, // freestanding-deleted, ForwardIterator first, // see [algorithms.parallel.overloads] ForwardIterator last, NoThrowForwardIterator result); template<class InputIterator, class Size, class NoThrowForwardIterator>constexpr pair<InputIterator, NoThrowForwardIterator> uninitialized_move_n(InputIterator first, Size n, // freestanding NoThrowForwardIterator result); template<class ExecutionPolicy, class ForwardIterator, class Size, class NoThrowForwardIterator> pair<ForwardIterator, NoThrowForwardIterator> uninitialized_move_n(ExecutionPolicy&& exec, // freestanding-deleted, ForwardIterator first, Size n, // see [algorithms.parallel.overloads] NoThrowForwardIterator result); namespace ranges {template<class I, class O>using uninitialized_move_result = in_out_result<I, O>; // freestandingtemplate<input_iterator I, sentinel_for S1, nothrow-forward-iterator O, nothrow-sentinel-for S2>requires constructible_from<iter_value_t, iter_rvalue_reference_t>constexpr uninitialized_move_result<I, O> uninitialized_move(I ifirst, S1 ilast, O ofirst, S2 olast); // freestandingtemplate<input_range IR, nothrow-forward-range OR>requires constructible_from<range_value_t, range_rvalue_reference_t>constexpr uninitialized_move_result<borrowed_iterator_t, borrowed_iterator_t> uninitialized_move(IR&& in_range, OR&& out_range); // freestandingtemplate<class I, class O>using uninitialized_move_n_result = in_out_result<I, O>; // freestandingtemplate<input_iterator I, nothrow-forward-iterator O, nothrow-sentinel-for S>requires constructible_from<iter_value_t, iter_rvalue_reference_t>constexpr uninitialized_move_n_result<I, O> uninitialized_move_n(I ifirst, iter_difference_t n, // freestanding O ofirst, S olast); template<execution-policy Ep, random_access_iterator I, sized_sentinel_for S1, nothrow-random-access-iterator O, nothrow-sized-sentinel-for S2>requires constructible_from<iter_value_t, iter_rvalue_reference_t> uninitialized_move_result<I, O> uninitialized_move(Ep&& exec, I ifirst, S1 ilast, // freestanding-deleted, O ofirst, S2 olast); // see [algorithms.parallel.overloads]template<execution-policy Ep, sized-random-access-range IR, nothrow-sized-random-access-range OR>requires constructible_from<range_value_t, range_rvalue_reference_t> uninitialized_move_result<borrowed_iterator_t, borrowed_iterator_t> uninitialized_move(Ep&& exec, IR&& in_range, OR&& out_range); // freestanding-deleted,// see [algorithms.parallel.overloads]template<execution-policy Ep, random_access_iterator I, nothrow-random-access-iterator O, nothrow-sized-sentinel-for S>requires constructible_from<iter_value_t, iter_rvalue_reference_t> uninitialized_move_n_result<I, O> uninitialized_move_n(Ep&& exec, I ifirst, iter_difference_t n, // freestanding-deleted, O ofirst, S olast); // see [algorithms.parallel.overloads]}template<class NoThrowForwardIterator, class T>constexpr void uninitialized_fill(NoThrowForwardIterator first, // freestanding NoThrowForwardIterator last, const T& x); template<class ExecutionPolicy, class NoThrowForwardIterator, class T>void uninitialized_fill(ExecutionPolicy&& exec, // freestanding-deleted, NoThrowForwardIterator first, // see [algorithms.parallel.overloads] NoThrowForwardIterator last, const T& x); template<class NoThrowForwardIterator, class Size, class T>constexpr NoThrowForwardIterator uninitialized_fill_n(NoThrowForwardIterator first, Size n, const T& x); // freestandingtemplate<class ExecutionPolicy, class NoThrowForwardIterator, class Size, class T> NoThrowForwardIterator uninitialized_fill_n(ExecutionPolicy&& exec, // freestanding-deleted, NoThrowForwardIterator first, // see [algorithms.parallel.overloads] Size n, const T& x); namespace ranges {template<nothrow-forward-iterator I, nothrow-sentinel-for S, class T>requires constructible_from<iter_value_t, const T&>constexpr I uninitialized_fill(I first, S last, const T& x); // freestandingtemplate<nothrow-forward-range R, class T>requires constructible_from<range_value_t, const T&>constexpr borrowed_iterator_t uninitialized_fill(R&& r, const T& x); // freestandingtemplate<nothrow-forward-iterator I, class T>requires constructible_from<iter_value_t, const T&>constexpr I uninitialized_fill_n(I first, // freestanding iter_difference_t n, const T& x); template<execution-policy Ep, nothrow-random-access-iterator I, nothrow-sized-sentinel-for S, class T>requires constructible_from<iter_value_t, const T&> I uninitialized_fill(Ep&& exec, I first, S last, const T& x); // freestanding-deleted,// see [algorithms.parallel.overloads]template<execution-policy Ep, nothrow-sized-random-access-range R, class T>requires constructible_from<range_value_t, const T&> borrowed_iterator_t uninitialized_fill(Ep&& exec, R&& r, // freestanding-deleted,const T& x); // see [algorithms.parallel.overloads]template<execution-policy Ep, nothrow-random-access-iterator I, class T>requires constructible_from<iter_value_t, const T&> I uninitialized_fill_n(Ep&& exec, I first, // freestanding-deleted, iter_difference_t n, const T& x); // see [algorithms.parallel.overloads]}// [specialized.construct], construct_attemplate<class T, class... Args>constexpr T* construct_at(T* location, Args&&... args); // freestandingnamespace ranges {template<class T, class... Args>constexpr T* construct_at(T* location, Args&&... args); // freestanding}// [specialized.destroy], destroytemplateconstexpr void destroy_at(T* location); // freestandingtemplateconstexpr void destroy(NoThrowForwardIterator first, // freestanding NoThrowForwardIterator last); template<class ExecutionPolicy, class NoThrowForwardIterator>void destroy(ExecutionPolicy&& exec, // freestanding-deleted, NoThrowForwardIterator first, // see [algorithms.parallel.overloads] NoThrowForwardIterator last); template<class NoThrowForwardIterator, class Size>constexpr NoThrowForwardIterator destroy_n(NoThrowForwardIterator first, // freestanding Size n); template<class ExecutionPolicy, class NoThrowForwardIterator, class Size> NoThrowForwardIterator destroy_n(ExecutionPolicy&& exec, // freestanding-deleted, NoThrowForwardIterator first, Size n); // see [algorithms.parallel.overloads]namespace ranges {template<destructible T>constexpr void destroy_at(T* location) noexcept; // freestandingtemplate<nothrow-input-iterator I, nothrow-sentinel-for S>requires destructible<iter_value_t>constexpr I destroy(I first, S last) noexcept; // freestandingtemplate<nothrow-input-range R>requires destructible<range_value_t>constexpr borrowed_iterator_t destroy(R&& r) noexcept; // freestandingtemplate<nothrow-input-iterator I>requires destructible<iter_value_t>constexpr I destroy_n(I first, iter_difference_t n) noexcept; // freestandingtemplate<execution-policy Ep, nothrow-random-access-iterator I, nothrow-sized-sentinel-for S>requires destructible<iter_value_t> I destroy(Ep&& exec, I first, S last) noexcept; // freestanding-deleted,// see [algorithms.parallel.overloads]template<execution-policy Ep, nothrow-sized-random-access-range R>requires destructible<range_value_t> borrowed_iterator_t destroy(Ep&& exec, R&& r) noexcept; // freestanding-deleted,// see [algorithms.parallel.overloads]template<execution-policy Ep, nothrow-random-access-iterator I>requires destructible<iter_value_t> I destroy_n(Ep&& exec, I first, iter_difference_t n) noexcept; // freestanding-deleted,// see [algorithms.parallel.overloads]}// [unique.ptr], class template unique_ptrtemplate struct default_delete; // freestandingtemplate struct default_delete<T[]>; // freestandingtemplate<class T, class D = default_delete> class unique_ptr; // freestandingtemplate<class T, class D> class unique_ptr<T[], D>; // freestandingtemplate<class T, class... Args>constexpr unique_ptr make_unique(Args&&... args); // T is not arraytemplateconstexpr unique_ptr make_unique(size_t n); // T is U[]template<class T, class... Args>unspecified make_unique(Args&&...) = delete; // T is U[N]templateconstexpr unique_ptr make_unique_for_overwrite(); // T is not arraytemplateconstexpr unique_ptr make_unique_for_overwrite(size_t n); // T is U[]template<class T, class... Args>unspecified make_unique_for_overwrite(Args&&...) = delete; // T is U[N]template<class T, class D>constexpr void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept; // freestandingtemplate<class T1, class D1, class T2, class D2>constexpr bool operator==(const unique_ptr<T1, D1>& x, // freestandingconst unique_ptr<T2, D2>& y); template<class T1, class D1, class T2, class D2>constexpr bool operator<(const unique_ptr<T1, D1>& x, // freestandingconst unique_ptr<T2, D2>& y); template<class T1, class D1, class T2, class D2>constexpr bool operator>(const unique_ptr<T1, D1>& x, // freestandingconst unique_ptr<T2, D2>& y); template<class T1, class D1, class T2, class D2>constexpr bool operator<=(const unique_ptr<T1, D1>& x, // freestandingconst unique_ptr<T2, D2>& y); template<class T1, class D1, class T2, class D2>constexpr bool operator>=(const unique_ptr<T1, D1>& x, // freestandingconst unique_ptr<T2, D2>& y); template<class T1, class D1, class T2, class D2>requires three_way_comparable_with<typename unique_ptr<T1, D1>::pointer, typename unique_ptr<T2, D2>::pointer>constexpr compare_three_way_result_t<typename unique_ptr<T1, D1>::pointer, typename unique_ptr<T2, D2>::pointer>operator<=>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); // freestandingtemplate<class T, class D>constexpr bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept; // freestandingtemplate<class T, class D>constexpr bool operator<(const unique_ptr<T, D>& x, nullptr_t); // freestandingtemplate<class T, class D>constexpr bool operator<(nullptr_t, const unique_ptr<T, D>& y); // freestandingtemplate<class T, class D>constexpr bool operator>(const unique_ptr<T, D>& x, nullptr_t); // freestandingtemplate<class T, class D>constexpr bool operator>(nullptr_t, const unique_ptr<T, D>& y); // freestandingtemplate<class T, class D>constexpr bool operator<=(const unique_ptr<T, D>& x, nullptr_t); // freestandingtemplate<class T, class D>constexpr bool operator<=(nullptr_t, const unique_ptr<T, D>& y); // freestandingtemplate<class T, class D>constexpr bool operator>=(const unique_ptr<T, D>& x, nullptr_t); // freestandingtemplate<class T, class D>constexpr bool operator>=(nullptr_t, const unique_ptr<T, D>& y); // freestandingtemplate<class T, class D>requires three_way_comparable<typename unique_ptr<T, D>::pointer>constexpr compare_three_way_result_t<typename unique_ptr<T, D>::pointer>operator<=>(const unique_ptr<T, D>& x, nullptr_t); // freestandingtemplate<class E, class T, class Y, class D> basic_ostream<E, T>& operator<<(basic_ostream<E, T>& os, const unique_ptr<Y, D>& p); // [util.smartptr.weak.bad], class bad_weak_ptrclass bad_weak_ptr; // [util.smartptr.shared], class template shared_ptrtemplate class shared_ptr; // [util.smartptr.shared.create], shared_ptr creationtemplate<class T, class... Args>constexpr shared_ptr make_shared(Args&&... args); // T is not arraytemplate<class T, class A, class... Args>constexpr shared_ptr allocate_shared(const A& a, Args&&... args); // T is not arraytemplateconstexpr shared_ptr make_shared(size_t N); // T is U[]template<class T, class A>constexpr shared_ptr allocate_shared(const A& a, size_t N); // T is U[]templateconstexpr shared_ptr make_shared(); // T is U[N]template<class T, class A>constexpr shared_ptr allocate_shared(const A& a); // T is U[N]templateconstexpr shared_ptr make_shared(size_t N, const remove_extent_t& u); // T is U[]template<class T, class A>constexpr shared_ptr allocate_shared(const A& a, size_t N, const remove_extent_t& u); // T is U[]templateconstexpr shared_ptr make_shared(const remove_extent_t& u); // T is U[N]template<class T, class A>constexpr shared_ptr allocate_shared(const A& a, // T is U[N]const remove_extent_t& u); templateconstexpr shared_ptr make_shared_for_overwrite(); // T is not U[]template<class T, class A>constexpr shared_ptr allocate_shared_for_overwrite(const A& a); // T is not U[]templateconstexpr shared_ptr make_shared_for_overwrite(size_t N); // T is U[]template<class T, class A>constexpr shared_ptr allocate_shared_for_overwrite(const A& a, size_t N); // T is U[]// [util.smartptr.shared.cmp], shared_ptr comparisonstemplate<class T, class U>constexpr bool operator==(const shared_ptr& a, const shared_ptr& b) noexcept; template<class T, class U>constexpr strong_ordering operator<=>(const shared_ptr& a, const shared_ptr& b) noexcept; templateconstexpr bool operator==(const shared_ptr& x, nullptr_t) noexcept; templateconstexpr strong_ordering operator<=>(const shared_ptr& x, nullptr_t) noexcept; // [util.smartptr.shared.spec], shared_ptr specialized algorithmstemplateconstexpr void swap(shared_ptr& a, shared_ptr& b) noexcept; // [util.smartptr.shared.cast], shared_ptr caststemplate<class T, class U>constexpr shared_ptr static_pointer_cast(const shared_ptr& r) noexcept; template<class T, class U>constexpr shared_ptr static_pointer_cast(shared_ptr&& r) noexcept; template<class T, class U>constexpr shared_ptr dynamic_pointer_cast(const shared_ptr& r) noexcept; template<class T, class U>constexpr shared_ptr dynamic_pointer_cast(shared_ptr&& r) noexcept; template<class T, class U>constexpr shared_ptr const_pointer_cast(const shared_ptr& r) noexcept; template<class T, class U>constexpr shared_ptr const_pointer_cast(shared_ptr&& r) noexcept; template<class T, class U> shared_ptr reinterpret_pointer_cast(const shared_ptr& r) noexcept; template<class T, class U> shared_ptr reinterpret_pointer_cast(shared_ptr&& r) noexcept; // [util.smartptr.getdeleter], shared_ptr get_deletertemplate<class D, class T>constexpr D* get_deleter(const shared_ptr& p) noexcept; // [util.smartptr.shared.io], shared_ptr I/Otemplate<class E, class T, class Y> basic_ostream<E, T>& operator<<(basic_ostream<E, T>& os, const shared_ptr& p); // [util.smartptr.weak], class template weak_ptrtemplate class weak_ptr; // [util.smartptr.weak.spec], weak_ptr specialized algorithmstemplate constexpr void swap(weak_ptr& a, weak_ptr& b) noexcept; // [util.smartptr.ownerless], class template owner_lesstemplate struct owner_less; // [util.smartptr.owner.hash], struct owner_hashstruct owner_hash; // [util.smartptr.owner.equal], struct owner_equalstruct owner_equal; // [util.smartptr.enab], class template enable_shared_from_thistemplate class enable_shared_from_this; // [util.smartptr.hash], hash supporttemplate struct hash; // freestandingtemplate<class T, class D> struct hash<unique_ptr<T, D>>; // freestandingtemplate struct hash<shared_ptr>; // [util.smartptr.atomic], atomic smart pointerstemplate struct atomic; // freestandingtemplate struct atomic<shared_ptr>; template struct atomic<weak_ptr>; // [out.ptr.t], class template out_ptr_ttemplate<class Smart, class Pointer, class... Args>class out_ptr_t; // freestanding// [out.ptr], function template out_ptrtemplate<class Pointer = void, class Smart, class... Args>constexpr auto out_ptr(Smart& s, Args&&... args); // freestanding// [inout.ptr.t], class template inout_ptr_ttemplate<class Smart, class Pointer, class... Args>class inout_ptr_t; // freestanding// [inout.ptr], function template inout_ptrtemplate<class Pointer = void, class Smart, class... Args>constexpr auto inout_ptr(Smart& s, Args&&... args); // freestanding// [indirect], class template indirecttemplate<class T, class Allocator = allocator>class indirect; // [indirect.hash], hash supporttemplate<class T, class Alloc> struct hash<indirect<T, Alloc>>; // [polymorphic], class template polymorphictemplate<class T, class Allocator = allocator>class polymorphic; namespace pmr {template using indirect = indirect<T, polymorphic_allocator>; template using polymorphic = polymorphic<T, polymorphic_allocator>; }}

20.2.3 Pointer traits [pointer.traits]

20.2.3.1 General [pointer.traits.general]

1

#

The class template pointer_traits supplies a uniform interface to certain attributes of pointer-like types.

🔗

namespace std {template struct pointer_traits {see below; }; template struct pointer_traits<T*> {using pointer = T*; using element_type = T; using difference_type = ptrdiff_t; template using rebind = U*; static constexpr pointer pointer_to(see below r) noexcept; };}

20.2.3.2 Member types [pointer.traits.types]

1

#

The definitions in this subclause make use of the following exposition-only class template and concept:templatestruct ptr-traits-elem // exposition only{ };

template requires requires { typename T::element_type; }struct ptr-traits-elem{ using type = typename T::element_type; };

template<template<class...> class SomePointer, class T, class... Args>requires (!requires { typename SomePointer<T, Args...>::element_type; })struct ptr-traits-elem<SomePointer<T, Args...>>{ using type = T; };

templateconcept has-elem-type = // exposition onlyrequires { typename ptr-traits-elem::type; }

2

#

If Ptr satisfies has-elem-type, a specialization pointer_traits generated from the pointer_traits primary template has the following members as well as those described in [pointer.traits.functions]; otherwise, such a specialization has no members by any of those names.

🔗

using pointer = see below;

3

#

Type: Ptr.

🔗

using element_type = see below;

4

#

Type: typename ptr-traits-elem::type.

🔗

using difference_type = see below;

5

#

Type: Ptr::difference_type if the qualified-id Ptr::difference_type is valid and denotes a type ([temp.deduct]); otherwise,ptrdiff_t.

🔗

template<class U> using rebind = see below;

6

#

Alias template: Ptr::rebind if the qualified-id Ptr::rebind is valid and denotes a type ([temp.deduct]); otherwise,SomePointer<U, Args> ifPtr is a class template instantiation of the form SomePointer<T, Args>, where Args is zero or more type arguments; otherwise, the instantiation ofrebind is ill-formed.

20.2.3.3 Member functions [pointer.traits.functions]

🔗

static pointer pointer_traits::pointer_to(see below r); static constexpr pointer pointer_traits<T*>::pointer_to(see below r) noexcept;

1

#

Mandates: For the first member function,Ptr::pointer_to(r) is well-formed.

2

#

Preconditions: For the first member function,Ptr::pointer_to(r) returns a pointer to r through which indirection is valid.

3

#

Returns: The first member function returns Ptr::pointer_to(r).

The second member function returns addressof(r).

4

#

Remarks: If element_type is cv void, the type ofr is unspecified; otherwise, it is element_type&.

20.2.3.4 Optional members [pointer.traits.optmem]

1

#

Specializations of pointer_traits may define the member declared in this subclause to customize the behavior of the standard library.

A specialization generated from the pointer_traits primary template has no member by this name.

🔗

static element_type* to_address(pointer p) noexcept;

2

#

Returns: A pointer of type element_type* that references the same location as the argument p.

3

#

[Note 1:

This function is intended to be the inverse of pointer_to.

If defined, it customizes the behavior of the non-member functionto_address ([pointer.conversion]).

— end note]

20.2.4 Pointer conversion [pointer.conversion]

🔗

template<class T> constexpr T* to_address(T* p) noexcept;

1

#

Mandates: T is not a function type.

2

#

Returns: p.

🔗

template<class Ptr> constexpr auto to_address(const Ptr& p) noexcept;

3

#

Returns: pointer_traits::to_address(p) if that expression is well-formed (see [pointer.traits.optmem]), otherwise to_address(p.operator->()).

20.2.5 Pointer alignment [ptr.align]

🔗

void* align(size_t alignment, size_t size, void*& ptr, size_t& space);

1

#

Preconditions:

alignment is a power of two

ptr represents the address of contiguous storage of at leastspace bytes

2

#

Effects: If it is possible to fit size bytes of storage aligned by alignment into the buffer pointed to byptr with length space, the function updatesptr to represent the first possible address of such storage and decreases space by the number of bytes used for alignment.

Otherwise, the function does nothing.

3

#

Returns: A null pointer if the requested aligned buffer would not fit into the available space, otherwise the adjusted value of ptr.

4

#

[Note 1:

The function updates its ptr and space arguments so that it can be called repeatedly with possibly different alignment and size arguments for the same buffer.

— end note]

🔗

template<size_t N, class T> constexpr T* assume_aligned(T* ptr);

5

#

Mandates: N is a power of two.

6

#

Preconditions: ptr points to an object X of a type similar ([conv.qual]) to T, where X has alignment N ([basic.align]).

7

#

Returns: ptr.

8

#

Throws: Nothing.

9

#

[Note 2:

The alignment assumption on an object X expressed by a call to assume_aligned might result in generation of more efficient code.

It is up to the program to ensure that the assumption actually holds.

The call does not cause the implementation to verify or enforce this.

An implementation might only make the assumption for those operations on X that access X through the pointer returned by assume_aligned.

— end note]

🔗

template<size_t Alignment, class T> bool is_sufficiently_aligned(T* ptr);

10

#

Preconditions: p points to an object X of a type similar ([conv.qual]) to T.

11

#

Returns: true if X has alignment at least Alignment, otherwise false.

12

#

Throws: Nothing.

20.2.6 Explicit lifetime management [obj.lifetime]

🔗

template<class T> T* start_lifetime_as(void* p) noexcept; template<class T> const T* start_lifetime_as(const void* p) noexcept; template<class T> volatile T* start_lifetime_as(volatile void* p) noexcept; template<class T> const volatile T* start_lifetime_as(const volatile void* p) noexcept;

1

#

Mandates: T is an implicit-lifetime type ([basic.types.general]) and not an incomplete type ([basic.types.general]).

2

#

Preconditions: [p, (char*)p + sizeof(T)) denotes a region of allocated storage that is a subset of the region of storage reachable through ([basic.compound]) p and suitably aligned for the type T.

3

#

Effects: Implicitly creates objects ([intro.object]) within the denoted region consisting of an object a of type T whose address is p, and objects nested within a, as follows: The object representation of a is the contents of the storage prior to the call to start_lifetime_as.

The value of each created object o of trivially copyable type ([basic.types.general]) U is determined in the same manner as for a call to bit_cast(E) ([bit.cast]), where E is an lvalue of type U denoting o, except that the storage is not accessed.

The value of any other created object is unspecified.

[Note 1:

The unspecified value can be indeterminate.

— end note]

4

#

Returns: A pointer to the a defined in the Effects paragraph.

🔗

template<class T> T* start_lifetime_as_array(void* p, size_t n) noexcept; template<class T> const T* start_lifetime_as_array(const void* p, size_t n) noexcept; template<class T> volatile T* start_lifetime_as_array(volatile void* p, size_t n) noexcept; template<class T> const volatile T* start_lifetime_as_array(const volatile void* p, size_t n) noexcept;

5

#

Mandates: T is a complete type.

6

#

Preconditions: p is suitably aligned for an array of T or is null.

n <= size_t(-1) / sizeof(T) is true.

If n > 0 is true, [(char*)p, (char*)p + (n * sizeof(T))) denotes a region of allocated storage that is a subset of the region of storage reachable through ([basic.compound]) p.

7

#

Effects: If n > 0 is true, equivalent tostart_lifetime_as(p) where U is the type “array of n T”.

Otherwise, there are no effects.

8

#

Returns: A pointer to the first element of the created array, if any; otherwise, a pointer that compares equal to p ([expr.eq]).

🔗

template<class T> T* trivially_relocate(T* first, T* last, T* result);

9

#

Mandates: is_trivially_relocatable_v && !is_const_v is true.

T is not an array of unknown bound.

10

#

Preconditions:

  • (10.1)

    [first, last) is a valid range.

  • (10.2)

    [result, result + (last - first)) denotes a region of storage that is a subset of the region reachable through result ([basic.compound]) and suitably aligned for the type T.

  • (10.3)

    No element in the range [first, last) is a potentially-overlapping subobject.

11

#

Postconditions: No effect if result == first is true.

Otherwise, the range denoted by [result, result + (last - first)) contains objects (including subobjects) whose lifetime has begun and whose object representations are the original object representations of the corresponding objects in the source range [first, last) except for any parts of the object representations used by the implementation to represent type information ([intro.object]).

If any of the objects has union type, its active member is the same as that of the corresponding object in the source range.

If any of the aforementioned objects has a non-static data member of reference type, that reference refers to the same entity as does the corresponding reference in the source range.

The lifetimes of the original objects in the source range have ended.

12

#

Returns: result + (last - first).

13

#

Throws: Nothing.

14

#

Complexity: Linear in the length of the source range.

15

#

Remarks: The destination region of storage is considered reused ([basic.life]).

No constructors or destructors are invoked.

[Note 2:

Overlapping ranges are supported.

— end note]

🔗

template<class T> constexpr T* relocate(T* first, T* last, T* result);

16

#

Mandates: is_nothrow_relocatable_v && !is_const_v is true.

T is not an array of unknown bound.

17

#

Preconditions:

  • (17.1)

    [first, last) is a valid range.

  • (17.2)

    [result, result + (last - first)) denotes a region of storage that is a subset of the region reachable through result ([basic.compound]) and suitably aligned for the type T.

  • (17.3)

    No element in the range [first, last) is a potentially-overlapping subobject.

18

#

Effects:

If result == first is true, no effect;

otherwise, if not called during constant evaluation and is_trivially_relocatable_v is true, then has effects equivalent to: trivially_relocate(first, last, result);

otherwise, for each integer i in [0, last - first),

if T is an array type, equivalent to: relocate(begin(first[i]), end(first[i]), *start_lifetime_as(result + i));

otherwise, equivalent to: construct_at(result + i, std::move(first[i])); destroy_at(first + i);

19

#

Returns: result + (last - first).

20

#

Throws: Nothing.

[Note 3:

Overlapping ranges are supported.

— end note]

20.2.7 Allocator argument tag [allocator.tag]

🔗

namespace std { struct allocator_arg_t { explicit allocator_arg_t() = default; }; inline constexpr allocator_arg_t allocator_arg{}; }

1

#

The allocator_arg_t struct is an empty class type used as a unique type to disambiguate constructor and function overloading.

Specifically, several types (seetuple [tuple]) have constructors with allocator_arg_t as the first argument, immediately followed by an argument of a type that meets theCpp17Allocator requirements ([allocator.requirements.general]).

20.2.8 uses_allocator [allocator.uses]

20.2.8.1 uses_allocator trait [allocator.uses.trait]

🔗

template<class T, class Alloc> struct uses_allocator;

1

#

Remarks: Automatically detects whether T has a nested allocator_type that is convertible from Alloc.

Meets the Cpp17BinaryTypeTrait requirements ([meta.rqmts]).

The implementation shall provide a definition that is derived from true_type if the qualified-id T::allocator_type is valid and denotes a type ([temp.deduct]) andis_convertible_v<Alloc, T::allocator_type> != false, otherwise it shall be derived from false_type.

A program may specialize this template to derive fromtrue_type for a program-defined type T that does not have a nestedallocator_type but nonetheless can be constructed with an allocator where either:

the first argument of a constructor has type allocator_arg_t and the second argument has type Alloc or

the last argument of a constructor has type Alloc.

20.2.8.2 Uses-allocator construction [allocator.uses.construction]

1

#

Uses-allocator construction with allocator alloc and constructor arguments args... refers to the construction of an object of type T such that alloc is passed to the constructor of T if T uses an allocator type compatible with alloc.

When applied to the construction of an object of type T, it is equivalent to initializing it with the value of the expressionmake_obj_using_allocator(alloc, args...), described below.

2

#

The following utility functions support three conventions for passing alloc to a constructor:

  • (2.1)

    If T does not use an allocator compatible with alloc, then alloc is ignored.

  • (2.2)

    Otherwise, if T has a constructor invocable as T(allocator_arg, alloc, args...) (leading-allocator convention), then uses-allocator construction chooses this constructor form.

  • (2.3)

    Otherwise, if T has a constructor invocable as T(args..., alloc) (trailing-allocator convention), then uses-allocator construction chooses this constructor form.

3

#

The uses_allocator_construction_args function template takes an allocator and argument list and produces (as a tuple) a new argument list matching one of the above conventions.

Additionally, overloads are provided that treat specializations of pair such that uses-allocator construction is applied individually to the first and second data members.

The make_obj_using_allocator anduninitialized_construct_using_allocator function templates apply the modified constructor arguments to construct an object of type T as a return value or in-place, respectively.

[Note 1:

For uses_allocator_construction_args andmake_obj_using_allocator, type T is not deduced and must therefore be specified explicitly by the caller.

— end note]

🔗

template<class T, class Alloc, class... Args> constexpr auto uses_allocator_construction_args(const Alloc& alloc, Args&&... args) noexcept;

4

#

Constraints: remove_cv_t is not a specialization of pair.

5

#

Returns: A tuple value determined as follows:

  • (5.1)

    If uses_allocator_v<remove_cv_t, Alloc> is false and is_constructible_v<T, Args...> is true, return forward_as_tuple(std::forward(args)...).

  • (5.2)

    Otherwise, if uses_allocator_v<remove_cv_t, Alloc> is true and is_constructible_v<T, allocator_arg_t, const Alloc&, Args...> is true, returntuple<allocator_arg_t, const Alloc&, Args&&...>( allocator_arg, alloc, std::forward(args)...)

  • (5.3)

    Otherwise, if uses_allocator_v<remove_cv_t, Alloc> is true and is_constructible_v<T, Args..., const Alloc&> is true, return forward_as_tuple(std::forward(args)..., alloc).

  • (5.4)

    Otherwise, the program is ill-formed.

[Note 2:

This definition prevents a silent failure to pass the allocator to a constructor of a type for whichuses_allocator_v<T, Alloc> is true.

— end note]

🔗

template<class T, class Alloc, class Tuple1, class Tuple2> constexpr auto uses_allocator_construction_args(const Alloc& alloc, piecewise_construct_t, Tuple1&& x, Tuple2&& y) noexcept;

6

#

Let T1 be T::first_type.

Let T2 be T::second_type.

7

#

Constraints: remove_cv_t is a specialization of pair.

8

#

Effects: Equivalent to:return make_tuple( piecewise_construct, apply([&alloc](auto&&... args1) {return uses_allocator_construction_args( alloc, std::forward<decltype(args1)>(args1)...); }, std::forward(x)), apply([&alloc](auto&&... args2) {return uses_allocator_construction_args( alloc, std::forward<decltype(args2)>(args2)...); }, std::forward(y)));

🔗

template<class T, class Alloc> constexpr auto uses_allocator_construction_args(const Alloc& alloc) noexcept;

9

#

Constraints: remove_cv_t is a specialization of pair.

10

#

Effects: Equivalent to:return uses_allocator_construction_args(alloc, piecewise_construct, tuple<>{}, tuple<>{});

🔗

template<class T, class Alloc, class U, class V> constexpr auto uses_allocator_construction_args(const Alloc& alloc, U&& u, V&& v) noexcept;

11

#

Constraints: remove_cv_t is a specialization of pair.

12

#

Effects: Equivalent to:return uses_allocator_construction_args(alloc, piecewise_construct, forward_as_tuple(std::forward(u)), forward_as_tuple(std::forward(v)));

🔗

template<class T, class Alloc, class U, class V> constexpr auto uses_allocator_construction_args(const Alloc& alloc, pair<U, V>& pr) noexcept; template<class T, class Alloc, class U, class V> constexpr auto uses_allocator_construction_args(const Alloc& alloc, const pair<U, V>& pr) noexcept;

13

#

Constraints: remove_cv_t is a specialization of pair.

14

#

Effects: Equivalent to:return uses_allocator_construction_args(alloc, piecewise_construct, forward_as_tuple(pr.first), forward_as_tuple(pr.second));

🔗

template<class T, class Alloc, class U, class V> constexpr auto uses_allocator_construction_args(const Alloc& alloc, pair<U, V>&& pr) noexcept; template<class T, class Alloc, class U, class V> constexpr auto uses_allocator_construction_args(const Alloc& alloc, const pair<U, V>&& pr) noexcept;

15

#

Constraints: remove_cv_t is a specialization of pair.

16

#

Effects: Equivalent to:return uses_allocator_construction_args(alloc, piecewise_construct, forward_as_tuple(get<0>(std::move(pr))), forward_as_tuple(get<1>(std::move(pr))));

🔗

template<class T, class Alloc, [pair-like](tuple.syn#concept:pair-like "22.4.2Header <tuple> synopsis[tuple.syn]") P> constexpr auto uses_allocator_construction_args(const Alloc& alloc, P&& p) noexcept;

17

#

Constraints: remove_cv_t is a specialization of pair andremove_cvref_t

is not a specialization of ranges::subrange.

18

#

Effects: Equivalent to:return uses_allocator_construction_args(alloc, piecewise_construct, forward_as_tuple(get<0>(std::forward

(p))), forward_as_tuple(get<1>(std::forward

(p))));

🔗

template<class T, class Alloc, class U> constexpr auto uses_allocator_construction_args(const Alloc& alloc, U&& u) noexcept;

19

#

Let FUN be the function template:template<class A, class B>void FUN(const pair<A, B>&);

20

#

Constraints: remove_cv_t is a specialization of pair, and either:

remove_cvref_t is a specialization of ranges::subrange, or

U does not satisfy pair-like and the expression FUN(u) is not well-formed when considered as an unevaluated operand.

21

#

Let pair-constructor be an exposition-only class defined as follows:class pair-constructor {using pair-type = remove_cv_t; // exposition onlyconstexpr auto do-construct(const pair-type& p) const { // exposition onlyreturn make_obj_using_allocator<pair-type>(alloc_, p); }constexpr auto do-construct(pair-type&& p) const { // exposition onlyreturn make_obj_using_allocator<pair-type>(alloc_, std::move(p)); }const Alloc& alloc_; // exposition only U& u_; // exposition onlypublic:constexpr operator pair-type() const {return do-construct(std::forward(u_)); }};

22

#

Returns: make_tuple(pc), where pc is a pair-constructor object whose alloc_ member is initialized with alloc and whose u_ member is initialized with u.

🔗

template<class T, class Alloc, class... Args> constexpr T make_obj_using_allocator(const Alloc& alloc, Args&&... args);

23

#

Effects: Equivalent to:return make_from_tuple(uses_allocator_construction_args( alloc, std::forward(args)...));

🔗

template<class T, class Alloc, class... Args> constexpr T* uninitialized_construct_using_allocator(T* p, const Alloc& alloc, Args&&... args);

24

#

Effects: Equivalent to:return apply([&]<class... U>(U&&... xs) {return construct_at(p, std::forward(xs)...); }, uses_allocator_construction_args(alloc, std::forward(args)...));

20.2.9 Allocator traits [allocator.traits]

20.2.9.1 General [allocator.traits.general]

1

#

The class template allocator_traits supplies a uniform interface to all allocator types.

An allocator cannot be a non-class type, however, even if allocator_traits supplies the entire required interface.

[Note 1:

Thus, it is always possible to create a derived class from an allocator.

— end note]

If a program declares an explicit or partial specialization of allocator_traits, the program is ill-formed, no diagnostic required.

🔗

namespace std {template struct allocator_traits {using allocator_type = Alloc; using value_type = typename Alloc::value_type; using pointer = see below; using const_pointer = see below; using void_pointer = see below; using const_void_pointer = see below; using difference_type = see below; using size_type = see below; using propagate_on_container_copy_assignment = see below; using propagate_on_container_move_assignment = see below; using propagate_on_container_swap = see below; using is_always_equal = see below; template using rebind_alloc = see below; template using rebind_traits = allocator_traits<rebind_alloc>; static constexpr pointer allocate(Alloc& a, size_type n); static constexpr pointer allocate(Alloc& a, size_type n, const_void_pointer hint); static constexpr allocation_result<pointer, size_type> allocate_at_least(Alloc& a, size_type n); static constexpr void deallocate(Alloc& a, pointer p, size_type n); template<class T, class... Args>static constexpr void construct(Alloc& a, T* p, Args&&... args); templatestatic constexpr void destroy(Alloc& a, T* p); static constexpr size_type max_size(const Alloc& a) noexcept; static constexpr Alloc select_on_container_copy_construction(const Alloc& rhs); };}

20.2.9.2 Member types [allocator.traits.types]

🔗

using pointer = see below;

1

#

Type: Alloc::pointer if the qualified-id Alloc::pointer is valid and denotes a type ([temp.deduct]); otherwise, value_type*.

🔗

using const_pointer = see below;

2

#

Type: Alloc::const_pointer if the qualified-id Alloc::const_pointer is valid and denotes a type ([temp.deduct]); otherwise,pointer_traits::rebind<const value_type>.

🔗

using void_pointer = see below;

3

#

Type: Alloc::void_pointer if the qualified-id Alloc::void_pointer is valid and denotes a type ([temp.deduct]); otherwise,pointer_traits::rebind<void>.

🔗

using const_void_pointer = see below;

4

#

Type: Alloc::const_void_pointer if the qualified-id Alloc::const_void_pointer is valid and denotes a type ([temp.deduct]); otherwise,pointer_traits::rebind.

🔗

using difference_type = see below;

5

#

Type: Alloc::difference_type if the qualified-id Alloc::difference_type is valid and denotes a type ([temp.deduct]); otherwise,pointer_traits::difference_type.

🔗

using size_type = see below;

6

#

Type: Alloc::size_type if the qualified-id Alloc::size_type is valid and denotes a type ([temp.deduct]); otherwise,make_unsigned_t<difference_type>.

🔗

using propagate_on_container_copy_assignment = see below;

7

#

Type: Alloc::propagate_on_container_copy_assignment if the qualified-id Alloc::propagate_on_container_copy_assignment is valid and denotes a type ([temp.deduct]); otherwisefalse_type.

🔗

using propagate_on_container_move_assignment = see below;

8

#

Type: Alloc::propagate_on_container_move_assignment if the qualified-id Alloc::propagate_on_container_move_assignment is valid and denotes a type ([temp.deduct]); otherwisefalse_type.

🔗

using propagate_on_container_swap = see below;

9

#

Type: Alloc::propagate_on_container_swap if the qualified-id Alloc::propagate_on_container_swap is valid and denotes a type ([temp.deduct]); otherwisefalse_type.

🔗

using is_always_equal = see below;

10

#

Type: Alloc::is_always_equal if the qualified-id Alloc::is_always_equal is valid and denotes a type ([temp.deduct]); otherwise is_empty::type.

🔗

template<class T> using rebind_alloc = see below;

11

#

Alias template: Alloc::rebind::other if the qualified-id Alloc::rebind::other is valid and denotes a type ([temp.deduct]); otherwise,Alloc<T, Args> if Alloc is a class template instantiation of the form Alloc<U, Args>, where Args is zero or more type arguments; otherwise, the instantiation of rebind_alloc is ill-formed.

20.2.9.3 Static member functions [allocator.traits.members]

🔗

static constexpr pointer allocate(Alloc& a, size_type n);

1

#

Returns: a.allocate(n).

🔗

static constexpr pointer allocate(Alloc& a, size_type n, const_void_pointer hint);

2

#

Returns: a.allocate(n, hint) if that expression is well-formed; otherwise, a.allocate(n).

🔗

static constexpr allocation_result<pointer, size_type> allocate_at_least(Alloc& a, size_type n);

3

#

Returns: a.allocate_at_least(n) if that expression is well-formed; otherwise, {a.allocate(n), n}.

🔗

static constexpr void deallocate(Alloc& a, pointer p, size_type n);

4

#

Effects: Calls a.deallocate(p, n).

5

#

Throws: Nothing.

🔗

template<class T, class... Args> static constexpr void construct(Alloc& a, T* p, Args&&... args);

6

#

Effects: Calls a.construct(p, std::forward(args)...) if that call is well-formed; otherwise, invokes construct_at(p, std::forward(args)...).

🔗

template<class T> static constexpr void destroy(Alloc& a, T* p);

7

#

Effects: Calls a.destroy(p) if that call is well-formed; otherwise, invokesdestroy_at(p).

🔗

static constexpr size_type max_size(const Alloc& a) noexcept;

8

#

Returns: a.max_size() if that expression is well-formed; otherwise,numeric_limits<size_type>::max() / sizeof(value_type).

🔗

static constexpr Alloc select_on_container_copy_construction(const Alloc& rhs);

9

#

Returns: rhs.select_on_container_copy_construction() if that expression is well-formed; otherwise, rhs.

20.2.9.4 Other [allocator.traits.other]

1

#

The class template allocation_result has the template parameters, data members, and special members specified above.

It has no base classes or members other than those specified.

20.2.10 The default allocator [default.allocator]

20.2.10.1 General [default.allocator.general]

1

#

All specializations of the default allocator meet the allocator completeness requirements ([allocator.requirements.completeness]).

🔗

namespace std {template class allocator {public:using value_type = T; using size_type = size_t; using difference_type = ptrdiff_t; using propagate_on_container_move_assignment = true_type; constexpr allocator() noexcept; constexpr allocator(const allocator&) noexcept; template constexpr allocator(const allocator&) noexcept; constexpr ~allocator(); constexpr allocator& operator=(const allocator&) = default; constexpr T* allocate(size_t n); constexpr allocation_result<T*> allocate_at_least(size_t n); constexpr void deallocate(T* p, size_t n); };}

2

#

allocator_traits<allocator>::is_always_equal::value is true for any T.

20.2.10.2 Members [allocator.members]

1

#

Except for the destructor, member functions of the default allocator shall not introduce data races ([intro.multithread]) as a result of concurrent calls to those member functions from different threads.

Calls to these functions that allocate or deallocate a particular unit of storage shall occur in a single total order, and each such deallocation call shall happen before the next allocation (if any) in this order.

🔗

constexpr T* allocate(size_t n);

2

#

Mandates: T is not an incomplete type ([basic.types.general]).

3

#

Returns: A pointer to the initial element of an array of n T.

4

#

Throws: bad_array_new_length ifnumeric_limits<size_t>::max() / sizeof(T) < n, orbad_alloc if the storage cannot be obtained.

5

#

Remarks: The storage for the array is obtained by calling ::operator new ([new.delete]), but it is unspecified when or how often this function is called.

This function starts the lifetime of the array object, but not that of any of the array elements.

🔗

constexpr allocation_result<T*> allocate_at_least(size_t n);

6

#

Mandates: T is not an incomplete type ([basic.types.general]).

7

#

Returns: allocation_result<T*>{ptr, count}, where ptr is a pointer to the initial element of an array of count T andcount ≥ n.

8

#

Throws: bad_array_new_length if numeric_limits<size_t>::max() / sizeof(T) < n, or bad_alloc if the storage cannot be obtained.

9

#

Remarks: The storage for the array is obtained by calling ::operator new, but it is unspecified when or how often this function is called.

This function starts the lifetime of the array object, but not that of any of the array elements.

🔗

constexpr void deallocate(T* p, size_t n);

10

#

Preconditions:

  • (10.1)

    If p is memory that was obtained by a call to allocate_at_least, let ret be the value returned andreq be the value passed as the first argument to that call. p is equal to ret.ptr andn is a value such that req ≤ n ≤ ret.count.

  • (10.2)

    Otherwise, p is a pointer value obtained from allocate. n equals the value passed as the first argument to the invocation of allocate which returned p.

11

#

Effects: Deallocates the storage referenced by p.

12

#

Remarks: Uses::operator delete ([new.delete]), but it is unspecified when this function is called.

20.2.10.3 Operators [allocator.globals]

🔗

template<class T, class U> constexpr bool operator==(const allocator<T>&, const allocator<U>&) noexcept;

1

#

Returns: true.

20.2.11 addressof [specialized.addressof]

🔗

template<class T> constexpr T* addressof(T& r) noexcept;

1

#

Returns: The actual address of the object or function referenced by r, even in the presence of an overloaded operator&.

2

#

Remarks: An expression addressof(E) is a constant subexpression ([defns.const.subexpr]) if E is an lvalue constant subexpression.

20.2.12 C library memory allocation [c.malloc]

1

#

[Note 1:

The header declares the functions described in this subclause.

— end note]

🔗

void* aligned_alloc(size_t alignment, size_t size); void* calloc(size_t nmemb, size_t size); void* malloc(size_t size);

2

#

Effects: These functions have the semantics specified in the C standard library.

3

#

Remarks: These functions do not attempt to allocate storage by calling ::operator new() ([new.delete]).

4

#

These functions implicitly create objects ([intro.object]) in the returned region of storage and return a pointer to a suitable created object.

In the case of calloc, the objects are created before the storage is zeroed.

🔗

void* realloc(void* ptr, size_t size);

5

#

Preconditions: free(ptr) has well-defined behavior.

6

#

Effects: If ptr is not null and size is zero, the behavior is erroneous and the effects are implementation-defined.

Otherwise, this function has the semantics specified in the C standard library.

7

#

Remarks: This function does not attempt to allocate storage by calling ::operator new() ([new.delete]).

When a non-null pointer is returned, this function implicitly creates objects ([intro.object]) in the returned region of storage and returns a pointer to a suitable created object.

The objects are created before the storage is copied.

🔗

void free(void* ptr);

8

#

Effects: This function has the semantics specified in the C standard library.

9

#

Remarks: This function does not attempt to deallocate storage by calling::operator delete().

See also: ISO/IEC 9899:2024, 7.22.3