xref: /freebsd/contrib/llvm-project/libcxx/include/memory (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_MEMORY
110b57cec5SDimitry Andric#define _LIBCPP_MEMORY
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    memory synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std
170b57cec5SDimitry Andric{
180b57cec5SDimitry Andric
190b57cec5SDimitry Andricstruct allocator_arg_t { };
200b57cec5SDimitry Andricinline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
210b57cec5SDimitry Andric
220b57cec5SDimitry Andrictemplate <class T, class Alloc> struct uses_allocator;
230b57cec5SDimitry Andric
240b57cec5SDimitry Andrictemplate <class Ptr>
250b57cec5SDimitry Andricstruct pointer_traits
260b57cec5SDimitry Andric{
270b57cec5SDimitry Andric    typedef Ptr pointer;
280b57cec5SDimitry Andric    typedef <details> element_type;
290b57cec5SDimitry Andric    typedef <details> difference_type;
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric    template <class U> using rebind = <details>;
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric    static pointer pointer_to(<details>);
340b57cec5SDimitry Andric};
350b57cec5SDimitry Andric
360b57cec5SDimitry Andrictemplate <class T>
370b57cec5SDimitry Andricstruct pointer_traits<T*>
380b57cec5SDimitry Andric{
390b57cec5SDimitry Andric    typedef T* pointer;
400b57cec5SDimitry Andric    typedef T element_type;
410b57cec5SDimitry Andric    typedef ptrdiff_t difference_type;
420b57cec5SDimitry Andric
430b57cec5SDimitry Andric    template <class U> using rebind = U*;
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric    static pointer pointer_to(<details>) noexcept; // constexpr in C++20
460b57cec5SDimitry Andric};
470b57cec5SDimitry Andric
480b57cec5SDimitry Andrictemplate <class T> constexpr T* to_address(T* p) noexcept; // C++20
49e8d8bef9SDimitry Andrictemplate <class Ptr> constexpr auto to_address(const Ptr& p) noexcept; // C++20
500b57cec5SDimitry Andric
510b57cec5SDimitry Andrictemplate <class Alloc>
520b57cec5SDimitry Andricstruct allocator_traits
530b57cec5SDimitry Andric{
540b57cec5SDimitry Andric    typedef Alloc                        allocator_type;
550b57cec5SDimitry Andric    typedef typename allocator_type::value_type
560b57cec5SDimitry Andric                                         value_type;
570b57cec5SDimitry Andric
580b57cec5SDimitry Andric    typedef Alloc::pointer | value_type* pointer;
590b57cec5SDimitry Andric    typedef Alloc::const_pointer
600b57cec5SDimitry Andric          | pointer_traits<pointer>::rebind<const value_type>
610b57cec5SDimitry Andric                                         const_pointer;
620b57cec5SDimitry Andric    typedef Alloc::void_pointer
630b57cec5SDimitry Andric          | pointer_traits<pointer>::rebind<void>
640b57cec5SDimitry Andric                                         void_pointer;
650b57cec5SDimitry Andric    typedef Alloc::const_void_pointer
660b57cec5SDimitry Andric          | pointer_traits<pointer>::rebind<const void>
670b57cec5SDimitry Andric                                         const_void_pointer;
680b57cec5SDimitry Andric    typedef Alloc::difference_type
690b57cec5SDimitry Andric          | pointer_traits<pointer>::difference_type
700b57cec5SDimitry Andric                                         difference_type;
710b57cec5SDimitry Andric    typedef Alloc::size_type
720b57cec5SDimitry Andric          | make_unsigned<difference_type>::type
730b57cec5SDimitry Andric                                         size_type;
740b57cec5SDimitry Andric    typedef Alloc::propagate_on_container_copy_assignment
750b57cec5SDimitry Andric          | false_type                   propagate_on_container_copy_assignment;
760b57cec5SDimitry Andric    typedef Alloc::propagate_on_container_move_assignment
770b57cec5SDimitry Andric          | false_type                   propagate_on_container_move_assignment;
780b57cec5SDimitry Andric    typedef Alloc::propagate_on_container_swap
790b57cec5SDimitry Andric          | false_type                   propagate_on_container_swap;
800b57cec5SDimitry Andric    typedef Alloc::is_always_equal
810b57cec5SDimitry Andric          | is_empty                     is_always_equal;
820b57cec5SDimitry Andric
835ffd83dbSDimitry Andric    template <class T> using rebind_alloc  = Alloc::rebind<T>::other | Alloc<T, Args...>;
840b57cec5SDimitry Andric    template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
850b57cec5SDimitry Andric
86e8d8bef9SDimitry Andric    static pointer allocate(allocator_type& a, size_type n);                          // constexpr and [[nodiscard]] in C++20
87e8d8bef9SDimitry Andric    static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // constexpr and [[nodiscard]] in C++20
880b57cec5SDimitry Andric
89e8d8bef9SDimitry Andric    static void deallocate(allocator_type& a, pointer p, size_type n) noexcept; // constexpr in C++20
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric    template <class T, class... Args>
92e8d8bef9SDimitry Andric    static void construct(allocator_type& a, T* p, Args&&... args); // constexpr in C++20
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric    template <class T>
95e8d8bef9SDimitry Andric    static void destroy(allocator_type& a, T* p); // constexpr in C++20
960b57cec5SDimitry Andric
97e8d8bef9SDimitry Andric    static size_type max_size(const allocator_type& a); // noexcept in C++14, constexpr in C++20
98e8d8bef9SDimitry Andric    static allocator_type select_on_container_copy_construction(const allocator_type& a); // constexpr in C++20
990b57cec5SDimitry Andric};
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andrictemplate <>
10223408297SDimitry Andricclass allocator<void> // removed in C++20
1030b57cec5SDimitry Andric{
1040b57cec5SDimitry Andricpublic:
105fe6060f1SDimitry Andric    typedef void*                                 pointer;
106fe6060f1SDimitry Andric    typedef const void*                           const_pointer;
107fe6060f1SDimitry Andric    typedef void                                  value_type;
1080b57cec5SDimitry Andric
109fe6060f1SDimitry Andric    template <class _Up> struct rebind {typedef allocator<_Up> other;};
1100b57cec5SDimitry Andric};
1110b57cec5SDimitry Andric
1120b57cec5SDimitry Andrictemplate <class T>
1130b57cec5SDimitry Andricclass allocator
1140b57cec5SDimitry Andric{
1150b57cec5SDimitry Andricpublic:
116e8d8bef9SDimitry Andric    typedef size_t    size_type;
117e8d8bef9SDimitry Andric    typedef ptrdiff_t difference_type;
1185ffd83dbSDimitry Andric    typedef T*        pointer;                           // deprecated in C++17, removed in C++20
1195ffd83dbSDimitry Andric    typedef const T*  const_pointer;                     // deprecated in C++17, removed in C++20
1205ffd83dbSDimitry Andric    typedef typename add_lvalue_reference<T>::type
1215ffd83dbSDimitry Andric                      reference;                         // deprecated in C++17, removed in C++20
1225ffd83dbSDimitry Andric    typedef typename add_lvalue_reference<const T>::type
1235ffd83dbSDimitry Andric                      const_reference;                   // deprecated in C++17, removed in C++20
1245ffd83dbSDimitry Andric
1250b57cec5SDimitry Andric    typedef T         value_type;
1260b57cec5SDimitry Andric
1275ffd83dbSDimitry Andric    template <class U> struct rebind {typedef allocator<U> other;}; // deprecated in C++17, removed in C++20
1285ffd83dbSDimitry Andric
1295ffd83dbSDimitry Andric    typedef true_type propagate_on_container_move_assignment;
1305ffd83dbSDimitry Andric    typedef true_type is_always_equal;
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric    constexpr allocator() noexcept;                      // constexpr in C++20
1330b57cec5SDimitry Andric    constexpr allocator(const allocator&) noexcept;      // constexpr in C++20
1340b57cec5SDimitry Andric    template <class U>
1350b57cec5SDimitry Andric      constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
136e8d8bef9SDimitry Andric    ~allocator();                                        // constexpr in C++20
1375ffd83dbSDimitry Andric    pointer address(reference x) const noexcept;             // deprecated in C++17, removed in C++20
1385ffd83dbSDimitry Andric    const_pointer address(const_reference x) const noexcept; // deprecated in C++17, removed in C++20
1395ffd83dbSDimitry Andric    T* allocate(size_t n, const void* hint);          // deprecated in C++17, removed in C++20
140e8d8bef9SDimitry Andric    T* allocate(size_t n);                              // constexpr in C++20
141e8d8bef9SDimitry Andric    void deallocate(T* p, size_t n) noexcept;           // constexpr in C++20
1425ffd83dbSDimitry Andric    size_type max_size() const noexcept;              // deprecated in C++17, removed in C++20
1430b57cec5SDimitry Andric    template<class U, class... Args>
1445ffd83dbSDimitry Andric        void construct(U* p, Args&&... args);         // deprecated in C++17, removed in C++20
1450b57cec5SDimitry Andric    template <class U>
1465ffd83dbSDimitry Andric        void destroy(U* p);                           // deprecated in C++17, removed in C++20
1470b57cec5SDimitry Andric};
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andrictemplate <class T, class U>
150e8d8bef9SDimitry Andricbool operator==(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
1510b57cec5SDimitry Andric
1520b57cec5SDimitry Andrictemplate <class T, class U>
153e8d8bef9SDimitry Andricbool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // constexpr in C++20
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andrictemplate <class OutputIterator, class T>
156fe6060f1SDimitry Andricclass raw_storage_iterator // deprecated in C++17, removed in C++20
157fe6060f1SDimitry Andric    : public iterator<output_iterator_tag, void, void, void, void> // until C++17
1580b57cec5SDimitry Andric{
1590b57cec5SDimitry Andricpublic:
160fe6060f1SDimitry Andric    typedef output_iterator_tag iterator_category;
161fe6060f1SDimitry Andric    typedef void                value_type;
162fe6060f1SDimitry Andric    typedef void                difference_type; // until C++20
163fe6060f1SDimitry Andric    typedef ptrdiff_t           difference_type; // since C++20
164fe6060f1SDimitry Andric    typedef void                pointer;
165fe6060f1SDimitry Andric    typedef void                reference;
166fe6060f1SDimitry Andric
1670b57cec5SDimitry Andric    explicit raw_storage_iterator(OutputIterator x);
1680b57cec5SDimitry Andric    raw_storage_iterator& operator*();
1690b57cec5SDimitry Andric    raw_storage_iterator& operator=(const T& element);
1700b57cec5SDimitry Andric    raw_storage_iterator& operator++();
1710b57cec5SDimitry Andric    raw_storage_iterator  operator++(int);
1720b57cec5SDimitry Andric};
1730b57cec5SDimitry Andric
1740b57cec5SDimitry Andrictemplate <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
1750b57cec5SDimitry Andrictemplate <class T> void               return_temporary_buffer(T* p) noexcept;
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andrictemplate <class T> T* addressof(T& r) noexcept;
1780b57cec5SDimitry Andrictemplate <class T> T* addressof(const T&& r) noexcept = delete;
1790b57cec5SDimitry Andric
1800b57cec5SDimitry Andrictemplate <class InputIterator, class ForwardIterator>
1810b57cec5SDimitry AndricForwardIterator
1820b57cec5SDimitry Andricuninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
1830b57cec5SDimitry Andric
184*04eeddc0SDimitry Andricnamespace ranges {
185*04eeddc0SDimitry Andric
186*04eeddc0SDimitry Andrictemplate<class InputIterator, class OutputIterator>
187*04eeddc0SDimitry Andricusing uninitialized_copy_result = in_out_result<InputIterator, OutputIterator>; // since C++20
188*04eeddc0SDimitry Andric
189*04eeddc0SDimitry Andrictemplate<input_iterator InputIterator, sentinel-for<InputIterator> Sentinel1, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel2>
190*04eeddc0SDimitry Andric  requires constructible_from<iter_value_t<OutputIterator>, iter_reference_t<InputIterator>>
191*04eeddc0SDimitry Andricuninitialized_copy_result<InputIterator, OutputIterator>
192*04eeddc0SDimitry Andricuninitialized_copy(InputIterator ifirst, Sentinel1 ilast, OutputIterator ofirst, Sentinel2 olast); // since C++20
193*04eeddc0SDimitry Andric
194*04eeddc0SDimitry Andrictemplate<input_range InputRange, nothrow-forward-range OutputRange>
195*04eeddc0SDimitry Andric  requires constructible_from<range_value_t<OutputRange>, range_reference_t<InputRange>>
196*04eeddc0SDimitry Andricuninitialized_copy_result<borrowed_iterator_t<InputRange>, borrowed_iterator_t<OutputRange>>
197*04eeddc0SDimitry Andricuninitialized_copy(InputRange&& in_range, OutputRange&& out_range); // since C++20
198*04eeddc0SDimitry Andric
199*04eeddc0SDimitry Andric}
200*04eeddc0SDimitry Andric
2010b57cec5SDimitry Andrictemplate <class InputIterator, class Size, class ForwardIterator>
2020b57cec5SDimitry AndricForwardIterator
2030b57cec5SDimitry Andricuninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
2040b57cec5SDimitry Andric
205*04eeddc0SDimitry Andricnamespace ranges {
206*04eeddc0SDimitry Andric
207*04eeddc0SDimitry Andrictemplate<class InputIterator, class OutputIterator>
208*04eeddc0SDimitry Andricusing uninitialized_copy_n_result = in_out_result<InputIterator, OutputIterator>; // since C++20
209*04eeddc0SDimitry Andric
210*04eeddc0SDimitry Andrictemplate<input_iterator InputIterator, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel>
211*04eeddc0SDimitry Andric  requires constructible_from<iter_value_t<OutputIterator>, iter_reference_t<InputIterator>>
212*04eeddc0SDimitry Andricuninitialized_copy_n_result<InputIterator, OutputIterator>
213*04eeddc0SDimitry Andricuninitialized_copy_n(InputIterator ifirst, iter_difference_t<InputIterator> n, OutputIterator ofirst, Sentinel olast); // since C++20
214*04eeddc0SDimitry Andric
215*04eeddc0SDimitry Andric}
216*04eeddc0SDimitry Andric
2170b57cec5SDimitry Andrictemplate <class ForwardIterator, class T>
2180b57cec5SDimitry Andricvoid uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
2190b57cec5SDimitry Andric
220*04eeddc0SDimitry Andricnamespace ranges {
221*04eeddc0SDimitry Andric
2220eae32dcSDimitry Andrictemplate <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel, class T>
2230eae32dcSDimitry Andric  requires constructible_from<iter_value_t<ForwardIterator>, const T&>
224*04eeddc0SDimitry AndricForwardIterator uninitialized_fill(ForwardIterator first, Sentinel last, const T& x); // since C++20
2250eae32dcSDimitry Andric
2260eae32dcSDimitry Andrictemplate <nothrow-forward-range ForwardRange, class T>
2270eae32dcSDimitry Andric  requires constructible_from<range_value_t<ForwardRange>, const T&>
228*04eeddc0SDimitry Andricborrowed_iterator_t<ForwardRange> uninitialized_fill(ForwardRange&& range, const T& x); // since C++20
229*04eeddc0SDimitry Andric
230*04eeddc0SDimitry Andric}
2310eae32dcSDimitry Andric
2320b57cec5SDimitry Andrictemplate <class ForwardIterator, class Size, class T>
2330b57cec5SDimitry AndricForwardIterator
2340b57cec5SDimitry Andricuninitialized_fill_n(ForwardIterator first, Size n, const T& x);
2350b57cec5SDimitry Andric
236*04eeddc0SDimitry Andricnamespace ranges {
237*04eeddc0SDimitry Andric
2380eae32dcSDimitry Andrictemplate <nothrow-forward-iterator ForwardIterator, class T>
2390eae32dcSDimitry Andric  requires constructible_from<iter_value_t<ForwardIterator>, const T&>
240*04eeddc0SDimitry AndricForwardIterator uninitialized_fill_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
241*04eeddc0SDimitry Andric
242*04eeddc0SDimitry Andric}
2430eae32dcSDimitry Andric
244e8d8bef9SDimitry Andrictemplate <class T, class ...Args>
245e8d8bef9SDimitry Andricconstexpr T* construct_at(T* location, Args&& ...args); // since C++20
246e8d8bef9SDimitry Andric
247*04eeddc0SDimitry Andricnamespace ranges {
248*04eeddc0SDimitry Andric  template<class T, class... Args>
249*04eeddc0SDimitry Andric    constexpr T* construct_at(T* location, Args&&... args); // since C++20
250*04eeddc0SDimitry Andric}
251*04eeddc0SDimitry Andric
2520b57cec5SDimitry Andrictemplate <class T>
253e8d8bef9SDimitry Andricvoid destroy_at(T* location); // constexpr in C++20
2540b57cec5SDimitry Andric
255*04eeddc0SDimitry Andricnamespace ranges {
256*04eeddc0SDimitry Andric  template<destructible T>
257*04eeddc0SDimitry Andric    constexpr void destroy_at(T* location) noexcept; // since C++20
258*04eeddc0SDimitry Andric}
259*04eeddc0SDimitry Andric
2600b57cec5SDimitry Andrictemplate <class ForwardIterator>
261e8d8bef9SDimitry Andricvoid destroy(ForwardIterator first, ForwardIterator last); // constexpr in C++20
2620b57cec5SDimitry Andric
263*04eeddc0SDimitry Andricnamespace ranges {
264*04eeddc0SDimitry Andric  template<nothrow-input-iterator InputIterator, nothrow-sentinel-for<InputIterator> Sentinel>
265*04eeddc0SDimitry Andric    requires destructible<iter_value_t<InputIterator>>
266*04eeddc0SDimitry Andric    constexpr InputIterator destroy(InputIterator first, Sentinel last) noexcept; // since C++20
267*04eeddc0SDimitry Andric  template<nothrow-input-range InputRange>
268*04eeddc0SDimitry Andric    requires destructible<range_value_t<InputRange>>
269*04eeddc0SDimitry Andric    constexpr borrowed_iterator_t<InputRange> destroy(InputRange&& range) noexcept; // since C++20
270*04eeddc0SDimitry Andric}
271*04eeddc0SDimitry Andric
2720b57cec5SDimitry Andrictemplate <class ForwardIterator, class Size>
273e8d8bef9SDimitry AndricForwardIterator destroy_n(ForwardIterator first, Size n); // constexpr in C++20
2740b57cec5SDimitry Andric
275*04eeddc0SDimitry Andricnamespace ranges {
276*04eeddc0SDimitry Andric  template<nothrow-input-iterator InputIterator>
277*04eeddc0SDimitry Andric    requires destructible<iter_value_t<InputIterator>>
278*04eeddc0SDimitry Andric    constexpr InputIterator destroy_n(InputIterator first, iter_difference_t<InputIterator> n) noexcept; // since C++20
279*04eeddc0SDimitry Andric}
280*04eeddc0SDimitry Andric
2810b57cec5SDimitry Andrictemplate <class InputIterator, class ForwardIterator>
2820b57cec5SDimitry Andric ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
2830b57cec5SDimitry Andric
284*04eeddc0SDimitry Andricnamespace ranges {
285*04eeddc0SDimitry Andric
286*04eeddc0SDimitry Andrictemplate<class InputIterator, class OutputIterator>
287*04eeddc0SDimitry Andricusing uninitialized_move_result = in_out_result<InputIterator, OutputIterator>; // since C++20
288*04eeddc0SDimitry Andric
289*04eeddc0SDimitry Andrictemplate <input_iterator InputIterator, sentinel_for<InputIterator> Sentinel1, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<O> Sentinel2>
290*04eeddc0SDimitry Andric  requires constructible_from<iter_value_t<OutputIterator>, iter_rvalue_reference_t<InputIterator>>
291*04eeddc0SDimitry Andricuninitialized_move_result<InputIterator, OutputIterator>
292*04eeddc0SDimitry Andricuninitialized_move(InputIterator ifirst, Sentinel1 ilast, OutputIterator ofirst, Sentinel2 olast); // since C++20
293*04eeddc0SDimitry Andric
294*04eeddc0SDimitry Andrictemplate<input_range InputRange, nothrow-forward-range OutputRange>
295*04eeddc0SDimitry Andric  requires constructible_from<range_value_t<OutputRange>, range_rvalue_reference_t<InputRange>>
296*04eeddc0SDimitry Andricuninitialized_move_result<borrowed_iterator_t<InputRange>, borrowed_iterator_t<OutputRange>>
297*04eeddc0SDimitry Andricuninitialized_move(InputRange&& in_range, OutputRange&& out_range); // since C++20
298*04eeddc0SDimitry Andric
299*04eeddc0SDimitry Andric}
300*04eeddc0SDimitry Andric
3010b57cec5SDimitry Andrictemplate <class InputIterator, class Size, class ForwardIterator>
3020b57cec5SDimitry Andric pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
3030b57cec5SDimitry Andric
304*04eeddc0SDimitry Andricnamespace ranges {
305*04eeddc0SDimitry Andric
306*04eeddc0SDimitry Andrictemplate<class InputIterator, class OutputIterator>
307*04eeddc0SDimitry Andricusing uninitialized_move_n_result = in_out_result<InputIterator, OutputIterator>; // since C++20
308*04eeddc0SDimitry Andric
309*04eeddc0SDimitry Andrictemplate<input_iterator InputIterator, nothrow-forward-iterator OutputIterator, nothrow-sentinel-for<OutputIterator> Sentinel>
310*04eeddc0SDimitry Andric  requires constructible_from<iter_value_t<OutputIterator>, iter_rvalue_reference_t<InputIterator>>
311*04eeddc0SDimitry Andricuninitialized_move_n_result<InputIterator, OutputIterator>
312*04eeddc0SDimitry Andricuninitialized_move_n(InputIterator ifirst, iter_difference_t<InputIterator> n, OutputIterator ofirst, Sentinel olast); // since C++20
313*04eeddc0SDimitry Andric
314*04eeddc0SDimitry Andric}
315*04eeddc0SDimitry Andric
3160b57cec5SDimitry Andrictemplate <class ForwardIterator>
3170b57cec5SDimitry Andric void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
3180b57cec5SDimitry Andric
319*04eeddc0SDimitry Andricnamespace ranges {
320*04eeddc0SDimitry Andric
3210eae32dcSDimitry Andrictemplate <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel>
3220eae32dcSDimitry Andric  requires default_initializable<iter_value_t<ForwardIterator>>
323*04eeddc0SDimitry Andric ForwardIterator uninitialized_value_construct(ForwardIterator first, Sentinel last); // since C++20
3240eae32dcSDimitry Andric
3250eae32dcSDimitry Andrictemplate <nothrow-forward-range ForwardRange>
3260eae32dcSDimitry Andric  requires default_initializable<range_value_t<ForwardRange>>
327*04eeddc0SDimitry Andric borrowed_iterator_t<ForwardRange> uninitialized_value_construct(ForwardRange&& r); // since C++20
328*04eeddc0SDimitry Andric
329*04eeddc0SDimitry Andric}
3300eae32dcSDimitry Andric
3310b57cec5SDimitry Andrictemplate <class ForwardIterator, class Size>
3320b57cec5SDimitry Andric ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
3330b57cec5SDimitry Andric
334*04eeddc0SDimitry Andricnamespace ranges {
335*04eeddc0SDimitry Andric
3360eae32dcSDimitry Andrictemplate <nothrow-forward-iterator ForwardIterator>
3370eae32dcSDimitry Andric  requires default_initializable<iter_value_t<ForwardIterator>>
338*04eeddc0SDimitry Andric ForwardIterator uninitialized_value_construct_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
339*04eeddc0SDimitry Andric
340*04eeddc0SDimitry Andric}
3410eae32dcSDimitry Andric
3420b57cec5SDimitry Andrictemplate <class ForwardIterator>
3430b57cec5SDimitry Andric void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
3440b57cec5SDimitry Andric
345*04eeddc0SDimitry Andricnamespace ranges {
346*04eeddc0SDimitry Andric
3470eae32dcSDimitry Andrictemplate <nothrow-forward-iterator ForwardIterator, nothrow-sentinel-for<ForwardIterator> Sentinel>
3480eae32dcSDimitry Andric  requires default_initializable<iter_value_t<ForwardIterator>>
349*04eeddc0SDimitry Andric ForwardIterator uninitialized_default_construct(ForwardIterator first, Sentinel last); // since C++20
3500eae32dcSDimitry Andric
3510eae32dcSDimitry Andrictemplate <nothrow-forward-range ForwardRange>
3520eae32dcSDimitry Andric  requires default_initializable<range_value_t<ForwardRange>>
353*04eeddc0SDimitry Andric borrowed_iterator_t<ForwardRange> uninitialized_default_construct(ForwardRange&& r); // since C++20
354*04eeddc0SDimitry Andric
355*04eeddc0SDimitry Andric}
3560eae32dcSDimitry Andric
3570b57cec5SDimitry Andrictemplate <class ForwardIterator, class Size>
3580b57cec5SDimitry Andric ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
3590b57cec5SDimitry Andric
360*04eeddc0SDimitry Andricnamespace ranges {
361*04eeddc0SDimitry Andric
3620eae32dcSDimitry Andrictemplate <nothrow-forward-iterator ForwardIterator>
3630eae32dcSDimitry Andric  requires default_initializable<iter_value_t<ForwardIterator>>
364*04eeddc0SDimitry Andric ForwardIterator uninitialized_default_construct_n(ForwardIterator first, iter_difference_t<ForwardIterator> n); // since C++20
365*04eeddc0SDimitry Andric
366*04eeddc0SDimitry Andric}
3670eae32dcSDimitry Andric
3680b57cec5SDimitry Andrictemplate <class Y> struct auto_ptr_ref {};      // deprecated in C++11, removed in C++17
3690b57cec5SDimitry Andric
3700b57cec5SDimitry Andrictemplate<class X>
3710b57cec5SDimitry Andricclass auto_ptr                                  // deprecated in C++11, removed in C++17
3720b57cec5SDimitry Andric{
3730b57cec5SDimitry Andricpublic:
3740b57cec5SDimitry Andric    typedef X element_type;
3750b57cec5SDimitry Andric
3760b57cec5SDimitry Andric    explicit auto_ptr(X* p =0) throw();
3770b57cec5SDimitry Andric    auto_ptr(auto_ptr&) throw();
3780b57cec5SDimitry Andric    template<class Y> auto_ptr(auto_ptr<Y>&) throw();
3790b57cec5SDimitry Andric    auto_ptr& operator=(auto_ptr&) throw();
3800b57cec5SDimitry Andric    template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
3810b57cec5SDimitry Andric    auto_ptr& operator=(auto_ptr_ref<X> r) throw();
3820b57cec5SDimitry Andric    ~auto_ptr() throw();
3830b57cec5SDimitry Andric
3840b57cec5SDimitry Andric    typename add_lvalue_reference<X>::type operator*() const throw();
3850b57cec5SDimitry Andric    X* operator->() const throw();
3860b57cec5SDimitry Andric    X* get() const throw();
3870b57cec5SDimitry Andric    X* release() throw();
3880b57cec5SDimitry Andric    void reset(X* p =0) throw();
3890b57cec5SDimitry Andric
3900b57cec5SDimitry Andric    auto_ptr(auto_ptr_ref<X>) throw();
3910b57cec5SDimitry Andric    template<class Y> operator auto_ptr_ref<Y>() throw();
3920b57cec5SDimitry Andric    template<class Y> operator auto_ptr<Y>() throw();
3930b57cec5SDimitry Andric};
3940b57cec5SDimitry Andric
3950b57cec5SDimitry Andrictemplate <class T>
3960b57cec5SDimitry Andricstruct default_delete
3970b57cec5SDimitry Andric{
3980b57cec5SDimitry Andric    constexpr default_delete() noexcept = default;
3990b57cec5SDimitry Andric    template <class U> default_delete(const default_delete<U>&) noexcept;
4000b57cec5SDimitry Andric
4010b57cec5SDimitry Andric    void operator()(T*) const noexcept;
4020b57cec5SDimitry Andric};
4030b57cec5SDimitry Andric
4040b57cec5SDimitry Andrictemplate <class T>
4050b57cec5SDimitry Andricstruct default_delete<T[]>
4060b57cec5SDimitry Andric{
4070b57cec5SDimitry Andric    constexpr default_delete() noexcept = default;
4080b57cec5SDimitry Andric    void operator()(T*) const noexcept;
4090b57cec5SDimitry Andric    template <class U> void operator()(U*) const = delete;
4100b57cec5SDimitry Andric};
4110b57cec5SDimitry Andric
4120b57cec5SDimitry Andrictemplate <class T, class D = default_delete<T>>
4130b57cec5SDimitry Andricclass unique_ptr
4140b57cec5SDimitry Andric{
4150b57cec5SDimitry Andricpublic:
4160b57cec5SDimitry Andric    typedef see below pointer;
4170b57cec5SDimitry Andric    typedef T element_type;
4180b57cec5SDimitry Andric    typedef D deleter_type;
4190b57cec5SDimitry Andric
4200b57cec5SDimitry Andric    // constructors
4210b57cec5SDimitry Andric    constexpr unique_ptr() noexcept;
4220b57cec5SDimitry Andric    explicit unique_ptr(pointer p) noexcept;
4230b57cec5SDimitry Andric    unique_ptr(pointer p, see below d1) noexcept;
4240b57cec5SDimitry Andric    unique_ptr(pointer p, see below d2) noexcept;
4250b57cec5SDimitry Andric    unique_ptr(unique_ptr&& u) noexcept;
4260b57cec5SDimitry Andric    unique_ptr(nullptr_t) noexcept : unique_ptr() { }
4270b57cec5SDimitry Andric    template <class U, class E>
4280b57cec5SDimitry Andric        unique_ptr(unique_ptr<U, E>&& u) noexcept;
4290b57cec5SDimitry Andric    template <class U>
4300b57cec5SDimitry Andric        unique_ptr(auto_ptr<U>&& u) noexcept;       // removed in C++17
4310b57cec5SDimitry Andric
4320b57cec5SDimitry Andric    // destructor
4330b57cec5SDimitry Andric    ~unique_ptr();
4340b57cec5SDimitry Andric
4350b57cec5SDimitry Andric    // assignment
4360b57cec5SDimitry Andric    unique_ptr& operator=(unique_ptr&& u) noexcept;
4370b57cec5SDimitry Andric    template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
4380b57cec5SDimitry Andric    unique_ptr& operator=(nullptr_t) noexcept;
4390b57cec5SDimitry Andric
4400b57cec5SDimitry Andric    // observers
4410b57cec5SDimitry Andric    typename add_lvalue_reference<T>::type operator*() const;
4420b57cec5SDimitry Andric    pointer operator->() const noexcept;
4430b57cec5SDimitry Andric    pointer get() const noexcept;
4440b57cec5SDimitry Andric    deleter_type& get_deleter() noexcept;
4450b57cec5SDimitry Andric    const deleter_type& get_deleter() const noexcept;
4460b57cec5SDimitry Andric    explicit operator bool() const noexcept;
4470b57cec5SDimitry Andric
4480b57cec5SDimitry Andric    // modifiers
4490b57cec5SDimitry Andric    pointer release() noexcept;
4500b57cec5SDimitry Andric    void reset(pointer p = pointer()) noexcept;
4510b57cec5SDimitry Andric    void swap(unique_ptr& u) noexcept;
4520b57cec5SDimitry Andric};
4530b57cec5SDimitry Andric
4540b57cec5SDimitry Andrictemplate <class T, class D>
4550b57cec5SDimitry Andricclass unique_ptr<T[], D>
4560b57cec5SDimitry Andric{
4570b57cec5SDimitry Andricpublic:
4580b57cec5SDimitry Andric    typedef implementation-defined pointer;
4590b57cec5SDimitry Andric    typedef T element_type;
4600b57cec5SDimitry Andric    typedef D deleter_type;
4610b57cec5SDimitry Andric
4620b57cec5SDimitry Andric    // constructors
4630b57cec5SDimitry Andric    constexpr unique_ptr() noexcept;
4640b57cec5SDimitry Andric    explicit unique_ptr(pointer p) noexcept;
4650b57cec5SDimitry Andric    unique_ptr(pointer p, see below d) noexcept;
4660b57cec5SDimitry Andric    unique_ptr(pointer p, see below d) noexcept;
4670b57cec5SDimitry Andric    unique_ptr(unique_ptr&& u) noexcept;
4680b57cec5SDimitry Andric    unique_ptr(nullptr_t) noexcept : unique_ptr() { }
4690b57cec5SDimitry Andric
4700b57cec5SDimitry Andric    // destructor
4710b57cec5SDimitry Andric    ~unique_ptr();
4720b57cec5SDimitry Andric
4730b57cec5SDimitry Andric    // assignment
4740b57cec5SDimitry Andric    unique_ptr& operator=(unique_ptr&& u) noexcept;
4750b57cec5SDimitry Andric    unique_ptr& operator=(nullptr_t) noexcept;
4760b57cec5SDimitry Andric
4770b57cec5SDimitry Andric    // observers
4780b57cec5SDimitry Andric    T& operator[](size_t i) const;
4790b57cec5SDimitry Andric    pointer get() const noexcept;
4800b57cec5SDimitry Andric    deleter_type& get_deleter() noexcept;
4810b57cec5SDimitry Andric    const deleter_type& get_deleter() const noexcept;
4820b57cec5SDimitry Andric    explicit operator bool() const noexcept;
4830b57cec5SDimitry Andric
4840b57cec5SDimitry Andric    // modifiers
4850b57cec5SDimitry Andric    pointer release() noexcept;
4860b57cec5SDimitry Andric    void reset(pointer p = pointer()) noexcept;
4870b57cec5SDimitry Andric    void reset(nullptr_t) noexcept;
4880b57cec5SDimitry Andric  template <class U> void reset(U) = delete;
4890b57cec5SDimitry Andric    void swap(unique_ptr& u) noexcept;
4900b57cec5SDimitry Andric};
4910b57cec5SDimitry Andric
4920b57cec5SDimitry Andrictemplate <class T, class D>
4930b57cec5SDimitry Andric    void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
4940b57cec5SDimitry Andric
4950b57cec5SDimitry Andrictemplate <class T1, class D1, class T2, class D2>
4960b57cec5SDimitry Andric    bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
4970b57cec5SDimitry Andrictemplate <class T1, class D1, class T2, class D2>
4980b57cec5SDimitry Andric    bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
4990b57cec5SDimitry Andrictemplate <class T1, class D1, class T2, class D2>
5000b57cec5SDimitry Andric    bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
5010b57cec5SDimitry Andrictemplate <class T1, class D1, class T2, class D2>
5020b57cec5SDimitry Andric    bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
5030b57cec5SDimitry Andrictemplate <class T1, class D1, class T2, class D2>
5040b57cec5SDimitry Andric    bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
5050b57cec5SDimitry Andrictemplate <class T1, class D1, class T2, class D2>
5060b57cec5SDimitry Andric    bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
5070b57cec5SDimitry Andric
5080b57cec5SDimitry Andrictemplate <class T, class D>
5090b57cec5SDimitry Andric    bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
5100b57cec5SDimitry Andrictemplate <class T, class D>
5110b57cec5SDimitry Andric    bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
5120b57cec5SDimitry Andrictemplate <class T, class D>
5130b57cec5SDimitry Andric    bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
5140b57cec5SDimitry Andrictemplate <class T, class D>
5150b57cec5SDimitry Andric    bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
5160b57cec5SDimitry Andric
5170b57cec5SDimitry Andrictemplate <class T, class D>
5180b57cec5SDimitry Andric    bool operator<(const unique_ptr<T, D>& x, nullptr_t);
5190b57cec5SDimitry Andrictemplate <class T, class D>
5200b57cec5SDimitry Andric    bool operator<(nullptr_t, const unique_ptr<T, D>& y);
5210b57cec5SDimitry Andrictemplate <class T, class D>
5220b57cec5SDimitry Andric    bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
5230b57cec5SDimitry Andrictemplate <class T, class D>
5240b57cec5SDimitry Andric    bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
5250b57cec5SDimitry Andrictemplate <class T, class D>
5260b57cec5SDimitry Andric    bool operator>(const unique_ptr<T, D>& x, nullptr_t);
5270b57cec5SDimitry Andrictemplate <class T, class D>
5280b57cec5SDimitry Andric    bool operator>(nullptr_t, const unique_ptr<T, D>& y);
5290b57cec5SDimitry Andrictemplate <class T, class D>
5300b57cec5SDimitry Andric    bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
5310b57cec5SDimitry Andrictemplate <class T, class D>
5320b57cec5SDimitry Andric    bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
5330b57cec5SDimitry Andric
5340b57cec5SDimitry Andricclass bad_weak_ptr
5350b57cec5SDimitry Andric    : public std::exception
5360b57cec5SDimitry Andric{
5370b57cec5SDimitry Andric    bad_weak_ptr() noexcept;
5380b57cec5SDimitry Andric};
5390b57cec5SDimitry Andric
5400b57cec5SDimitry Andrictemplate<class T, class... Args> unique_ptr<T> make_unique(Args&&... args);     // C++14
5410b57cec5SDimitry Andrictemplate<class T>                unique_ptr<T> make_unique(size_t n);           // C++14
5420b57cec5SDimitry Andrictemplate<class T, class... Args> unspecified   make_unique(Args&&...) = delete; // C++14, T == U[N]
5430b57cec5SDimitry Andric
5440b57cec5SDimitry Andrictemplate<class E, class T, class Y, class D>
5450b57cec5SDimitry Andric    basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
5460b57cec5SDimitry Andric
5470b57cec5SDimitry Andrictemplate<class T>
5480b57cec5SDimitry Andricclass shared_ptr
5490b57cec5SDimitry Andric{
5500b57cec5SDimitry Andricpublic:
551349cc55cSDimitry Andric    typedef T element_type; // until C++17
552349cc55cSDimitry Andric    typedef remove_extent_t<T> element_type; // since C++17
5530b57cec5SDimitry Andric    typedef weak_ptr<T> weak_type; // C++17
5540b57cec5SDimitry Andric
5550b57cec5SDimitry Andric    // constructors:
5560b57cec5SDimitry Andric    constexpr shared_ptr() noexcept;
5570b57cec5SDimitry Andric    template<class Y> explicit shared_ptr(Y* p);
5580b57cec5SDimitry Andric    template<class Y, class D> shared_ptr(Y* p, D d);
5590b57cec5SDimitry Andric    template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
5600b57cec5SDimitry Andric    template <class D> shared_ptr(nullptr_t p, D d);
5610b57cec5SDimitry Andric    template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
5620b57cec5SDimitry Andric    template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
5630b57cec5SDimitry Andric    shared_ptr(const shared_ptr& r) noexcept;
5640b57cec5SDimitry Andric    template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
5650b57cec5SDimitry Andric    shared_ptr(shared_ptr&& r) noexcept;
5660b57cec5SDimitry Andric    template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
5670b57cec5SDimitry Andric    template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
5680b57cec5SDimitry Andric    template<class Y> shared_ptr(auto_ptr<Y>&& r);          // removed in C++17
5690b57cec5SDimitry Andric    template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
5700b57cec5SDimitry Andric    shared_ptr(nullptr_t) : shared_ptr() { }
5710b57cec5SDimitry Andric
5720b57cec5SDimitry Andric    // destructor:
5730b57cec5SDimitry Andric    ~shared_ptr();
5740b57cec5SDimitry Andric
5750b57cec5SDimitry Andric    // assignment:
5760b57cec5SDimitry Andric    shared_ptr& operator=(const shared_ptr& r) noexcept;
5770b57cec5SDimitry Andric    template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
5780b57cec5SDimitry Andric    shared_ptr& operator=(shared_ptr&& r) noexcept;
5790b57cec5SDimitry Andric    template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
5800b57cec5SDimitry Andric    template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
5810b57cec5SDimitry Andric    template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
5820b57cec5SDimitry Andric
5830b57cec5SDimitry Andric    // modifiers:
5840b57cec5SDimitry Andric    void swap(shared_ptr& r) noexcept;
5850b57cec5SDimitry Andric    void reset() noexcept;
5860b57cec5SDimitry Andric    template<class Y> void reset(Y* p);
5870b57cec5SDimitry Andric    template<class Y, class D> void reset(Y* p, D d);
5880b57cec5SDimitry Andric    template<class Y, class D, class A> void reset(Y* p, D d, A a);
5890b57cec5SDimitry Andric
5900b57cec5SDimitry Andric    // observers:
5910b57cec5SDimitry Andric    T* get() const noexcept;
5920b57cec5SDimitry Andric    T& operator*() const noexcept;
5930b57cec5SDimitry Andric    T* operator->() const noexcept;
5940b57cec5SDimitry Andric    long use_count() const noexcept;
5950b57cec5SDimitry Andric    bool unique() const noexcept;
5960b57cec5SDimitry Andric    explicit operator bool() const noexcept;
5970b57cec5SDimitry Andric    template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
5980b57cec5SDimitry Andric    template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
5990b57cec5SDimitry Andric};
6000b57cec5SDimitry Andric
6015ffd83dbSDimitry Andrictemplate<class T>
6025ffd83dbSDimitry Andricshared_ptr(weak_ptr<T>) -> shared_ptr<T>;
6035ffd83dbSDimitry Andrictemplate<class T, class D>
6045ffd83dbSDimitry Andricshared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
6055ffd83dbSDimitry Andric
6060b57cec5SDimitry Andric// shared_ptr comparisons:
6070b57cec5SDimitry Andrictemplate<class T, class U>
6080b57cec5SDimitry Andric    bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
6090b57cec5SDimitry Andrictemplate<class T, class U>
6100b57cec5SDimitry Andric    bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
6110b57cec5SDimitry Andrictemplate<class T, class U>
6120b57cec5SDimitry Andric    bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
6130b57cec5SDimitry Andrictemplate<class T, class U>
6140b57cec5SDimitry Andric    bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
6150b57cec5SDimitry Andrictemplate<class T, class U>
6160b57cec5SDimitry Andric    bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
6170b57cec5SDimitry Andrictemplate<class T, class U>
6180b57cec5SDimitry Andric    bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
6190b57cec5SDimitry Andric
6200b57cec5SDimitry Andrictemplate <class T>
6210b57cec5SDimitry Andric    bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
6220b57cec5SDimitry Andrictemplate <class T>
6230b57cec5SDimitry Andric    bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
6240b57cec5SDimitry Andrictemplate <class T>
6250b57cec5SDimitry Andric    bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
6260b57cec5SDimitry Andrictemplate <class T>
6270b57cec5SDimitry Andric    bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
6280b57cec5SDimitry Andrictemplate <class T>
6290b57cec5SDimitry Andric    bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
6300b57cec5SDimitry Andrictemplate <class T>
6310b57cec5SDimitry Andricbool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
6320b57cec5SDimitry Andrictemplate <class T>
6330b57cec5SDimitry Andric    bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
6340b57cec5SDimitry Andrictemplate <class T>
6350b57cec5SDimitry Andric    bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
6360b57cec5SDimitry Andrictemplate <class T>
6370b57cec5SDimitry Andric    bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
6380b57cec5SDimitry Andrictemplate <class T>
6390b57cec5SDimitry Andric    bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
6400b57cec5SDimitry Andrictemplate <class T>
6410b57cec5SDimitry Andric    bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
6420b57cec5SDimitry Andrictemplate <class T>
6430b57cec5SDimitry Andric    bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
6440b57cec5SDimitry Andric
6450b57cec5SDimitry Andric// shared_ptr specialized algorithms:
6460b57cec5SDimitry Andrictemplate<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
6470b57cec5SDimitry Andric
6480b57cec5SDimitry Andric// shared_ptr casts:
6490b57cec5SDimitry Andrictemplate<class T, class U>
6500b57cec5SDimitry Andric    shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
6510b57cec5SDimitry Andrictemplate<class T, class U>
6520b57cec5SDimitry Andric    shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
6530b57cec5SDimitry Andrictemplate<class T, class U>
6540b57cec5SDimitry Andric    shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
6550b57cec5SDimitry Andric
6560b57cec5SDimitry Andric// shared_ptr I/O:
6570b57cec5SDimitry Andrictemplate<class E, class T, class Y>
6580b57cec5SDimitry Andric    basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
6590b57cec5SDimitry Andric
6600b57cec5SDimitry Andric// shared_ptr get_deleter:
6610b57cec5SDimitry Andrictemplate<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
6620b57cec5SDimitry Andric
6630b57cec5SDimitry Andrictemplate<class T, class... Args>
6640b57cec5SDimitry Andric    shared_ptr<T> make_shared(Args&&... args);
6650b57cec5SDimitry Andrictemplate<class T, class A, class... Args>
6660b57cec5SDimitry Andric    shared_ptr<T> allocate_shared(const A& a, Args&&... args);
6670b57cec5SDimitry Andric
6680b57cec5SDimitry Andrictemplate<class T>
6690b57cec5SDimitry Andricclass weak_ptr
6700b57cec5SDimitry Andric{
6710b57cec5SDimitry Andricpublic:
672349cc55cSDimitry Andric    typedef T element_type; // until C++17
673349cc55cSDimitry Andric    typedef remove_extent_t<T> element_type; // since C++17
6740b57cec5SDimitry Andric
6750b57cec5SDimitry Andric    // constructors
6760b57cec5SDimitry Andric    constexpr weak_ptr() noexcept;
6770b57cec5SDimitry Andric    template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
6780b57cec5SDimitry Andric    weak_ptr(weak_ptr const& r) noexcept;
6790b57cec5SDimitry Andric    template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
6800b57cec5SDimitry Andric    weak_ptr(weak_ptr&& r) noexcept;                      // C++14
6810b57cec5SDimitry Andric    template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
6820b57cec5SDimitry Andric
6830b57cec5SDimitry Andric    // destructor
6840b57cec5SDimitry Andric    ~weak_ptr();
6850b57cec5SDimitry Andric
6860b57cec5SDimitry Andric    // assignment
6870b57cec5SDimitry Andric    weak_ptr& operator=(weak_ptr const& r) noexcept;
6880b57cec5SDimitry Andric    template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
6890b57cec5SDimitry Andric    template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
6900b57cec5SDimitry Andric    weak_ptr& operator=(weak_ptr&& r) noexcept;                      // C++14
6910b57cec5SDimitry Andric    template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
6920b57cec5SDimitry Andric
6930b57cec5SDimitry Andric    // modifiers
6940b57cec5SDimitry Andric    void swap(weak_ptr& r) noexcept;
6950b57cec5SDimitry Andric    void reset() noexcept;
6960b57cec5SDimitry Andric
6970b57cec5SDimitry Andric    // observers
6980b57cec5SDimitry Andric    long use_count() const noexcept;
6990b57cec5SDimitry Andric    bool expired() const noexcept;
7000b57cec5SDimitry Andric    shared_ptr<T> lock() const noexcept;
7010b57cec5SDimitry Andric    template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
7020b57cec5SDimitry Andric    template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
7030b57cec5SDimitry Andric};
7040b57cec5SDimitry Andric
7055ffd83dbSDimitry Andrictemplate<class T>
7065ffd83dbSDimitry Andricweak_ptr(shared_ptr<T>) -> weak_ptr<T>;
7075ffd83dbSDimitry Andric
7080b57cec5SDimitry Andric// weak_ptr specialized algorithms:
7090b57cec5SDimitry Andrictemplate<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
7100b57cec5SDimitry Andric
7110b57cec5SDimitry Andric// class owner_less:
7120b57cec5SDimitry Andrictemplate<class T> struct owner_less;
7130b57cec5SDimitry Andric
7140b57cec5SDimitry Andrictemplate<class T>
7150b57cec5SDimitry Andricstruct owner_less<shared_ptr<T> >
7160b57cec5SDimitry Andric    : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
7170b57cec5SDimitry Andric{
7180b57cec5SDimitry Andric    typedef bool result_type;
7190b57cec5SDimitry Andric    bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
7200b57cec5SDimitry Andric    bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
7210b57cec5SDimitry Andric    bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
7220b57cec5SDimitry Andric};
7230b57cec5SDimitry Andric
7240b57cec5SDimitry Andrictemplate<class T>
7250b57cec5SDimitry Andricstruct owner_less<weak_ptr<T> >
7260b57cec5SDimitry Andric    : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
7270b57cec5SDimitry Andric{
7280b57cec5SDimitry Andric    typedef bool result_type;
7290b57cec5SDimitry Andric    bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
7300b57cec5SDimitry Andric    bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
7310b57cec5SDimitry Andric    bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
7320b57cec5SDimitry Andric};
7330b57cec5SDimitry Andric
7340b57cec5SDimitry Andrictemplate <>  // Added in C++14
7350b57cec5SDimitry Andricstruct owner_less<void>
7360b57cec5SDimitry Andric{
7370b57cec5SDimitry Andric    template <class _Tp, class _Up>
7380b57cec5SDimitry Andric    bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
7390b57cec5SDimitry Andric    template <class _Tp, class _Up>
7400b57cec5SDimitry Andric    bool operator()( shared_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const noexcept;
7410b57cec5SDimitry Andric    template <class _Tp, class _Up>
7420b57cec5SDimitry Andric    bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
7430b57cec5SDimitry Andric    template <class _Tp, class _Up>
7440b57cec5SDimitry Andric    bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const noexcept;
7450b57cec5SDimitry Andric
7460b57cec5SDimitry Andric    typedef void is_transparent;
7470b57cec5SDimitry Andric};
7480b57cec5SDimitry Andric
7490b57cec5SDimitry Andrictemplate<class T>
7500b57cec5SDimitry Andricclass enable_shared_from_this
7510b57cec5SDimitry Andric{
7520b57cec5SDimitry Andricprotected:
7530b57cec5SDimitry Andric    constexpr enable_shared_from_this() noexcept;
7540b57cec5SDimitry Andric    enable_shared_from_this(enable_shared_from_this const&) noexcept;
7550b57cec5SDimitry Andric    enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
7560b57cec5SDimitry Andric    ~enable_shared_from_this();
7570b57cec5SDimitry Andricpublic:
7580b57cec5SDimitry Andric    shared_ptr<T> shared_from_this();
7590b57cec5SDimitry Andric    shared_ptr<T const> shared_from_this() const;
7600b57cec5SDimitry Andric};
7610b57cec5SDimitry Andric
7620b57cec5SDimitry Andrictemplate<class T>
7630b57cec5SDimitry Andric    bool atomic_is_lock_free(const shared_ptr<T>* p);
7640b57cec5SDimitry Andrictemplate<class T>
7650b57cec5SDimitry Andric    shared_ptr<T> atomic_load(const shared_ptr<T>* p);
7660b57cec5SDimitry Andrictemplate<class T>
7670b57cec5SDimitry Andric    shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
7680b57cec5SDimitry Andrictemplate<class T>
7690b57cec5SDimitry Andric    void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
7700b57cec5SDimitry Andrictemplate<class T>
7710b57cec5SDimitry Andric    void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
7720b57cec5SDimitry Andrictemplate<class T>
7730b57cec5SDimitry Andric    shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
7740b57cec5SDimitry Andrictemplate<class T>
7750b57cec5SDimitry Andric    shared_ptr<T>
7760b57cec5SDimitry Andric    atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
7770b57cec5SDimitry Andrictemplate<class T>
7780b57cec5SDimitry Andric    bool
7790b57cec5SDimitry Andric    atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
7800b57cec5SDimitry Andrictemplate<class T>
7810b57cec5SDimitry Andric    bool
7820b57cec5SDimitry Andric    atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
7830b57cec5SDimitry Andrictemplate<class T>
7840b57cec5SDimitry Andric    bool
7850b57cec5SDimitry Andric    atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
7860b57cec5SDimitry Andric                                          shared_ptr<T> w, memory_order success,
7870b57cec5SDimitry Andric                                          memory_order failure);
7880b57cec5SDimitry Andrictemplate<class T>
7890b57cec5SDimitry Andric    bool
7900b57cec5SDimitry Andric    atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
7910b57cec5SDimitry Andric                                            shared_ptr<T> w, memory_order success,
7920b57cec5SDimitry Andric                                            memory_order failure);
7930b57cec5SDimitry Andric// Hash support
7940b57cec5SDimitry Andrictemplate <class T> struct hash;
7950b57cec5SDimitry Andrictemplate <class T, class D> struct hash<unique_ptr<T, D> >;
7960b57cec5SDimitry Andrictemplate <class T> struct hash<shared_ptr<T> >;
7970b57cec5SDimitry Andric
7980b57cec5SDimitry Andrictemplate <class T, class Alloc>
7990b57cec5SDimitry Andric  inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
8000b57cec5SDimitry Andric
8010b57cec5SDimitry Andricvoid* align(size_t alignment, size_t size, void*& ptr, size_t& space);
8020b57cec5SDimitry Andric
8030b57cec5SDimitry Andric}  // std
8040b57cec5SDimitry Andric
8050b57cec5SDimitry Andric*/
8060b57cec5SDimitry Andric
8070b57cec5SDimitry Andric#include <__config>
808fe6060f1SDimitry Andric#include <__functional_base>
809fe6060f1SDimitry Andric#include <__memory/addressof.h>
810fe6060f1SDimitry Andric#include <__memory/allocation_guard.h>
811fe6060f1SDimitry Andric#include <__memory/allocator.h>
812fe6060f1SDimitry Andric#include <__memory/allocator_arg_t.h>
813fe6060f1SDimitry Andric#include <__memory/allocator_traits.h>
814fe6060f1SDimitry Andric#include <__memory/compressed_pair.h>
8150eae32dcSDimitry Andric#include <__memory/concepts.h>
816fe6060f1SDimitry Andric#include <__memory/construct_at.h>
817fe6060f1SDimitry Andric#include <__memory/pointer_traits.h>
818*04eeddc0SDimitry Andric#include <__memory/ranges_construct_at.h>
8190eae32dcSDimitry Andric#include <__memory/ranges_uninitialized_algorithms.h>
820fe6060f1SDimitry Andric#include <__memory/raw_storage_iterator.h>
821fe6060f1SDimitry Andric#include <__memory/shared_ptr.h>
822fe6060f1SDimitry Andric#include <__memory/temporary_buffer.h>
823fe6060f1SDimitry Andric#include <__memory/uninitialized_algorithms.h>
824fe6060f1SDimitry Andric#include <__memory/unique_ptr.h>
825fe6060f1SDimitry Andric#include <__memory/uses_allocator.h>
826fe6060f1SDimitry Andric#include <compare>
8270b57cec5SDimitry Andric#include <cstddef>
8280b57cec5SDimitry Andric#include <cstdint>
8290b57cec5SDimitry Andric#include <cstring>
830fe6060f1SDimitry Andric#include <iosfwd>
831fe6060f1SDimitry Andric#include <iterator>
832fe6060f1SDimitry Andric#include <new>
833fe6060f1SDimitry Andric#include <stdexcept>
834fe6060f1SDimitry Andric#include <tuple>
835fe6060f1SDimitry Andric#include <type_traits>
836fe6060f1SDimitry Andric#include <typeinfo>
837fe6060f1SDimitry Andric#include <utility>
8380b57cec5SDimitry Andric#include <version>
8390b57cec5SDimitry Andric
840fe6060f1SDimitry Andric#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
841fe6060f1SDimitry Andric#   include <__memory/auto_ptr.h>
842fe6060f1SDimitry Andric#endif
843fe6060f1SDimitry Andric
8440b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
8450b57cec5SDimitry Andric#pragma GCC system_header
8460b57cec5SDimitry Andric#endif
8470b57cec5SDimitry Andric
8480b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
8490b57cec5SDimitry Andric
8500b57cec5SDimitry Andrictemplate <class _Alloc, class _Ptr>
8510b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
852e8d8bef9SDimitry Andricvoid __construct_forward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2) {
853e8d8bef9SDimitry Andric    static_assert(__is_cpp17_move_insertable<_Alloc>::value,
854e8d8bef9SDimitry Andric        "The specified type does not meet the requirements of Cpp17MoveInsertable");
855e8d8bef9SDimitry Andric    typedef allocator_traits<_Alloc> _Traits;
856e8d8bef9SDimitry Andric    for (; __begin1 != __end1; ++__begin1, (void)++__begin2) {
857e8d8bef9SDimitry Andric        _Traits::construct(__a, _VSTD::__to_address(__begin2),
858e40139ffSDimitry Andric#ifdef _LIBCPP_NO_EXCEPTIONS
859e40139ffSDimitry Andric            _VSTD::move(*__begin1)
860e40139ffSDimitry Andric#else
861e40139ffSDimitry Andric            _VSTD::move_if_noexcept(*__begin1)
862e40139ffSDimitry Andric#endif
863e40139ffSDimitry Andric        );
8640b57cec5SDimitry Andric    }
865e8d8bef9SDimitry Andric}
8660b57cec5SDimitry Andric
867e8d8bef9SDimitry Andrictemplate <class _Alloc, class _Tp, typename enable_if<
868e8d8bef9SDimitry Andric    (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
869e8d8bef9SDimitry Andric    is_trivially_move_constructible<_Tp>::value
870e8d8bef9SDimitry Andric>::type>
8710b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
872e8d8bef9SDimitry Andricvoid __construct_forward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2) {
8730b57cec5SDimitry Andric    ptrdiff_t _Np = __end1 - __begin1;
874e8d8bef9SDimitry Andric    if (_Np > 0) {
8750b57cec5SDimitry Andric        _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
8760b57cec5SDimitry Andric        __begin2 += _Np;
8770b57cec5SDimitry Andric    }
8780b57cec5SDimitry Andric}
8790b57cec5SDimitry Andric
880e8d8bef9SDimitry Andrictemplate <class _Alloc, class _Iter, class _Ptr>
8810b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
882e8d8bef9SDimitry Andricvoid __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2) {
883e8d8bef9SDimitry Andric    typedef allocator_traits<_Alloc> _Traits;
884e8d8bef9SDimitry Andric    for (; __begin1 != __end1; ++__begin1, (void) ++__begin2) {
885e8d8bef9SDimitry Andric        _Traits::construct(__a, _VSTD::__to_address(__begin2), *__begin1);
886e8d8bef9SDimitry Andric    }
8870b57cec5SDimitry Andric}
8880b57cec5SDimitry Andric
889e8d8bef9SDimitry Andrictemplate <class _Alloc, class _Source, class _Dest,
890e8d8bef9SDimitry Andric          class _RawSource = typename remove_const<_Source>::type,
891e8d8bef9SDimitry Andric          class _RawDest = typename remove_const<_Dest>::type,
892e8d8bef9SDimitry Andric          class =
893e8d8bef9SDimitry Andric    typename enable_if<
894e8d8bef9SDimitry Andric        is_trivially_copy_constructible<_Dest>::value &&
895e8d8bef9SDimitry Andric        is_same<_RawSource, _RawDest>::value &&
896e8d8bef9SDimitry Andric        (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
897e8d8bef9SDimitry Andric    >::type>
8980b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
899e8d8bef9SDimitry Andricvoid __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
9000b57cec5SDimitry Andric    ptrdiff_t _Np = __end1 - __begin1;
901e8d8bef9SDimitry Andric    if (_Np > 0) {
902e8d8bef9SDimitry Andric        _VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
9030b57cec5SDimitry Andric        __begin2 += _Np;
9040b57cec5SDimitry Andric    }
9050b57cec5SDimitry Andric}
9060b57cec5SDimitry Andric
907e8d8bef9SDimitry Andrictemplate <class _Alloc, class _Ptr>
9080b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
909e8d8bef9SDimitry Andricvoid __construct_backward_with_exception_guarantees(_Alloc& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2) {
910e8d8bef9SDimitry Andric    static_assert(__is_cpp17_move_insertable<_Alloc>::value,
911e40139ffSDimitry Andric        "The specified type does not meet the requirements of Cpp17MoveInsertable");
912e8d8bef9SDimitry Andric    typedef allocator_traits<_Alloc> _Traits;
913e8d8bef9SDimitry Andric    while (__end1 != __begin1) {
914e8d8bef9SDimitry Andric        _Traits::construct(__a, _VSTD::__to_address(__end2 - 1),
915e40139ffSDimitry Andric#ifdef _LIBCPP_NO_EXCEPTIONS
916e40139ffSDimitry Andric            _VSTD::move(*--__end1)
917e40139ffSDimitry Andric#else
918e40139ffSDimitry Andric            _VSTD::move_if_noexcept(*--__end1)
919e40139ffSDimitry Andric#endif
920e40139ffSDimitry Andric        );
9210b57cec5SDimitry Andric        --__end2;
9220b57cec5SDimitry Andric    }
9230b57cec5SDimitry Andric}
9240b57cec5SDimitry Andric
925e8d8bef9SDimitry Andrictemplate <class _Alloc, class _Tp, class = typename enable_if<
926e8d8bef9SDimitry Andric    (__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Tp*, _Tp>::value) &&
927e8d8bef9SDimitry Andric    is_trivially_move_constructible<_Tp>::value
928e8d8bef9SDimitry Andric>::type>
9290b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
930e8d8bef9SDimitry Andricvoid __construct_backward_with_exception_guarantees(_Alloc&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2) {
9310b57cec5SDimitry Andric    ptrdiff_t _Np = __end1 - __begin1;
9320b57cec5SDimitry Andric    __end2 -= _Np;
9330b57cec5SDimitry Andric    if (_Np > 0)
934fe6060f1SDimitry Andric        _VSTD::memcpy(static_cast<void*>(__end2), static_cast<void const*>(__begin1), _Np * sizeof(_Tp));
9350b57cec5SDimitry Andric}
9360b57cec5SDimitry Andric
9370b57cec5SDimitry Andricstruct __destruct_n
9380b57cec5SDimitry Andric{
9390b57cec5SDimitry Andricprivate:
9400b57cec5SDimitry Andric    size_t __size_;
9410b57cec5SDimitry Andric
9420b57cec5SDimitry Andric    template <class _Tp>
9430b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
9440b57cec5SDimitry Andric        {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
9450b57cec5SDimitry Andric
9460b57cec5SDimitry Andric    template <class _Tp>
9470b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
9480b57cec5SDimitry Andric        {}
9490b57cec5SDimitry Andric
9500b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
9510b57cec5SDimitry Andric        {++__size_;}
9520b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
9530b57cec5SDimitry Andric        {}
9540b57cec5SDimitry Andric
9550b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
9560b57cec5SDimitry Andric        {__size_ = __s;}
9570b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
9580b57cec5SDimitry Andric        {}
9590b57cec5SDimitry Andricpublic:
9600b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
9610b57cec5SDimitry Andric        : __size_(__s) {}
9620b57cec5SDimitry Andric
9630b57cec5SDimitry Andric    template <class _Tp>
964e8d8bef9SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
9650b57cec5SDimitry Andric        {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
9660b57cec5SDimitry Andric
9670b57cec5SDimitry Andric    template <class _Tp>
9680b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
9690b57cec5SDimitry Andric        {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
9700b57cec5SDimitry Andric
9710b57cec5SDimitry Andric    template <class _Tp>
9720b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
9730b57cec5SDimitry Andric        {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
9740b57cec5SDimitry Andric};
9750b57cec5SDimitry Andric
9760b57cec5SDimitry Andric_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
9770b57cec5SDimitry Andric
9780b57cec5SDimitry Andric// --- Helper for container swap --
9790b57cec5SDimitry Andrictemplate <typename _Alloc>
9800eae32dcSDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
9810b57cec5SDimitry Andricvoid __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
9820eae32dcSDimitry Andric#if _LIBCPP_STD_VER > 11
9830b57cec5SDimitry Andric    _NOEXCEPT
9840b57cec5SDimitry Andric#else
9850b57cec5SDimitry Andric    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
9860b57cec5SDimitry Andric#endif
9870b57cec5SDimitry Andric{
9880b57cec5SDimitry Andric    using _VSTD::swap;
9890b57cec5SDimitry Andric    swap(__a1, __a2);
9900b57cec5SDimitry Andric}
9910b57cec5SDimitry Andric
9920b57cec5SDimitry Andrictemplate <typename _Alloc>
9930eae32dcSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
9940b57cec5SDimitry Andricvoid __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
9950b57cec5SDimitry Andric
996e8d8bef9SDimitry Andrictemplate <typename _Alloc>
9970eae32dcSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
998e8d8bef9SDimitry Andricvoid __swap_allocator(_Alloc & __a1, _Alloc & __a2)
9990eae32dcSDimitry Andric#if _LIBCPP_STD_VER > 11
1000e8d8bef9SDimitry Andric    _NOEXCEPT
1001e8d8bef9SDimitry Andric#else
1002e8d8bef9SDimitry Andric    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
1003e8d8bef9SDimitry Andric#endif
1004e8d8bef9SDimitry Andric{
1005e8d8bef9SDimitry Andric    _VSTD::__swap_allocator(__a1, __a2,
1006fe6060f1SDimitry Andric      integral_constant<bool, allocator_traits<_Alloc>::propagate_on_container_swap::value>());
1007e8d8bef9SDimitry Andric}
1008e8d8bef9SDimitry Andric
10090b57cec5SDimitry Andrictemplate <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
10100b57cec5SDimitry Andricstruct __noexcept_move_assign_container : public integral_constant<bool,
10110b57cec5SDimitry Andric    _Traits::propagate_on_container_move_assignment::value
10120b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
10130b57cec5SDimitry Andric        || _Traits::is_always_equal::value
10140b57cec5SDimitry Andric#else
10150b57cec5SDimitry Andric        && is_nothrow_move_assignable<_Alloc>::value
10160b57cec5SDimitry Andric#endif
10170b57cec5SDimitry Andric    > {};
10180b57cec5SDimitry Andric
10190b57cec5SDimitry Andric
10200b57cec5SDimitry Andrictemplate <class _Tp, class _Alloc>
10210b57cec5SDimitry Andricstruct __temp_value {
10220b57cec5SDimitry Andric    typedef allocator_traits<_Alloc> _Traits;
10230b57cec5SDimitry Andric
10240b57cec5SDimitry Andric    typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
10250b57cec5SDimitry Andric    _Alloc &__a;
10260b57cec5SDimitry Andric
10270b57cec5SDimitry Andric    _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
10280b57cec5SDimitry Andric    _Tp &   get() { return *__addr(); }
10290b57cec5SDimitry Andric
10300b57cec5SDimitry Andric    template<class... _Args>
10310b57cec5SDimitry Andric    _LIBCPP_NO_CFI
10320b57cec5SDimitry Andric    __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
10330b57cec5SDimitry Andric      _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
10340b57cec5SDimitry Andric                         _VSTD::forward<_Args>(__args)...);
10350b57cec5SDimitry Andric    }
10360b57cec5SDimitry Andric
10370b57cec5SDimitry Andric    ~__temp_value() { _Traits::destroy(__a, __addr()); }
10380b57cec5SDimitry Andric    };
10390b57cec5SDimitry Andric
10400b57cec5SDimitry Andrictemplate<typename _Alloc, typename = void, typename = void>
10410b57cec5SDimitry Andricstruct __is_allocator : false_type {};
10420b57cec5SDimitry Andric
10430b57cec5SDimitry Andrictemplate<typename _Alloc>
10440b57cec5SDimitry Andricstruct __is_allocator<_Alloc,
10450b57cec5SDimitry Andric       typename __void_t<typename _Alloc::value_type>::type,
1046fe6060f1SDimitry Andric       typename __void_t<decltype(declval<_Alloc&>().allocate(size_t(0)))>::type
10470b57cec5SDimitry Andric     >
10480b57cec5SDimitry Andric   : true_type {};
10490b57cec5SDimitry Andric
10500b57cec5SDimitry Andric// __builtin_new_allocator -- A non-templated helper for allocating and
10510b57cec5SDimitry Andric// deallocating memory using __builtin_operator_new and
10520b57cec5SDimitry Andric// __builtin_operator_delete. It should be used in preference to
10530b57cec5SDimitry Andric// `std::allocator<T>` to avoid additional instantiations.
10540b57cec5SDimitry Andricstruct __builtin_new_allocator {
10550b57cec5SDimitry Andric  struct __builtin_new_deleter {
10560b57cec5SDimitry Andric    typedef void* pointer_type;
10570b57cec5SDimitry Andric
10580b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
10590b57cec5SDimitry Andric        : __size_(__size), __align_(__align) {}
10600b57cec5SDimitry Andric
10610b57cec5SDimitry Andric    void operator()(void* p) const _NOEXCEPT {
1062e8d8bef9SDimitry Andric        _VSTD::__libcpp_deallocate(p, __size_, __align_);
10630b57cec5SDimitry Andric    }
10640b57cec5SDimitry Andric
10650b57cec5SDimitry Andric   private:
10660b57cec5SDimitry Andric    size_t __size_;
10670b57cec5SDimitry Andric    size_t __align_;
10680b57cec5SDimitry Andric  };
10690b57cec5SDimitry Andric
10700b57cec5SDimitry Andric  typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
10710b57cec5SDimitry Andric
10720b57cec5SDimitry Andric  static __holder_t __allocate_bytes(size_t __s, size_t __align) {
1073e8d8bef9SDimitry Andric      return __holder_t(_VSTD::__libcpp_allocate(__s, __align),
10740b57cec5SDimitry Andric                     __builtin_new_deleter(__s, __align));
10750b57cec5SDimitry Andric  }
10760b57cec5SDimitry Andric
10770b57cec5SDimitry Andric  static void __deallocate_bytes(void* __p, size_t __s,
10780b57cec5SDimitry Andric                                 size_t __align) _NOEXCEPT {
1079e8d8bef9SDimitry Andric      _VSTD::__libcpp_deallocate(__p, __s, __align);
10800b57cec5SDimitry Andric  }
10810b57cec5SDimitry Andric
10820b57cec5SDimitry Andric  template <class _Tp>
10830b57cec5SDimitry Andric  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
10840b57cec5SDimitry Andric  static __holder_t __allocate_type(size_t __n) {
10850b57cec5SDimitry Andric      return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
10860b57cec5SDimitry Andric  }
10870b57cec5SDimitry Andric
10880b57cec5SDimitry Andric  template <class _Tp>
10890b57cec5SDimitry Andric  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
10900b57cec5SDimitry Andric  static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
10910b57cec5SDimitry Andric      __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
10920b57cec5SDimitry Andric  }
10930b57cec5SDimitry Andric};
10940b57cec5SDimitry Andric
10950b57cec5SDimitry Andric
10960b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
10970b57cec5SDimitry Andric
1098e40139ffSDimitry Andric#if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
1099e40139ffSDimitry Andric#   include <__pstl_memory>
1100e40139ffSDimitry Andric#endif
1101e40139ffSDimitry Andric
11020b57cec5SDimitry Andric#endif // _LIBCPP_MEMORY
1103