xref: /llvm-project-15.0.7/libcxx/include/memory (revision fb2bd4a9)
13e519524SHoward Hinnant// -*- C++ -*-
23e519524SHoward Hinnant//===-------------------------- memory ------------------------------------===//
33e519524SHoward Hinnant//
457b08b09SChandler Carruth// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
557b08b09SChandler Carruth// See https://llvm.org/LICENSE.txt for license information.
657b08b09SChandler Carruth// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
73e519524SHoward Hinnant//
83e519524SHoward Hinnant//===----------------------------------------------------------------------===//
93e519524SHoward Hinnant
103e519524SHoward Hinnant#ifndef _LIBCPP_MEMORY
113e519524SHoward Hinnant#define _LIBCPP_MEMORY
123e519524SHoward Hinnant
133e519524SHoward Hinnant/*
143e519524SHoward Hinnant    memory synopsis
153e519524SHoward Hinnant
163e519524SHoward Hinnantnamespace std
173e519524SHoward Hinnant{
183e519524SHoward Hinnant
193e519524SHoward Hinnantstruct allocator_arg_t { };
2040a01d53SMarshall Clowinline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
213e519524SHoward Hinnant
223e519524SHoward Hinnanttemplate <class T, class Alloc> struct uses_allocator;
233e519524SHoward Hinnant
243e519524SHoward Hinnanttemplate <class Ptr>
253e519524SHoward Hinnantstruct pointer_traits
263e519524SHoward Hinnant{
273e519524SHoward Hinnant    typedef Ptr pointer;
283e519524SHoward Hinnant    typedef <details> element_type;
293e519524SHoward Hinnant    typedef <details> difference_type;
303e519524SHoward Hinnant
313e519524SHoward Hinnant    template <class U> using rebind = <details>;
323e519524SHoward Hinnant
333e519524SHoward Hinnant    static pointer pointer_to(<details>);
343e519524SHoward Hinnant};
353e519524SHoward Hinnant
363739fe79SHoward Hinnanttemplate <class T>
373739fe79SHoward Hinnantstruct pointer_traits<T*>
383739fe79SHoward Hinnant{
393739fe79SHoward Hinnant    typedef T* pointer;
403739fe79SHoward Hinnant    typedef T element_type;
413739fe79SHoward Hinnant    typedef ptrdiff_t difference_type;
423739fe79SHoward Hinnant
433739fe79SHoward Hinnant    template <class U> using rebind = U*;
443739fe79SHoward Hinnant
4572720269SLouis Dionne    static pointer pointer_to(<details>) noexcept; // constexpr in C++20
463739fe79SHoward Hinnant};
473739fe79SHoward Hinnant
4832952747SEric Fiseliertemplate <class T> constexpr T* to_address(T* p) noexcept; // C++20
4932952747SEric Fiseliertemplate <class Ptr> auto to_address(const Ptr& p) noexcept; // C++20
5032952747SEric Fiselier
513e519524SHoward Hinnanttemplate <class Alloc>
523e519524SHoward Hinnantstruct allocator_traits
533e519524SHoward Hinnant{
543e519524SHoward Hinnant    typedef Alloc                        allocator_type;
553e519524SHoward Hinnant    typedef typename allocator_type::value_type
563e519524SHoward Hinnant                                         value_type;
573e519524SHoward Hinnant
583e519524SHoward Hinnant    typedef Alloc::pointer | value_type* pointer;
593e519524SHoward Hinnant    typedef Alloc::const_pointer
603e519524SHoward Hinnant          | pointer_traits<pointer>::rebind<const value_type>
613e519524SHoward Hinnant                                         const_pointer;
623e519524SHoward Hinnant    typedef Alloc::void_pointer
633e519524SHoward Hinnant          | pointer_traits<pointer>::rebind<void>
643e519524SHoward Hinnant                                         void_pointer;
653e519524SHoward Hinnant    typedef Alloc::const_void_pointer
663e519524SHoward Hinnant          | pointer_traits<pointer>::rebind<const void>
673e519524SHoward Hinnant                                         const_void_pointer;
683e519524SHoward Hinnant    typedef Alloc::difference_type
69a4a1ef1fSHoward Hinnant          | pointer_traits<pointer>::difference_type
70a4a1ef1fSHoward Hinnant                                         difference_type;
71a4a1ef1fSHoward Hinnant    typedef Alloc::size_type
72a4a1ef1fSHoward Hinnant          | make_unsigned<difference_type>::type
73a4a1ef1fSHoward Hinnant                                         size_type;
743e519524SHoward Hinnant    typedef Alloc::propagate_on_container_copy_assignment
753e519524SHoward Hinnant          | false_type                   propagate_on_container_copy_assignment;
763e519524SHoward Hinnant    typedef Alloc::propagate_on_container_move_assignment
773e519524SHoward Hinnant          | false_type                   propagate_on_container_move_assignment;
783e519524SHoward Hinnant    typedef Alloc::propagate_on_container_swap
793e519524SHoward Hinnant          | false_type                   propagate_on_container_swap;
8031a47313SMarshall Clow    typedef Alloc::is_always_equal
8131a47313SMarshall Clow          | is_empty                     is_always_equal;
823e519524SHoward Hinnant
833e519524SHoward Hinnant    template <class T> using rebind_alloc  = Alloc::rebind<U>::other | Alloc<T, Args...>;
843e519524SHoward Hinnant    template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
853e519524SHoward Hinnant
863fddff51SMarshall Clow    static pointer allocate(allocator_type& a, size_type n);                          // [[nodiscard]] in C++20
873fddff51SMarshall Clow    static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // [[nodiscard]] in C++20
883e519524SHoward Hinnant
893739fe79SHoward Hinnant    static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
903e519524SHoward Hinnant
913e519524SHoward Hinnant    template <class T, class... Args>
923e519524SHoward Hinnant        static void construct(allocator_type& a, T* p, Args&&... args);
933e519524SHoward Hinnant
943e519524SHoward Hinnant    template <class T>
953e519524SHoward Hinnant        static void destroy(allocator_type& a, T* p);
963e519524SHoward Hinnant
9706fbed03SMarshall Clow    static size_type max_size(const allocator_type& a); // noexcept in C++14
983e519524SHoward Hinnant
993e519524SHoward Hinnant    static allocator_type
1003e519524SHoward Hinnant        select_on_container_copy_construction(const allocator_type& a);
1013e519524SHoward Hinnant};
1023e519524SHoward Hinnant
1033e519524SHoward Hinnanttemplate <>
1043e519524SHoward Hinnantclass allocator<void>
1053e519524SHoward Hinnant{
1063e519524SHoward Hinnantpublic:
1073e519524SHoward Hinnant    typedef void*                                 pointer;
1083e519524SHoward Hinnant    typedef const void*                           const_pointer;
1093e519524SHoward Hinnant    typedef void                                  value_type;
1103e519524SHoward Hinnant
1113e519524SHoward Hinnant    template <class _Up> struct rebind {typedef allocator<_Up> other;};
1123e519524SHoward Hinnant};
1133e519524SHoward Hinnant
1143e519524SHoward Hinnanttemplate <class T>
1153e519524SHoward Hinnantclass allocator
1163e519524SHoward Hinnant{
1173e519524SHoward Hinnantpublic:
1183e519524SHoward Hinnant    typedef size_t                                size_type;
1193e519524SHoward Hinnant    typedef ptrdiff_t                             difference_type;
1203e519524SHoward Hinnant    typedef T*                                    pointer;
1213e519524SHoward Hinnant    typedef const T*                              const_pointer;
1223e519524SHoward Hinnant    typedef typename add_lvalue_reference<T>::type       reference;
1233e519524SHoward Hinnant    typedef typename add_lvalue_reference<const T>::type const_reference;
1243e519524SHoward Hinnant    typedef T                                     value_type;
1253e519524SHoward Hinnant
1263e519524SHoward Hinnant    template <class U> struct rebind {typedef allocator<U> other;};
1273e519524SHoward Hinnant
128e0742c4aSMarshall Clow    constexpr allocator() noexcept;                      // constexpr in C++20
129e0742c4aSMarshall Clow    constexpr allocator(const allocator&) noexcept;      // constexpr in C++20
130e0742c4aSMarshall Clow    template <class U>
131e0742c4aSMarshall Clow      constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
1323739fe79SHoward Hinnant    ~allocator();
1333739fe79SHoward Hinnant    pointer address(reference x) const noexcept;
1343739fe79SHoward Hinnant    const_pointer address(const_reference x) const noexcept;
1353e519524SHoward Hinnant    pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
1363739fe79SHoward Hinnant    void deallocate(pointer p, size_type n) noexcept;
1373739fe79SHoward Hinnant    size_type max_size() const noexcept;
1383739fe79SHoward Hinnant    template<class U, class... Args>
1393739fe79SHoward Hinnant        void construct(U* p, Args&&... args);
1403739fe79SHoward Hinnant    template <class U>
1413739fe79SHoward Hinnant        void destroy(U* p);
1423e519524SHoward Hinnant};
1433e519524SHoward Hinnant
1443e519524SHoward Hinnanttemplate <class T, class U>
1453739fe79SHoward Hinnantbool operator==(const allocator<T>&, const allocator<U>&) noexcept;
1463e519524SHoward Hinnant
1473e519524SHoward Hinnanttemplate <class T, class U>
1483739fe79SHoward Hinnantbool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
1493e519524SHoward Hinnant
1503e519524SHoward Hinnanttemplate <class OutputIterator, class T>
1513e519524SHoward Hinnantclass raw_storage_iterator
1523e519524SHoward Hinnant    : public iterator<output_iterator_tag,
1533e519524SHoward Hinnant                      T,                               // purposefully not C++03
1543e519524SHoward Hinnant                      ptrdiff_t,                       // purposefully not C++03
1553e519524SHoward Hinnant                      T*,                              // purposefully not C++03
1563e519524SHoward Hinnant                      raw_storage_iterator&>           // purposefully not C++03
1573e519524SHoward Hinnant{
1583e519524SHoward Hinnantpublic:
1593e519524SHoward Hinnant    explicit raw_storage_iterator(OutputIterator x);
1603e519524SHoward Hinnant    raw_storage_iterator& operator*();
1613e519524SHoward Hinnant    raw_storage_iterator& operator=(const T& element);
1623e519524SHoward Hinnant    raw_storage_iterator& operator++();
1633e519524SHoward Hinnant    raw_storage_iterator  operator++(int);
1643e519524SHoward Hinnant};
1653e519524SHoward Hinnant
1663739fe79SHoward Hinnanttemplate <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
1673739fe79SHoward Hinnanttemplate <class T> void               return_temporary_buffer(T* p) noexcept;
1683739fe79SHoward Hinnant
1693739fe79SHoward Hinnanttemplate <class T> T* addressof(T& r) noexcept;
1701c7fe126SMarshall Clowtemplate <class T> T* addressof(const T&& r) noexcept = delete;
1713e519524SHoward Hinnant
1723e519524SHoward Hinnanttemplate <class InputIterator, class ForwardIterator>
1733e519524SHoward HinnantForwardIterator
1743e519524SHoward Hinnantuninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
1753e519524SHoward Hinnant
1763739fe79SHoward Hinnanttemplate <class InputIterator, class Size, class ForwardIterator>
1773739fe79SHoward HinnantForwardIterator
1783739fe79SHoward Hinnantuninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
1793739fe79SHoward Hinnant
1803e519524SHoward Hinnanttemplate <class ForwardIterator, class T>
1813e519524SHoward Hinnantvoid uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
1823e519524SHoward Hinnant
1833e519524SHoward Hinnanttemplate <class ForwardIterator, class Size, class T>
18448d05bd2SHoward HinnantForwardIterator
18548d05bd2SHoward Hinnantuninitialized_fill_n(ForwardIterator first, Size n, const T& x);
1863e519524SHoward Hinnant
187e4d9c316SEric Fiseliertemplate <class T>
188e4d9c316SEric Fiseliervoid destroy_at(T* location);
189e4d9c316SEric Fiselier
190e4d9c316SEric Fiseliertemplate <class ForwardIterator>
191e4d9c316SEric Fiselier void destroy(ForwardIterator first, ForwardIterator last);
192e4d9c316SEric Fiselier
193e4d9c316SEric Fiseliertemplate <class ForwardIterator, class Size>
194e4d9c316SEric Fiselier ForwardIterator destroy_n(ForwardIterator first, Size n);
195e4d9c316SEric Fiselier
196e4d9c316SEric Fiseliertemplate <class InputIterator, class ForwardIterator>
197e4d9c316SEric Fiselier ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
198e4d9c316SEric Fiselier
199e4d9c316SEric Fiseliertemplate <class InputIterator, class Size, class ForwardIterator>
200e4d9c316SEric Fiselier pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
201e4d9c316SEric Fiselier
202e4d9c316SEric Fiseliertemplate <class ForwardIterator>
203e4d9c316SEric Fiselier void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
204e4d9c316SEric Fiselier
205e4d9c316SEric Fiseliertemplate <class ForwardIterator, class Size>
206e4d9c316SEric Fiselier ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
207e4d9c316SEric Fiselier
208e4d9c316SEric Fiseliertemplate <class ForwardIterator>
209e4d9c316SEric Fiselier void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
210e4d9c316SEric Fiselier
211e4d9c316SEric Fiseliertemplate <class ForwardIterator, class Size>
212e4d9c316SEric Fiselier ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
213e4d9c316SEric Fiselier
214ea5cd3b4SLouis Dionnetemplate <class Y> struct auto_ptr_ref {};      // deprecated in C++11, removed in C++17
2153e519524SHoward Hinnant
2163e519524SHoward Hinnanttemplate<class X>
217ea5cd3b4SLouis Dionneclass auto_ptr                                  // deprecated in C++11, removed in C++17
2183e519524SHoward Hinnant{
2193e519524SHoward Hinnantpublic:
2203e519524SHoward Hinnant    typedef X element_type;
2213e519524SHoward Hinnant
2223e519524SHoward Hinnant    explicit auto_ptr(X* p =0) throw();
2233e519524SHoward Hinnant    auto_ptr(auto_ptr&) throw();
2243e519524SHoward Hinnant    template<class Y> auto_ptr(auto_ptr<Y>&) throw();
2253e519524SHoward Hinnant    auto_ptr& operator=(auto_ptr&) throw();
2263e519524SHoward Hinnant    template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
2273e519524SHoward Hinnant    auto_ptr& operator=(auto_ptr_ref<X> r) throw();
2283e519524SHoward Hinnant    ~auto_ptr() throw();
2293e519524SHoward Hinnant
2303e519524SHoward Hinnant    typename add_lvalue_reference<X>::type operator*() const throw();
2313e519524SHoward Hinnant    X* operator->() const throw();
2323e519524SHoward Hinnant    X* get() const throw();
2333e519524SHoward Hinnant    X* release() throw();
2343e519524SHoward Hinnant    void reset(X* p =0) throw();
2353e519524SHoward Hinnant
2363e519524SHoward Hinnant    auto_ptr(auto_ptr_ref<X>) throw();
2373e519524SHoward Hinnant    template<class Y> operator auto_ptr_ref<Y>() throw();
2383e519524SHoward Hinnant    template<class Y> operator auto_ptr<Y>() throw();
2393e519524SHoward Hinnant};
2403e519524SHoward Hinnant
24120cc2a42SHoward Hinnanttemplate <class T>
24220cc2a42SHoward Hinnantstruct default_delete
24320cc2a42SHoward Hinnant{
2443739fe79SHoward Hinnant    constexpr default_delete() noexcept = default;
2453739fe79SHoward Hinnant    template <class U> default_delete(const default_delete<U>&) noexcept;
24620cc2a42SHoward Hinnant
2473739fe79SHoward Hinnant    void operator()(T*) const noexcept;
24820cc2a42SHoward Hinnant};
24920cc2a42SHoward Hinnant
25020cc2a42SHoward Hinnanttemplate <class T>
25120cc2a42SHoward Hinnantstruct default_delete<T[]>
25220cc2a42SHoward Hinnant{
2533739fe79SHoward Hinnant    constexpr default_delete() noexcept = default;
2543739fe79SHoward Hinnant    void operator()(T*) const noexcept;
25520cc2a42SHoward Hinnant    template <class U> void operator()(U*) const = delete;
25620cc2a42SHoward Hinnant};
25720cc2a42SHoward Hinnant
25820cc2a42SHoward Hinnanttemplate <class T, class D = default_delete<T>>
25920cc2a42SHoward Hinnantclass unique_ptr
26020cc2a42SHoward Hinnant{
26120cc2a42SHoward Hinnantpublic:
26220cc2a42SHoward Hinnant    typedef see below pointer;
26320cc2a42SHoward Hinnant    typedef T element_type;
26420cc2a42SHoward Hinnant    typedef D deleter_type;
26520cc2a42SHoward Hinnant
26620cc2a42SHoward Hinnant    // constructors
2673739fe79SHoward Hinnant    constexpr unique_ptr() noexcept;
2683739fe79SHoward Hinnant    explicit unique_ptr(pointer p) noexcept;
2693739fe79SHoward Hinnant    unique_ptr(pointer p, see below d1) noexcept;
2703739fe79SHoward Hinnant    unique_ptr(pointer p, see below d2) noexcept;
2713739fe79SHoward Hinnant    unique_ptr(unique_ptr&& u) noexcept;
2723739fe79SHoward Hinnant    unique_ptr(nullptr_t) noexcept : unique_ptr() { }
27320cc2a42SHoward Hinnant    template <class U, class E>
2743739fe79SHoward Hinnant        unique_ptr(unique_ptr<U, E>&& u) noexcept;
27520cc2a42SHoward Hinnant    template <class U>
276e67179bcSMarshall Clow        unique_ptr(auto_ptr<U>&& u) noexcept;       // removed in C++17
27720cc2a42SHoward Hinnant
27820cc2a42SHoward Hinnant    // destructor
27920cc2a42SHoward Hinnant    ~unique_ptr();
28020cc2a42SHoward Hinnant
28120cc2a42SHoward Hinnant    // assignment
2823739fe79SHoward Hinnant    unique_ptr& operator=(unique_ptr&& u) noexcept;
2833739fe79SHoward Hinnant    template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
2843739fe79SHoward Hinnant    unique_ptr& operator=(nullptr_t) noexcept;
28520cc2a42SHoward Hinnant
28620cc2a42SHoward Hinnant    // observers
28720cc2a42SHoward Hinnant    typename add_lvalue_reference<T>::type operator*() const;
2883739fe79SHoward Hinnant    pointer operator->() const noexcept;
2893739fe79SHoward Hinnant    pointer get() const noexcept;
2903739fe79SHoward Hinnant    deleter_type& get_deleter() noexcept;
2913739fe79SHoward Hinnant    const deleter_type& get_deleter() const noexcept;
2923739fe79SHoward Hinnant    explicit operator bool() const noexcept;
29320cc2a42SHoward Hinnant
29420cc2a42SHoward Hinnant    // modifiers
2953739fe79SHoward Hinnant    pointer release() noexcept;
2963739fe79SHoward Hinnant    void reset(pointer p = pointer()) noexcept;
2973739fe79SHoward Hinnant    void swap(unique_ptr& u) noexcept;
29820cc2a42SHoward Hinnant};
29920cc2a42SHoward Hinnant
30020cc2a42SHoward Hinnanttemplate <class T, class D>
30120cc2a42SHoward Hinnantclass unique_ptr<T[], D>
30220cc2a42SHoward Hinnant{
30320cc2a42SHoward Hinnantpublic:
30420cc2a42SHoward Hinnant    typedef implementation-defined pointer;
30520cc2a42SHoward Hinnant    typedef T element_type;
30620cc2a42SHoward Hinnant    typedef D deleter_type;
30720cc2a42SHoward Hinnant
30820cc2a42SHoward Hinnant    // constructors
3093739fe79SHoward Hinnant    constexpr unique_ptr() noexcept;
3103739fe79SHoward Hinnant    explicit unique_ptr(pointer p) noexcept;
3113739fe79SHoward Hinnant    unique_ptr(pointer p, see below d) noexcept;
3123739fe79SHoward Hinnant    unique_ptr(pointer p, see below d) noexcept;
3133739fe79SHoward Hinnant    unique_ptr(unique_ptr&& u) noexcept;
3143739fe79SHoward Hinnant    unique_ptr(nullptr_t) noexcept : unique_ptr() { }
31520cc2a42SHoward Hinnant
31620cc2a42SHoward Hinnant    // destructor
317b3371f6fSHoward Hinnant    ~unique_ptr();
31820cc2a42SHoward Hinnant
31920cc2a42SHoward Hinnant    // assignment
3203739fe79SHoward Hinnant    unique_ptr& operator=(unique_ptr&& u) noexcept;
3213739fe79SHoward Hinnant    unique_ptr& operator=(nullptr_t) noexcept;
32220cc2a42SHoward Hinnant
32320cc2a42SHoward Hinnant    // observers
32420cc2a42SHoward Hinnant    T& operator[](size_t i) const;
3253739fe79SHoward Hinnant    pointer get() const noexcept;
3263739fe79SHoward Hinnant    deleter_type& get_deleter() noexcept;
3273739fe79SHoward Hinnant    const deleter_type& get_deleter() const noexcept;
3283739fe79SHoward Hinnant    explicit operator bool() const noexcept;
32920cc2a42SHoward Hinnant
33020cc2a42SHoward Hinnant    // modifiers
3313739fe79SHoward Hinnant    pointer release() noexcept;
3323739fe79SHoward Hinnant    void reset(pointer p = pointer()) noexcept;
3333739fe79SHoward Hinnant    void reset(nullptr_t) noexcept;
33420cc2a42SHoward Hinnant    template <class U> void reset(U) = delete;
3353739fe79SHoward Hinnant    void swap(unique_ptr& u) noexcept;
33620cc2a42SHoward Hinnant};
33720cc2a42SHoward Hinnant
33820cc2a42SHoward Hinnanttemplate <class T, class D>
3393739fe79SHoward Hinnant    void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
34020cc2a42SHoward Hinnant
34120cc2a42SHoward Hinnanttemplate <class T1, class D1, class T2, class D2>
34220cc2a42SHoward Hinnant    bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
34320cc2a42SHoward Hinnanttemplate <class T1, class D1, class T2, class D2>
34420cc2a42SHoward Hinnant    bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
34520cc2a42SHoward Hinnanttemplate <class T1, class D1, class T2, class D2>
34620cc2a42SHoward Hinnant    bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
34720cc2a42SHoward Hinnanttemplate <class T1, class D1, class T2, class D2>
34820cc2a42SHoward Hinnant    bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
34920cc2a42SHoward Hinnanttemplate <class T1, class D1, class T2, class D2>
35020cc2a42SHoward Hinnant    bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
35120cc2a42SHoward Hinnanttemplate <class T1, class D1, class T2, class D2>
35220cc2a42SHoward Hinnant    bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
35320cc2a42SHoward Hinnant
3543739fe79SHoward Hinnanttemplate <class T, class D>
3553739fe79SHoward Hinnant    bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
3563739fe79SHoward Hinnanttemplate <class T, class D>
3573739fe79SHoward Hinnant    bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
3583739fe79SHoward Hinnanttemplate <class T, class D>
3593739fe79SHoward Hinnant    bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
3603739fe79SHoward Hinnanttemplate <class T, class D>
3613739fe79SHoward Hinnant    bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
3623739fe79SHoward Hinnant
3633739fe79SHoward Hinnanttemplate <class T, class D>
3643739fe79SHoward Hinnant    bool operator<(const unique_ptr<T, D>& x, nullptr_t);
3653739fe79SHoward Hinnanttemplate <class T, class D>
3663739fe79SHoward Hinnant    bool operator<(nullptr_t, const unique_ptr<T, D>& y);
3673739fe79SHoward Hinnanttemplate <class T, class D>
3683739fe79SHoward Hinnant    bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
3693739fe79SHoward Hinnanttemplate <class T, class D>
3703739fe79SHoward Hinnant    bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
3713739fe79SHoward Hinnanttemplate <class T, class D>
3723739fe79SHoward Hinnant    bool operator>(const unique_ptr<T, D>& x, nullptr_t);
3733739fe79SHoward Hinnanttemplate <class T, class D>
3743739fe79SHoward Hinnant    bool operator>(nullptr_t, const unique_ptr<T, D>& y);
3753739fe79SHoward Hinnanttemplate <class T, class D>
3763739fe79SHoward Hinnant    bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
3773739fe79SHoward Hinnanttemplate <class T, class D>
3783739fe79SHoward Hinnant    bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
3793739fe79SHoward Hinnant
38020cc2a42SHoward Hinnantclass bad_weak_ptr
38120cc2a42SHoward Hinnant    : public std::exception
38220cc2a42SHoward Hinnant{
3833739fe79SHoward Hinnant    bad_weak_ptr() noexcept;
38420cc2a42SHoward Hinnant};
38520cc2a42SHoward Hinnant
38628d8ba5fSMarshall Clowtemplate<class T, class... Args> unique_ptr<T> make_unique(Args&&... args);     // C++14
38728d8ba5fSMarshall Clowtemplate<class T>                unique_ptr<T> make_unique(size_t n);           // C++14
38828d8ba5fSMarshall Clowtemplate<class T, class... Args> unspecified   make_unique(Args&&...) = delete; // C++14, T == U[N]
38928d8ba5fSMarshall Clow
39048f36539SMarshall Clowtemplate<class E, class T, class Y, class D>
39148f36539SMarshall Clow    basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
39248f36539SMarshall Clow
39320cc2a42SHoward Hinnanttemplate<class T>
39420cc2a42SHoward Hinnantclass shared_ptr
39520cc2a42SHoward Hinnant{
39620cc2a42SHoward Hinnantpublic:
39720cc2a42SHoward Hinnant    typedef T element_type;
39868436a9bSEric Fiselier    typedef weak_ptr<T> weak_type; // C++17
39920cc2a42SHoward Hinnant
40020cc2a42SHoward Hinnant    // constructors:
4013739fe79SHoward Hinnant    constexpr shared_ptr() noexcept;
40220cc2a42SHoward Hinnant    template<class Y> explicit shared_ptr(Y* p);
40320cc2a42SHoward Hinnant    template<class Y, class D> shared_ptr(Y* p, D d);
40420cc2a42SHoward Hinnant    template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
40520cc2a42SHoward Hinnant    template <class D> shared_ptr(nullptr_t p, D d);
40620cc2a42SHoward Hinnant    template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
4073739fe79SHoward Hinnant    template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
4083739fe79SHoward Hinnant    shared_ptr(const shared_ptr& r) noexcept;
4093739fe79SHoward Hinnant    template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
4103739fe79SHoward Hinnant    shared_ptr(shared_ptr&& r) noexcept;
4113739fe79SHoward Hinnant    template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
41220cc2a42SHoward Hinnant    template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
413e67179bcSMarshall Clow    template<class Y> shared_ptr(auto_ptr<Y>&& r);          // removed in C++17
41420cc2a42SHoward Hinnant    template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
41520cc2a42SHoward Hinnant    shared_ptr(nullptr_t) : shared_ptr() { }
41620cc2a42SHoward Hinnant
41720cc2a42SHoward Hinnant    // destructor:
41820cc2a42SHoward Hinnant    ~shared_ptr();
41920cc2a42SHoward Hinnant
42020cc2a42SHoward Hinnant    // assignment:
4213739fe79SHoward Hinnant    shared_ptr& operator=(const shared_ptr& r) noexcept;
4223739fe79SHoward Hinnant    template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
4233739fe79SHoward Hinnant    shared_ptr& operator=(shared_ptr&& r) noexcept;
42420cc2a42SHoward Hinnant    template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
425e67179bcSMarshall Clow    template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
42620cc2a42SHoward Hinnant    template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
42720cc2a42SHoward Hinnant
42820cc2a42SHoward Hinnant    // modifiers:
4293739fe79SHoward Hinnant    void swap(shared_ptr& r) noexcept;
4303739fe79SHoward Hinnant    void reset() noexcept;
43120cc2a42SHoward Hinnant    template<class Y> void reset(Y* p);
43220cc2a42SHoward Hinnant    template<class Y, class D> void reset(Y* p, D d);
43320cc2a42SHoward Hinnant    template<class Y, class D, class A> void reset(Y* p, D d, A a);
43420cc2a42SHoward Hinnant
4353739fe79SHoward Hinnant    // observers:
4363739fe79SHoward Hinnant    T* get() const noexcept;
4373739fe79SHoward Hinnant    T& operator*() const noexcept;
4383739fe79SHoward Hinnant    T* operator->() const noexcept;
4393739fe79SHoward Hinnant    long use_count() const noexcept;
4403739fe79SHoward Hinnant    bool unique() const noexcept;
4413739fe79SHoward Hinnant    explicit operator bool() const noexcept;
44236bc7178SMarshall Clow    template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
44336bc7178SMarshall Clow    template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
44420cc2a42SHoward Hinnant};
44520cc2a42SHoward Hinnant
44620cc2a42SHoward Hinnant// shared_ptr comparisons:
44720cc2a42SHoward Hinnanttemplate<class T, class U>
4483739fe79SHoward Hinnant    bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
44920cc2a42SHoward Hinnanttemplate<class T, class U>
4503739fe79SHoward Hinnant    bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
45120cc2a42SHoward Hinnanttemplate<class T, class U>
4523739fe79SHoward Hinnant    bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
45320cc2a42SHoward Hinnanttemplate<class T, class U>
4543739fe79SHoward Hinnant    bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
45520cc2a42SHoward Hinnanttemplate<class T, class U>
4563739fe79SHoward Hinnant    bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
45720cc2a42SHoward Hinnanttemplate<class T, class U>
4583739fe79SHoward Hinnant    bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
4593739fe79SHoward Hinnant
4603739fe79SHoward Hinnanttemplate <class T>
4613739fe79SHoward Hinnant    bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
4623739fe79SHoward Hinnanttemplate <class T>
4633739fe79SHoward Hinnant    bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
4643739fe79SHoward Hinnanttemplate <class T>
4653739fe79SHoward Hinnant    bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
4663739fe79SHoward Hinnanttemplate <class T>
4673739fe79SHoward Hinnant    bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
4683739fe79SHoward Hinnanttemplate <class T>
4693739fe79SHoward Hinnant    bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
4703739fe79SHoward Hinnanttemplate <class T>
4713739fe79SHoward Hinnantbool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
4723739fe79SHoward Hinnanttemplate <class T>
4733739fe79SHoward Hinnant    bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
4743739fe79SHoward Hinnanttemplate <class T>
4753739fe79SHoward Hinnant    bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
4763739fe79SHoward Hinnanttemplate <class T>
4773739fe79SHoward Hinnant    bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
4783739fe79SHoward Hinnanttemplate <class T>
4793739fe79SHoward Hinnant    bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
4803739fe79SHoward Hinnanttemplate <class T>
4813739fe79SHoward Hinnant    bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
4823739fe79SHoward Hinnanttemplate <class T>
4833739fe79SHoward Hinnant    bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
48420cc2a42SHoward Hinnant
48520cc2a42SHoward Hinnant// shared_ptr specialized algorithms:
4863739fe79SHoward Hinnanttemplate<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
48720cc2a42SHoward Hinnant
48820cc2a42SHoward Hinnant// shared_ptr casts:
48920cc2a42SHoward Hinnanttemplate<class T, class U>
4903739fe79SHoward Hinnant    shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
49120cc2a42SHoward Hinnanttemplate<class T, class U>
4923739fe79SHoward Hinnant    shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
49320cc2a42SHoward Hinnanttemplate<class T, class U>
4943739fe79SHoward Hinnant    shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
49520cc2a42SHoward Hinnant
49620cc2a42SHoward Hinnant// shared_ptr I/O:
49720cc2a42SHoward Hinnanttemplate<class E, class T, class Y>
49820cc2a42SHoward Hinnant    basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
49920cc2a42SHoward Hinnant
50020cc2a42SHoward Hinnant// shared_ptr get_deleter:
5013739fe79SHoward Hinnanttemplate<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
50220cc2a42SHoward Hinnant
50320cc2a42SHoward Hinnanttemplate<class T, class... Args>
50420cc2a42SHoward Hinnant    shared_ptr<T> make_shared(Args&&... args);
50520cc2a42SHoward Hinnanttemplate<class T, class A, class... Args>
50620cc2a42SHoward Hinnant    shared_ptr<T> allocate_shared(const A& a, Args&&... args);
50720cc2a42SHoward Hinnant
50820cc2a42SHoward Hinnanttemplate<class T>
50920cc2a42SHoward Hinnantclass weak_ptr
51020cc2a42SHoward Hinnant{
51120cc2a42SHoward Hinnantpublic:
51220cc2a42SHoward Hinnant    typedef T element_type;
51320cc2a42SHoward Hinnant
51420cc2a42SHoward Hinnant    // constructors
5153739fe79SHoward Hinnant    constexpr weak_ptr() noexcept;
5163739fe79SHoward Hinnant    template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
5173739fe79SHoward Hinnant    weak_ptr(weak_ptr const& r) noexcept;
5183739fe79SHoward Hinnant    template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
5194703f763SMarshall Clow    weak_ptr(weak_ptr&& r) noexcept;                      // C++14
5204703f763SMarshall Clow    template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
52120cc2a42SHoward Hinnant
52220cc2a42SHoward Hinnant    // destructor
52320cc2a42SHoward Hinnant    ~weak_ptr();
52420cc2a42SHoward Hinnant
52520cc2a42SHoward Hinnant    // assignment
5263739fe79SHoward Hinnant    weak_ptr& operator=(weak_ptr const& r) noexcept;
5273739fe79SHoward Hinnant    template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
5283739fe79SHoward Hinnant    template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
5294703f763SMarshall Clow    weak_ptr& operator=(weak_ptr&& r) noexcept;                      // C++14
5304703f763SMarshall Clow    template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
53120cc2a42SHoward Hinnant
53220cc2a42SHoward Hinnant    // modifiers
5333739fe79SHoward Hinnant    void swap(weak_ptr& r) noexcept;
5343739fe79SHoward Hinnant    void reset() noexcept;
53520cc2a42SHoward Hinnant
53620cc2a42SHoward Hinnant    // observers
5373739fe79SHoward Hinnant    long use_count() const noexcept;
5383739fe79SHoward Hinnant    bool expired() const noexcept;
5393739fe79SHoward Hinnant    shared_ptr<T> lock() const noexcept;
54036bc7178SMarshall Clow    template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
54136bc7178SMarshall Clow    template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
54220cc2a42SHoward Hinnant};
54320cc2a42SHoward Hinnant
54420cc2a42SHoward Hinnant// weak_ptr specialized algorithms:
5453739fe79SHoward Hinnanttemplate<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
54620cc2a42SHoward Hinnant
54720cc2a42SHoward Hinnant// class owner_less:
54820cc2a42SHoward Hinnanttemplate<class T> struct owner_less;
54920cc2a42SHoward Hinnant
55020cc2a42SHoward Hinnanttemplate<class T>
55120cc2a42SHoward Hinnantstruct owner_less<shared_ptr<T> >
55220cc2a42SHoward Hinnant    : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
55320cc2a42SHoward Hinnant{
55420cc2a42SHoward Hinnant    typedef bool result_type;
55536bc7178SMarshall Clow    bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
55636bc7178SMarshall Clow    bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
55736bc7178SMarshall Clow    bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
55820cc2a42SHoward Hinnant};
55920cc2a42SHoward Hinnant
56020cc2a42SHoward Hinnanttemplate<class T>
56120cc2a42SHoward Hinnantstruct owner_less<weak_ptr<T> >
56220cc2a42SHoward Hinnant    : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
56320cc2a42SHoward Hinnant{
56420cc2a42SHoward Hinnant    typedef bool result_type;
56536bc7178SMarshall Clow    bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
56636bc7178SMarshall Clow    bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
56736bc7178SMarshall Clow    bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
56836bc7178SMarshall Clow};
56936bc7178SMarshall Clow
57036bc7178SMarshall Clowtemplate <>  // Added in C++14
57136bc7178SMarshall Clowstruct owner_less<void>
57236bc7178SMarshall Clow{
57336bc7178SMarshall Clow    template <class _Tp, class _Up>
57436bc7178SMarshall Clow    bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
57536bc7178SMarshall Clow    template <class _Tp, class _Up>
57636bc7178SMarshall Clow    bool operator()( shared_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const noexcept;
57736bc7178SMarshall Clow    template <class _Tp, class _Up>
57836bc7178SMarshall Clow    bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
57936bc7178SMarshall Clow    template <class _Tp, class _Up>
58036bc7178SMarshall Clow    bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const noexcept;
58136bc7178SMarshall Clow
58236bc7178SMarshall Clow    typedef void is_transparent;
58320cc2a42SHoward Hinnant};
58420cc2a42SHoward Hinnant
58520cc2a42SHoward Hinnanttemplate<class T>
58620cc2a42SHoward Hinnantclass enable_shared_from_this
58720cc2a42SHoward Hinnant{
58820cc2a42SHoward Hinnantprotected:
5893739fe79SHoward Hinnant    constexpr enable_shared_from_this() noexcept;
5903739fe79SHoward Hinnant    enable_shared_from_this(enable_shared_from_this const&) noexcept;
5913739fe79SHoward Hinnant    enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
59220cc2a42SHoward Hinnant    ~enable_shared_from_this();
59320cc2a42SHoward Hinnantpublic:
59420cc2a42SHoward Hinnant    shared_ptr<T> shared_from_this();
59520cc2a42SHoward Hinnant    shared_ptr<T const> shared_from_this() const;
59620cc2a42SHoward Hinnant};
59720cc2a42SHoward Hinnant
59820cc2a42SHoward Hinnanttemplate<class T>
59920cc2a42SHoward Hinnant    bool atomic_is_lock_free(const shared_ptr<T>* p);
60020cc2a42SHoward Hinnanttemplate<class T>
60120cc2a42SHoward Hinnant    shared_ptr<T> atomic_load(const shared_ptr<T>* p);
60220cc2a42SHoward Hinnanttemplate<class T>
60320cc2a42SHoward Hinnant    shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
60420cc2a42SHoward Hinnanttemplate<class T>
60520cc2a42SHoward Hinnant    void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
60620cc2a42SHoward Hinnanttemplate<class T>
60720cc2a42SHoward Hinnant    void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
60820cc2a42SHoward Hinnanttemplate<class T>
60920cc2a42SHoward Hinnant    shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
61020cc2a42SHoward Hinnanttemplate<class T>
61120cc2a42SHoward Hinnant    shared_ptr<T>
61220cc2a42SHoward Hinnant    atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
61320cc2a42SHoward Hinnanttemplate<class T>
61420cc2a42SHoward Hinnant    bool
61520cc2a42SHoward Hinnant    atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
61620cc2a42SHoward Hinnanttemplate<class T>
61720cc2a42SHoward Hinnant    bool
61820cc2a42SHoward Hinnant    atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
61920cc2a42SHoward Hinnanttemplate<class T>
62020cc2a42SHoward Hinnant    bool
62120cc2a42SHoward Hinnant    atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
62220cc2a42SHoward Hinnant                                          shared_ptr<T> w, memory_order success,
62320cc2a42SHoward Hinnant                                          memory_order failure);
62420cc2a42SHoward Hinnanttemplate<class T>
62520cc2a42SHoward Hinnant    bool
62620cc2a42SHoward Hinnant    atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
62720cc2a42SHoward Hinnant                                            shared_ptr<T> w, memory_order success,
62820cc2a42SHoward Hinnant                                            memory_order failure);
62920cc2a42SHoward Hinnant// Hash support
63020cc2a42SHoward Hinnanttemplate <class T> struct hash;
63120cc2a42SHoward Hinnanttemplate <class T, class D> struct hash<unique_ptr<T, D> >;
63220cc2a42SHoward Hinnanttemplate <class T> struct hash<shared_ptr<T> >;
63320cc2a42SHoward Hinnant
63440a01d53SMarshall Clowtemplate <class T, class Alloc>
63540a01d53SMarshall Clow  inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
63640a01d53SMarshall Clow
63720cc2a42SHoward Hinnant// Pointer safety
63820cc2a42SHoward Hinnantenum class pointer_safety { relaxed, preferred, strict };
63920cc2a42SHoward Hinnantvoid declare_reachable(void *p);
64020cc2a42SHoward Hinnanttemplate <class T> T *undeclare_reachable(T *p);
64120cc2a42SHoward Hinnantvoid declare_no_pointers(char *p, size_t n);
64220cc2a42SHoward Hinnantvoid undeclare_no_pointers(char *p, size_t n);
6433739fe79SHoward Hinnantpointer_safety get_pointer_safety() noexcept;
64420cc2a42SHoward Hinnant
6453e519524SHoward Hinnantvoid* align(size_t alignment, size_t size, void*& ptr, size_t& space);
6463e519524SHoward Hinnant
6473e519524SHoward Hinnant}  // std
6483e519524SHoward Hinnant
6493e519524SHoward Hinnant*/
6503e519524SHoward Hinnant
6513e519524SHoward Hinnant#include <__config>
6523e519524SHoward Hinnant#include <type_traits>
6533e519524SHoward Hinnant#include <typeinfo>
6543e519524SHoward Hinnant#include <cstddef>
6553e519524SHoward Hinnant#include <cstdint>
6563e519524SHoward Hinnant#include <new>
6573e519524SHoward Hinnant#include <utility>
6583e519524SHoward Hinnant#include <limits>
6593e519524SHoward Hinnant#include <iterator>
6603e519524SHoward Hinnant#include <__functional_base>
661e3163f5aSHoward Hinnant#include <iosfwd>
662a87b5e34SHoward Hinnant#include <tuple>
6634524d6e7SEric Fiselier#include <stdexcept>
6649741d6c9SHoward Hinnant#include <cstring>
665c88580c4SEric Fiselier#include <cassert>
666749adebaSEric Fiselier#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
667d77851e8SHoward Hinnant#  include <atomic>
668d77851e8SHoward Hinnant#endif
669f56972e2SMarshall Clow#include <version>
670d77851e8SHoward Hinnant
671073458b1SHoward Hinnant#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
6723e519524SHoward Hinnant#pragma GCC system_header
673073458b1SHoward Hinnant#endif
6743e519524SHoward Hinnant
675a016efb1SEric Fiselier_LIBCPP_PUSH_MACROS
676a016efb1SEric Fiselier#include <__undef_macros>
677a016efb1SEric Fiselier
678a016efb1SEric Fiselier
6793e519524SHoward Hinnant_LIBCPP_BEGIN_NAMESPACE_STD
6803e519524SHoward Hinnant
6811faf289eSEric Fiseliertemplate <class _ValueType>
682dc7200b4SLouis Dionneinline _LIBCPP_INLINE_VISIBILITY
6831faf289eSEric Fiselier_ValueType __libcpp_relaxed_load(_ValueType const* __value) {
6841faf289eSEric Fiselier#if !defined(_LIBCPP_HAS_NO_THREADS) && \
6851faf289eSEric Fiselier    defined(__ATOMIC_RELAXED) &&        \
686ae62727aSRichard Smith    (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
6871faf289eSEric Fiselier    return __atomic_load_n(__value, __ATOMIC_RELAXED);
6881faf289eSEric Fiselier#else
6891faf289eSEric Fiselier    return *__value;
6901faf289eSEric Fiselier#endif
6911faf289eSEric Fiselier}
6921faf289eSEric Fiselier
693224264adSKuba Breckatemplate <class _ValueType>
694dc7200b4SLouis Dionneinline _LIBCPP_INLINE_VISIBILITY
695224264adSKuba Brecka_ValueType __libcpp_acquire_load(_ValueType const* __value) {
696224264adSKuba Brecka#if !defined(_LIBCPP_HAS_NO_THREADS) && \
697224264adSKuba Brecka    defined(__ATOMIC_ACQUIRE) &&        \
698ae62727aSRichard Smith    (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
699224264adSKuba Brecka    return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
700224264adSKuba Brecka#else
701224264adSKuba Brecka    return *__value;
702224264adSKuba Brecka#endif
703224264adSKuba Brecka}
704224264adSKuba Brecka
7051c7fe126SMarshall Clow// addressof moved to <type_traits>
70664ec101eSDouglas Gregor
7073e519524SHoward Hinnanttemplate <class _Tp> class allocator;
7083e519524SHoward Hinnant
7093e519524SHoward Hinnanttemplate <>
710e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS allocator<void>
7113e519524SHoward Hinnant{
7123e519524SHoward Hinnantpublic:
7133e519524SHoward Hinnant    typedef void*             pointer;
7143e519524SHoward Hinnant    typedef const void*       const_pointer;
7153e519524SHoward Hinnant    typedef void              value_type;
7163e519524SHoward Hinnant
7173e519524SHoward Hinnant    template <class _Up> struct rebind {typedef allocator<_Up> other;};
7183e519524SHoward Hinnant};
7193e519524SHoward Hinnant
72021fa1887SHoward Hinnanttemplate <>
721e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS allocator<const void>
72221fa1887SHoward Hinnant{
72321fa1887SHoward Hinnantpublic:
72421fa1887SHoward Hinnant    typedef const void*       pointer;
72521fa1887SHoward Hinnant    typedef const void*       const_pointer;
72621fa1887SHoward Hinnant    typedef const void        value_type;
72721fa1887SHoward Hinnant
72821fa1887SHoward Hinnant    template <class _Up> struct rebind {typedef allocator<_Up> other;};
72921fa1887SHoward Hinnant};
73021fa1887SHoward Hinnant
7313e519524SHoward Hinnant// pointer_traits
7323e519524SHoward Hinnant
733b082f11fSMarshall Clowtemplate <class _Tp, class = void>
734b082f11fSMarshall Clowstruct __has_element_type : false_type {};
735b082f11fSMarshall Clow
7363e519524SHoward Hinnanttemplate <class _Tp>
737b082f11fSMarshall Clowstruct __has_element_type<_Tp,
738b082f11fSMarshall Clow              typename __void_t<typename _Tp::element_type>::type> : true_type {};
7393e519524SHoward Hinnant
7403e519524SHoward Hinnanttemplate <class _Ptr, bool = __has_element_type<_Ptr>::value>
7413e519524SHoward Hinnantstruct __pointer_traits_element_type;
7423e519524SHoward Hinnant
7433e519524SHoward Hinnanttemplate <class _Ptr>
7443e519524SHoward Hinnantstruct __pointer_traits_element_type<_Ptr, true>
7453e519524SHoward Hinnant{
74614d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
7473e519524SHoward Hinnant};
7483e519524SHoward Hinnant
7493e519524SHoward Hinnant#ifndef _LIBCPP_HAS_NO_VARIADICS
7503e519524SHoward Hinnant
7513e519524SHoward Hinnanttemplate <template <class, class...> class _Sp, class _Tp, class ..._Args>
7523e519524SHoward Hinnantstruct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
7533e519524SHoward Hinnant{
75414d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
7553e519524SHoward Hinnant};
7563e519524SHoward Hinnant
7573e519524SHoward Hinnanttemplate <template <class, class...> class _Sp, class _Tp, class ..._Args>
7583e519524SHoward Hinnantstruct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
7593e519524SHoward Hinnant{
76014d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE _Tp type;
7613e519524SHoward Hinnant};
7623e519524SHoward Hinnant
763b3371f6fSHoward Hinnant#else  // _LIBCPP_HAS_NO_VARIADICS
7643e519524SHoward Hinnant
7653e519524SHoward Hinnanttemplate <template <class> class _Sp, class _Tp>
7663e519524SHoward Hinnantstruct __pointer_traits_element_type<_Sp<_Tp>, true>
7673e519524SHoward Hinnant{
7683e519524SHoward Hinnant    typedef typename _Sp<_Tp>::element_type type;
7693e519524SHoward Hinnant};
7703e519524SHoward Hinnant
7713e519524SHoward Hinnanttemplate <template <class> class _Sp, class _Tp>
7723e519524SHoward Hinnantstruct __pointer_traits_element_type<_Sp<_Tp>, false>
7733e519524SHoward Hinnant{
7743e519524SHoward Hinnant    typedef _Tp type;
7753e519524SHoward Hinnant};
7763e519524SHoward Hinnant
7773e519524SHoward Hinnanttemplate <template <class, class> class _Sp, class _Tp, class _A0>
7783e519524SHoward Hinnantstruct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
7793e519524SHoward Hinnant{
7803e519524SHoward Hinnant    typedef typename _Sp<_Tp, _A0>::element_type type;
7813e519524SHoward Hinnant};
7823e519524SHoward Hinnant
7833e519524SHoward Hinnanttemplate <template <class, class> class _Sp, class _Tp, class _A0>
7843e519524SHoward Hinnantstruct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
7853e519524SHoward Hinnant{
7863e519524SHoward Hinnant    typedef _Tp type;
7873e519524SHoward Hinnant};
7883e519524SHoward Hinnant
7893e519524SHoward Hinnanttemplate <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
7903e519524SHoward Hinnantstruct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
7913e519524SHoward Hinnant{
7923e519524SHoward Hinnant    typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
7933e519524SHoward Hinnant};
7943e519524SHoward Hinnant
7953e519524SHoward Hinnanttemplate <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
7963e519524SHoward Hinnantstruct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
7973e519524SHoward Hinnant{
7983e519524SHoward Hinnant    typedef _Tp type;
7993e519524SHoward Hinnant};
8003e519524SHoward Hinnant
8013e519524SHoward Hinnanttemplate <template <class, class, class, class> class _Sp, class _Tp, class _A0,
8023e519524SHoward Hinnant                                                           class _A1, class _A2>
8033e519524SHoward Hinnantstruct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
8043e519524SHoward Hinnant{
8053e519524SHoward Hinnant    typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
8063e519524SHoward Hinnant};
8073e519524SHoward Hinnant
8083e519524SHoward Hinnanttemplate <template <class, class, class, class> class _Sp, class _Tp, class _A0,
8093e519524SHoward Hinnant                                                           class _A1, class _A2>
8103e519524SHoward Hinnantstruct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
8113e519524SHoward Hinnant{
8123e519524SHoward Hinnant    typedef _Tp type;
8133e519524SHoward Hinnant};
8143e519524SHoward Hinnant
815b3371f6fSHoward Hinnant#endif  // _LIBCPP_HAS_NO_VARIADICS
8163e519524SHoward Hinnant
817b082f11fSMarshall Clowtemplate <class _Tp, class = void>
818b082f11fSMarshall Clowstruct __has_difference_type : false_type {};
819b082f11fSMarshall Clow
8203e519524SHoward Hinnanttemplate <class _Tp>
821b082f11fSMarshall Clowstruct __has_difference_type<_Tp,
822b082f11fSMarshall Clow            typename __void_t<typename _Tp::difference_type>::type> : true_type {};
8233e519524SHoward Hinnant
8243e519524SHoward Hinnanttemplate <class _Ptr, bool = __has_difference_type<_Ptr>::value>
8253e519524SHoward Hinnantstruct __pointer_traits_difference_type
8263e519524SHoward Hinnant{
82714d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
8283e519524SHoward Hinnant};
8293e519524SHoward Hinnant
8303e519524SHoward Hinnanttemplate <class _Ptr>
8313e519524SHoward Hinnantstruct __pointer_traits_difference_type<_Ptr, true>
8323e519524SHoward Hinnant{
83314d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
8343e519524SHoward Hinnant};
8353e519524SHoward Hinnant
8363e519524SHoward Hinnanttemplate <class _Tp, class _Up>
8373e519524SHoward Hinnantstruct __has_rebind
8383e519524SHoward Hinnant{
8393e519524SHoward Hinnantprivate:
84054d333a6SHoward Hinnant    struct __two {char __lx; char __lxx;};
8413e519524SHoward Hinnant    template <class _Xp> static __two __test(...);
8423e519524SHoward Hinnant    template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
8433e519524SHoward Hinnantpublic:
8443e519524SHoward Hinnant    static const bool value = sizeof(__test<_Tp>(0)) == 1;
8453e519524SHoward Hinnant};
8463e519524SHoward Hinnant
8473e519524SHoward Hinnanttemplate <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
8483e519524SHoward Hinnantstruct __pointer_traits_rebind
8493e519524SHoward Hinnant{
85054613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
85114d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
8523e519524SHoward Hinnant#else
85314d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
8543e519524SHoward Hinnant#endif
8553e519524SHoward Hinnant};
8563e519524SHoward Hinnant
8573e519524SHoward Hinnant#ifndef _LIBCPP_HAS_NO_VARIADICS
8583e519524SHoward Hinnant
8593e519524SHoward Hinnanttemplate <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
8603e519524SHoward Hinnantstruct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
8613e519524SHoward Hinnant{
86254613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
86314d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
8643e519524SHoward Hinnant#else
86514d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
8663e519524SHoward Hinnant#endif
8673e519524SHoward Hinnant};
8683e519524SHoward Hinnant
8693e519524SHoward Hinnanttemplate <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
8703e519524SHoward Hinnantstruct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
8713e519524SHoward Hinnant{
8723e519524SHoward Hinnant    typedef _Sp<_Up, _Args...> type;
8733e519524SHoward Hinnant};
8743e519524SHoward Hinnant
875b3371f6fSHoward Hinnant#else  // _LIBCPP_HAS_NO_VARIADICS
8763e519524SHoward Hinnant
8773e519524SHoward Hinnanttemplate <template <class> class _Sp, class _Tp, class _Up>
8783e519524SHoward Hinnantstruct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
8793e519524SHoward Hinnant{
88054613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
8813e519524SHoward Hinnant    typedef typename _Sp<_Tp>::template rebind<_Up> type;
8823e519524SHoward Hinnant#else
8833e519524SHoward Hinnant    typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
8843e519524SHoward Hinnant#endif
8853e519524SHoward Hinnant};
8863e519524SHoward Hinnant
8873e519524SHoward Hinnanttemplate <template <class> class _Sp, class _Tp, class _Up>
8883e519524SHoward Hinnantstruct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
8893e519524SHoward Hinnant{
8903e519524SHoward Hinnant    typedef _Sp<_Up> type;
8913e519524SHoward Hinnant};
8923e519524SHoward Hinnant
8933e519524SHoward Hinnanttemplate <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
8943e519524SHoward Hinnantstruct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
8953e519524SHoward Hinnant{
89654613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
8973e519524SHoward Hinnant    typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
8983e519524SHoward Hinnant#else
8993e519524SHoward Hinnant    typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
9003e519524SHoward Hinnant#endif
9013e519524SHoward Hinnant};
9023e519524SHoward Hinnant
9033e519524SHoward Hinnanttemplate <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
9043e519524SHoward Hinnantstruct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
9053e519524SHoward Hinnant{
9063e519524SHoward Hinnant    typedef _Sp<_Up, _A0> type;
9073e519524SHoward Hinnant};
9083e519524SHoward Hinnant
9093e519524SHoward Hinnanttemplate <template <class, class, class> class _Sp, class _Tp, class _A0,
9103e519524SHoward Hinnant                                         class _A1, class _Up>
9113e519524SHoward Hinnantstruct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
9123e519524SHoward Hinnant{
91354613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
9143e519524SHoward Hinnant    typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
9153e519524SHoward Hinnant#else
9163e519524SHoward Hinnant    typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
9173e519524SHoward Hinnant#endif
9183e519524SHoward Hinnant};
9193e519524SHoward Hinnant
9203e519524SHoward Hinnanttemplate <template <class, class, class> class _Sp, class _Tp, class _A0,
9213e519524SHoward Hinnant                                         class _A1, class _Up>
9223e519524SHoward Hinnantstruct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
9233e519524SHoward Hinnant{
9243e519524SHoward Hinnant    typedef _Sp<_Up, _A0, _A1> type;
9253e519524SHoward Hinnant};
9263e519524SHoward Hinnant
9273e519524SHoward Hinnanttemplate <template <class, class, class, class> class _Sp, class _Tp, class _A0,
9283e519524SHoward Hinnant                                                class _A1, class _A2, class _Up>
9293e519524SHoward Hinnantstruct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
9303e519524SHoward Hinnant{
93154613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
9323e519524SHoward Hinnant    typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
9333e519524SHoward Hinnant#else
9343e519524SHoward Hinnant    typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
9353e519524SHoward Hinnant#endif
9363e519524SHoward Hinnant};
9373e519524SHoward Hinnant
9383e519524SHoward Hinnanttemplate <template <class, class, class, class> class _Sp, class _Tp, class _A0,
9393e519524SHoward Hinnant                                                class _A1, class _A2, class _Up>
9403e519524SHoward Hinnantstruct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
9413e519524SHoward Hinnant{
9423e519524SHoward Hinnant    typedef _Sp<_Up, _A0, _A1, _A2> type;
9433e519524SHoward Hinnant};
9443e519524SHoward Hinnant
945b3371f6fSHoward Hinnant#endif  // _LIBCPP_HAS_NO_VARIADICS
9463e519524SHoward Hinnant
9473e519524SHoward Hinnanttemplate <class _Ptr>
948e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS pointer_traits
9493e519524SHoward Hinnant{
9503e519524SHoward Hinnant    typedef _Ptr                                                     pointer;
9513e519524SHoward Hinnant    typedef typename __pointer_traits_element_type<pointer>::type    element_type;
9523e519524SHoward Hinnant    typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
9533e519524SHoward Hinnant
95454613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
955f7384975SHoward Hinnant    template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
9563e519524SHoward Hinnant#else
9573e519524SHoward Hinnant    template <class _Up> struct rebind
9583e519524SHoward Hinnant        {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
95954613ab4SEric Fiselier#endif  // _LIBCPP_CXX03_LANG
9603e519524SHoward Hinnant
9613e519524SHoward Hinnantprivate:
9623e519524SHoward Hinnant    struct __nat {};
9633e519524SHoward Hinnantpublic:
964848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
9653e519524SHoward Hinnant    static pointer pointer_to(typename conditional<is_void<element_type>::value,
9663e519524SHoward Hinnant                                           __nat, element_type>::type& __r)
9673e519524SHoward Hinnant        {return pointer::pointer_to(__r);}
9683e519524SHoward Hinnant};
9693e519524SHoward Hinnant
9703e519524SHoward Hinnanttemplate <class _Tp>
971e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
9723e519524SHoward Hinnant{
9733e519524SHoward Hinnant    typedef _Tp*      pointer;
9743e519524SHoward Hinnant    typedef _Tp       element_type;
9753e519524SHoward Hinnant    typedef ptrdiff_t difference_type;
9763e519524SHoward Hinnant
97754613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
9783e519524SHoward Hinnant    template <class _Up> using rebind = _Up*;
9793e519524SHoward Hinnant#else
9803e519524SHoward Hinnant    template <class _Up> struct rebind {typedef _Up* other;};
9813e519524SHoward Hinnant#endif
9823e519524SHoward Hinnant
9833e519524SHoward Hinnantprivate:
9843e519524SHoward Hinnant    struct __nat {};
9853e519524SHoward Hinnantpublic:
98672720269SLouis Dionne    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
9873e519524SHoward Hinnant    static pointer pointer_to(typename conditional<is_void<element_type>::value,
9883739fe79SHoward Hinnant                                      __nat, element_type>::type& __r) _NOEXCEPT
989ce48a113SHoward Hinnant        {return _VSTD::addressof(__r);}
9903e519524SHoward Hinnant};
9913e519524SHoward Hinnant
9921c813407SEric Fiseliertemplate <class _From, class _To>
9931c813407SEric Fiselierstruct __rebind_pointer {
99454613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
9951c813407SEric Fiselier    typedef typename pointer_traits<_From>::template rebind<_To>        type;
9961c813407SEric Fiselier#else
9971c813407SEric Fiselier    typedef typename pointer_traits<_From>::template rebind<_To>::other type;
9981c813407SEric Fiselier#endif
9991c813407SEric Fiselier};
10001c813407SEric Fiselier
10013e519524SHoward Hinnant// allocator_traits
10023e519524SHoward Hinnant
1003b082f11fSMarshall Clowtemplate <class _Tp, class = void>
1004b082f11fSMarshall Clowstruct __has_pointer_type : false_type {};
10053e519524SHoward Hinnant
10063e519524SHoward Hinnanttemplate <class _Tp>
1007b082f11fSMarshall Clowstruct __has_pointer_type<_Tp,
1008b082f11fSMarshall Clow          typename __void_t<typename _Tp::pointer>::type> : true_type {};
10093e519524SHoward Hinnant
10103e519524SHoward Hinnantnamespace __pointer_type_imp
10113e519524SHoward Hinnant{
10123e519524SHoward Hinnant
10133e519524SHoward Hinnanttemplate <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
10143e519524SHoward Hinnantstruct __pointer_type
10153e519524SHoward Hinnant{
101614d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
10173e519524SHoward Hinnant};
10183e519524SHoward Hinnant
10193e519524SHoward Hinnanttemplate <class _Tp, class _Dp>
10203e519524SHoward Hinnantstruct __pointer_type<_Tp, _Dp, false>
10213e519524SHoward Hinnant{
102214d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
10233e519524SHoward Hinnant};
10243e519524SHoward Hinnant
1025a4a1ef1fSHoward Hinnant}  // __pointer_type_imp
10263e519524SHoward Hinnant
10273e519524SHoward Hinnanttemplate <class _Tp, class _Dp>
10283e519524SHoward Hinnantstruct __pointer_type
10293e519524SHoward Hinnant{
103014d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
10313e519524SHoward Hinnant};
10323e519524SHoward Hinnant
1033b082f11fSMarshall Clowtemplate <class _Tp, class = void>
1034b082f11fSMarshall Clowstruct __has_const_pointer : false_type {};
1035b082f11fSMarshall Clow
10363e519524SHoward Hinnanttemplate <class _Tp>
1037b082f11fSMarshall Clowstruct __has_const_pointer<_Tp,
1038b082f11fSMarshall Clow            typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
10393e519524SHoward Hinnant
10403e519524SHoward Hinnanttemplate <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
10413e519524SHoward Hinnantstruct __const_pointer
10423e519524SHoward Hinnant{
104314d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
10443e519524SHoward Hinnant};
10453e519524SHoward Hinnant
10463e519524SHoward Hinnanttemplate <class _Tp, class _Ptr, class _Alloc>
10473e519524SHoward Hinnantstruct __const_pointer<_Tp, _Ptr, _Alloc, false>
10483e519524SHoward Hinnant{
104954613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
105014d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
10513e519524SHoward Hinnant#else
10523e519524SHoward Hinnant    typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
10533e519524SHoward Hinnant#endif
10543e519524SHoward Hinnant};
10553e519524SHoward Hinnant
1056b082f11fSMarshall Clowtemplate <class _Tp, class = void>
1057b082f11fSMarshall Clowstruct __has_void_pointer : false_type {};
1058b082f11fSMarshall Clow
10593e519524SHoward Hinnanttemplate <class _Tp>
1060b082f11fSMarshall Clowstruct __has_void_pointer<_Tp,
1061b082f11fSMarshall Clow               typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
10623e519524SHoward Hinnant
10633e519524SHoward Hinnanttemplate <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
10643e519524SHoward Hinnantstruct __void_pointer
10653e519524SHoward Hinnant{
106614d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
10673e519524SHoward Hinnant};
10683e519524SHoward Hinnant
10693e519524SHoward Hinnanttemplate <class _Ptr, class _Alloc>
10703e519524SHoward Hinnantstruct __void_pointer<_Ptr, _Alloc, false>
10713e519524SHoward Hinnant{
107254613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
107314d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
10743e519524SHoward Hinnant#else
107514d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
10763e519524SHoward Hinnant#endif
10773e519524SHoward Hinnant};
10783e519524SHoward Hinnant
1079b082f11fSMarshall Clowtemplate <class _Tp, class = void>
1080b082f11fSMarshall Clowstruct __has_const_void_pointer : false_type {};
1081b082f11fSMarshall Clow
10823e519524SHoward Hinnanttemplate <class _Tp>
1083b082f11fSMarshall Clowstruct __has_const_void_pointer<_Tp,
1084b082f11fSMarshall Clow            typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
10853e519524SHoward Hinnant
10863e519524SHoward Hinnanttemplate <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
10873e519524SHoward Hinnantstruct __const_void_pointer
10883e519524SHoward Hinnant{
108914d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
10903e519524SHoward Hinnant};
10913e519524SHoward Hinnant
10923e519524SHoward Hinnanttemplate <class _Ptr, class _Alloc>
10933e519524SHoward Hinnantstruct __const_void_pointer<_Ptr, _Alloc, false>
10943e519524SHoward Hinnant{
109554613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
109614d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
10973e519524SHoward Hinnant#else
109814d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
10993e519524SHoward Hinnant#endif
11003e519524SHoward Hinnant};
11013e519524SHoward Hinnant
1102c003db1fSHoward Hinnanttemplate <class _Tp>
110332952747SEric Fiselierinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1104c003db1fSHoward Hinnant_Tp*
1105c003db1fSHoward Hinnant__to_raw_pointer(_Tp* __p) _NOEXCEPT
11063e519524SHoward Hinnant{
11073e519524SHoward Hinnant    return __p;
11083e519524SHoward Hinnant}
11093e519524SHoward Hinnant
111032952747SEric Fiselier#if _LIBCPP_STD_VER <= 17
11113e519524SHoward Hinnanttemplate <class _Pointer>
11123e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
11133e519524SHoward Hinnanttypename pointer_traits<_Pointer>::element_type*
11143739fe79SHoward Hinnant__to_raw_pointer(_Pointer __p) _NOEXCEPT
11153e519524SHoward Hinnant{
1116ce48a113SHoward Hinnant    return _VSTD::__to_raw_pointer(__p.operator->());
11173e519524SHoward Hinnant}
111832952747SEric Fiselier#else
111932952747SEric Fiseliertemplate <class _Pointer>
112032952747SEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
112132952747SEric Fiselierauto
112232952747SEric Fiselier__to_raw_pointer(const _Pointer& __p) _NOEXCEPT
112332952747SEric Fiselier-> decltype(pointer_traits<_Pointer>::to_address(__p))
112432952747SEric Fiselier{
112532952747SEric Fiselier    return pointer_traits<_Pointer>::to_address(__p);
112632952747SEric Fiselier}
112732952747SEric Fiselier
112832952747SEric Fiseliertemplate <class _Pointer, class... _None>
112932952747SEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
113032952747SEric Fiselierauto
113132952747SEric Fiselier__to_raw_pointer(const _Pointer& __p, _None...) _NOEXCEPT
113232952747SEric Fiselier{
113332952747SEric Fiselier    return _VSTD::__to_raw_pointer(__p.operator->());
113432952747SEric Fiselier}
113532952747SEric Fiselier
113632952747SEric Fiseliertemplate <class _Tp>
113732952747SEric Fiselierinline _LIBCPP_INLINE_VISIBILITY constexpr
113832952747SEric Fiselier_Tp*
113932952747SEric Fiselierto_address(_Tp* __p) _NOEXCEPT
114032952747SEric Fiselier{
114132952747SEric Fiselier    static_assert(!is_function_v<_Tp>, "_Tp is a function type");
114232952747SEric Fiselier    return __p;
114332952747SEric Fiselier}
114432952747SEric Fiselier
114532952747SEric Fiseliertemplate <class _Pointer>
114632952747SEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
114732952747SEric Fiselierauto
114832952747SEric Fiselierto_address(const _Pointer& __p) _NOEXCEPT
114932952747SEric Fiselier{
115032952747SEric Fiselier    return _VSTD::__to_raw_pointer(__p);
115132952747SEric Fiselier}
115232952747SEric Fiselier#endif
11533e519524SHoward Hinnant
1154b082f11fSMarshall Clowtemplate <class _Tp, class = void>
1155b082f11fSMarshall Clowstruct __has_size_type : false_type {};
1156b082f11fSMarshall Clow
11573e519524SHoward Hinnanttemplate <class _Tp>
1158b082f11fSMarshall Clowstruct __has_size_type<_Tp,
1159b082f11fSMarshall Clow               typename __void_t<typename _Tp::size_type>::type> : true_type {};
11603e519524SHoward Hinnant
1161a4a1ef1fSHoward Hinnanttemplate <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
11623e519524SHoward Hinnantstruct __size_type
11633e519524SHoward Hinnant{
116414d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
11653e519524SHoward Hinnant};
11663e519524SHoward Hinnant
1167a4a1ef1fSHoward Hinnanttemplate <class _Alloc, class _DiffType>
1168a4a1ef1fSHoward Hinnantstruct __size_type<_Alloc, _DiffType, true>
11693e519524SHoward Hinnant{
117014d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
11713e519524SHoward Hinnant};
11723e519524SHoward Hinnant
1173b082f11fSMarshall Clowtemplate <class _Tp, class = void>
1174b082f11fSMarshall Clowstruct __has_propagate_on_container_copy_assignment : false_type {};
1175b082f11fSMarshall Clow
11763e519524SHoward Hinnanttemplate <class _Tp>
1177b082f11fSMarshall Clowstruct __has_propagate_on_container_copy_assignment<_Tp,
1178b082f11fSMarshall Clow    typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
1179b082f11fSMarshall Clow        : true_type {};
11803e519524SHoward Hinnant
11813e519524SHoward Hinnanttemplate <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
11823e519524SHoward Hinnantstruct __propagate_on_container_copy_assignment
11833e519524SHoward Hinnant{
118414d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE false_type type;
11853e519524SHoward Hinnant};
11863e519524SHoward Hinnant
11873e519524SHoward Hinnanttemplate <class _Alloc>
11883e519524SHoward Hinnantstruct __propagate_on_container_copy_assignment<_Alloc, true>
11893e519524SHoward Hinnant{
119014d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
11913e519524SHoward Hinnant};
11923e519524SHoward Hinnant
1193b082f11fSMarshall Clowtemplate <class _Tp, class = void>
1194b082f11fSMarshall Clowstruct __has_propagate_on_container_move_assignment : false_type {};
1195b082f11fSMarshall Clow
11963e519524SHoward Hinnanttemplate <class _Tp>
1197b082f11fSMarshall Clowstruct __has_propagate_on_container_move_assignment<_Tp,
1198b082f11fSMarshall Clow           typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
1199b082f11fSMarshall Clow               : true_type {};
12003e519524SHoward Hinnant
12013e519524SHoward Hinnanttemplate <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
12023e519524SHoward Hinnantstruct __propagate_on_container_move_assignment
12033e519524SHoward Hinnant{
12043e519524SHoward Hinnant    typedef false_type type;
12053e519524SHoward Hinnant};
12063e519524SHoward Hinnant
12073e519524SHoward Hinnanttemplate <class _Alloc>
12083e519524SHoward Hinnantstruct __propagate_on_container_move_assignment<_Alloc, true>
12093e519524SHoward Hinnant{
121014d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
12113e519524SHoward Hinnant};
12123e519524SHoward Hinnant
1213b082f11fSMarshall Clowtemplate <class _Tp, class = void>
1214b082f11fSMarshall Clowstruct __has_propagate_on_container_swap : false_type {};
1215b082f11fSMarshall Clow
12163e519524SHoward Hinnanttemplate <class _Tp>
1217b082f11fSMarshall Clowstruct __has_propagate_on_container_swap<_Tp,
1218b082f11fSMarshall Clow           typename __void_t<typename _Tp::propagate_on_container_swap>::type>
1219b082f11fSMarshall Clow               : true_type {};
12203e519524SHoward Hinnant
12213e519524SHoward Hinnanttemplate <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
12223e519524SHoward Hinnantstruct __propagate_on_container_swap
12233e519524SHoward Hinnant{
12243e519524SHoward Hinnant    typedef false_type type;
12253e519524SHoward Hinnant};
12263e519524SHoward Hinnant
12273e519524SHoward Hinnanttemplate <class _Alloc>
12283e519524SHoward Hinnantstruct __propagate_on_container_swap<_Alloc, true>
12293e519524SHoward Hinnant{
123014d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
12313e519524SHoward Hinnant};
12323e519524SHoward Hinnant
1233b082f11fSMarshall Clowtemplate <class _Tp, class = void>
1234b082f11fSMarshall Clowstruct __has_is_always_equal : false_type {};
1235b082f11fSMarshall Clow
123631a47313SMarshall Clowtemplate <class _Tp>
1237b082f11fSMarshall Clowstruct __has_is_always_equal<_Tp,
1238b082f11fSMarshall Clow           typename __void_t<typename _Tp::is_always_equal>::type>
1239b082f11fSMarshall Clow               : true_type {};
124031a47313SMarshall Clow
124131a47313SMarshall Clowtemplate <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
124231a47313SMarshall Clowstruct __is_always_equal
124331a47313SMarshall Clow{
124414d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
124531a47313SMarshall Clow};
124631a47313SMarshall Clow
124731a47313SMarshall Clowtemplate <class _Alloc>
124831a47313SMarshall Clowstruct __is_always_equal<_Alloc, true>
124931a47313SMarshall Clow{
125014d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
125131a47313SMarshall Clow};
125231a47313SMarshall Clow
12533e519524SHoward Hinnanttemplate <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
12543e519524SHoward Hinnantstruct __has_rebind_other
12553e519524SHoward Hinnant{
12563e519524SHoward Hinnantprivate:
125754d333a6SHoward Hinnant    struct __two {char __lx; char __lxx;};
12583e519524SHoward Hinnant    template <class _Xp> static __two __test(...);
12593e519524SHoward Hinnant    template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
12603e519524SHoward Hinnantpublic:
12613e519524SHoward Hinnant    static const bool value = sizeof(__test<_Tp>(0)) == 1;
12623e519524SHoward Hinnant};
12633e519524SHoward Hinnant
12643e519524SHoward Hinnanttemplate <class _Tp, class _Up>
12653e519524SHoward Hinnantstruct __has_rebind_other<_Tp, _Up, false>
12663e519524SHoward Hinnant{
12673e519524SHoward Hinnant    static const bool value = false;
12683e519524SHoward Hinnant};
12693e519524SHoward Hinnant
12703e519524SHoward Hinnanttemplate <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
12713e519524SHoward Hinnantstruct __allocator_traits_rebind
12723e519524SHoward Hinnant{
127314d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
12743e519524SHoward Hinnant};
12753e519524SHoward Hinnant
12763e519524SHoward Hinnant#ifndef _LIBCPP_HAS_NO_VARIADICS
12773e519524SHoward Hinnant
12783e519524SHoward Hinnanttemplate <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
12793e519524SHoward Hinnantstruct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
12803e519524SHoward Hinnant{
128114d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
12823e519524SHoward Hinnant};
12833e519524SHoward Hinnant
12843e519524SHoward Hinnanttemplate <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
12853e519524SHoward Hinnantstruct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
12863e519524SHoward Hinnant{
128714d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
12883e519524SHoward Hinnant};
12893e519524SHoward Hinnant
1290b3371f6fSHoward Hinnant#else  // _LIBCPP_HAS_NO_VARIADICS
12913e519524SHoward Hinnant
12923e519524SHoward Hinnanttemplate <template <class> class _Alloc, class _Tp, class _Up>
12933e519524SHoward Hinnantstruct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
12943e519524SHoward Hinnant{
12953e519524SHoward Hinnant    typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
12963e519524SHoward Hinnant};
12973e519524SHoward Hinnant
12983e519524SHoward Hinnanttemplate <template <class> class _Alloc, class _Tp, class _Up>
12993e519524SHoward Hinnantstruct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
13003e519524SHoward Hinnant{
13013e519524SHoward Hinnant    typedef _Alloc<_Up> type;
13023e519524SHoward Hinnant};
13033e519524SHoward Hinnant
13043e519524SHoward Hinnanttemplate <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
13053e519524SHoward Hinnantstruct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
13063e519524SHoward Hinnant{
13073e519524SHoward Hinnant    typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
13083e519524SHoward Hinnant};
13093e519524SHoward Hinnant
13103e519524SHoward Hinnanttemplate <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
13113e519524SHoward Hinnantstruct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
13123e519524SHoward Hinnant{
13133e519524SHoward Hinnant    typedef _Alloc<_Up, _A0> type;
13143e519524SHoward Hinnant};
13153e519524SHoward Hinnant
13163e519524SHoward Hinnanttemplate <template <class, class, class> class _Alloc, class _Tp, class _A0,
13173e519524SHoward Hinnant                                         class _A1, class _Up>
13183e519524SHoward Hinnantstruct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
13193e519524SHoward Hinnant{
13203e519524SHoward Hinnant    typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
13213e519524SHoward Hinnant};
13223e519524SHoward Hinnant
13233e519524SHoward Hinnanttemplate <template <class, class, class> class _Alloc, class _Tp, class _A0,
13243e519524SHoward Hinnant                                         class _A1, class _Up>
13253e519524SHoward Hinnantstruct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
13263e519524SHoward Hinnant{
13273e519524SHoward Hinnant    typedef _Alloc<_Up, _A0, _A1> type;
13283e519524SHoward Hinnant};
13293e519524SHoward Hinnant
13303e519524SHoward Hinnanttemplate <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
13313e519524SHoward Hinnant                                                class _A1, class _A2, class _Up>
13323e519524SHoward Hinnantstruct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
13333e519524SHoward Hinnant{
13343e519524SHoward Hinnant    typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
13353e519524SHoward Hinnant};
13363e519524SHoward Hinnant
13373e519524SHoward Hinnanttemplate <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
13383e519524SHoward Hinnant                                                class _A1, class _A2, class _Up>
13393e519524SHoward Hinnantstruct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
13403e519524SHoward Hinnant{
13413e519524SHoward Hinnant    typedef _Alloc<_Up, _A0, _A1, _A2> type;
13423e519524SHoward Hinnant};
13433e519524SHoward Hinnant
1344b3371f6fSHoward Hinnant#endif  // _LIBCPP_HAS_NO_VARIADICS
13453e519524SHoward Hinnant
134654613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
13473e519524SHoward Hinnant
13483e519524SHoward Hinnanttemplate <class _Alloc, class _SizeType, class _ConstVoidPtr>
13493e519524SHoward Hinnantauto
13503e519524SHoward Hinnant__has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
13519f8fef95SEric Fiselier    -> decltype((void)__a.allocate(__sz, __p), true_type());
13523e519524SHoward Hinnant
13533e519524SHoward Hinnanttemplate <class _Alloc, class _SizeType, class _ConstVoidPtr>
13543e519524SHoward Hinnantauto
13553e519524SHoward Hinnant__has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
13563e519524SHoward Hinnant    -> false_type;
13573e519524SHoward Hinnant
13583e519524SHoward Hinnanttemplate <class _Alloc, class _SizeType, class _ConstVoidPtr>
13593e519524SHoward Hinnantstruct __has_allocate_hint
13603e519524SHoward Hinnant    : integral_constant<bool,
13613e519524SHoward Hinnant        is_same<
13629f8fef95SEric Fiselier            decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
13633e519524SHoward Hinnant                                          declval<_SizeType>(),
13643e519524SHoward Hinnant                                          declval<_ConstVoidPtr>())),
13653e519524SHoward Hinnant            true_type>::value>
13663e519524SHoward Hinnant{
13673e519524SHoward Hinnant};
13683e519524SHoward Hinnant
136954613ab4SEric Fiselier#else  // _LIBCPP_CXX03_LANG
13703e519524SHoward Hinnant
13713e519524SHoward Hinnanttemplate <class _Alloc, class _SizeType, class _ConstVoidPtr>
13723e519524SHoward Hinnantstruct __has_allocate_hint
13733e519524SHoward Hinnant    : true_type
13743e519524SHoward Hinnant{
13753e519524SHoward Hinnant};
13763e519524SHoward Hinnant
137754613ab4SEric Fiselier#endif  // _LIBCPP_CXX03_LANG
13783e519524SHoward Hinnant
137954613ab4SEric Fiselier#if !defined(_LIBCPP_CXX03_LANG)
13803e519524SHoward Hinnant
13813e519524SHoward Hinnanttemplate <class _Alloc, class _Tp, class ..._Args>
1382ce48a113SHoward Hinnantdecltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
1383ce48a113SHoward Hinnant                                           _VSTD::declval<_Args>()...),
13843e519524SHoward Hinnant                                           true_type())
13853e519524SHoward Hinnant__has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
13863e519524SHoward Hinnant
13873e519524SHoward Hinnanttemplate <class _Alloc, class _Pointer, class ..._Args>
13883e519524SHoward Hinnantfalse_type
13893e519524SHoward Hinnant__has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
13903e519524SHoward Hinnant
13913e519524SHoward Hinnanttemplate <class _Alloc, class _Pointer, class ..._Args>
13923e519524SHoward Hinnantstruct __has_construct
13933e519524SHoward Hinnant    : integral_constant<bool,
13943e519524SHoward Hinnant        is_same<
13959f8fef95SEric Fiselier            decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
13963e519524SHoward Hinnant                                          declval<_Pointer>(),
13973e519524SHoward Hinnant                                          declval<_Args>()...)),
13983e519524SHoward Hinnant            true_type>::value>
13993e519524SHoward Hinnant{
14003e519524SHoward Hinnant};
14013e519524SHoward Hinnant
14023e519524SHoward Hinnanttemplate <class _Alloc, class _Pointer>
14033e519524SHoward Hinnantauto
14043e519524SHoward Hinnant__has_destroy_test(_Alloc&& __a, _Pointer&& __p)
14053e519524SHoward Hinnant    -> decltype(__a.destroy(__p), true_type());
14063e519524SHoward Hinnant
14073e519524SHoward Hinnanttemplate <class _Alloc, class _Pointer>
14083e519524SHoward Hinnantauto
14093e519524SHoward Hinnant__has_destroy_test(const _Alloc& __a, _Pointer&& __p)
14103e519524SHoward Hinnant    -> false_type;
14113e519524SHoward Hinnant
14123e519524SHoward Hinnanttemplate <class _Alloc, class _Pointer>
14133e519524SHoward Hinnantstruct __has_destroy
14143e519524SHoward Hinnant    : integral_constant<bool,
14153e519524SHoward Hinnant        is_same<
14169f8fef95SEric Fiselier            decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
14173e519524SHoward Hinnant                                        declval<_Pointer>())),
14183e519524SHoward Hinnant            true_type>::value>
14193e519524SHoward Hinnant{
14203e519524SHoward Hinnant};
14213e519524SHoward Hinnant
14223e519524SHoward Hinnanttemplate <class _Alloc>
14233e519524SHoward Hinnantauto
14243e519524SHoward Hinnant__has_max_size_test(_Alloc&& __a)
14253e519524SHoward Hinnant    -> decltype(__a.max_size(), true_type());
14263e519524SHoward Hinnant
14273e519524SHoward Hinnanttemplate <class _Alloc>
14283e519524SHoward Hinnantauto
14293e519524SHoward Hinnant__has_max_size_test(const volatile _Alloc& __a)
14303e519524SHoward Hinnant    -> false_type;
14313e519524SHoward Hinnant
14323e519524SHoward Hinnanttemplate <class _Alloc>
14333e519524SHoward Hinnantstruct __has_max_size
14343e519524SHoward Hinnant    : integral_constant<bool,
14353e519524SHoward Hinnant        is_same<
14369f8fef95SEric Fiselier            decltype(_VSTD::__has_max_size_test(declval<_Alloc&>())),
14373e519524SHoward Hinnant            true_type>::value>
14383e519524SHoward Hinnant{
14393e519524SHoward Hinnant};
14403e519524SHoward Hinnant
14413e519524SHoward Hinnanttemplate <class _Alloc>
14423e519524SHoward Hinnantauto
14433e519524SHoward Hinnant__has_select_on_container_copy_construction_test(_Alloc&& __a)
14443e519524SHoward Hinnant    -> decltype(__a.select_on_container_copy_construction(), true_type());
14453e519524SHoward Hinnant
14463e519524SHoward Hinnanttemplate <class _Alloc>
14473e519524SHoward Hinnantauto
14483e519524SHoward Hinnant__has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
14493e519524SHoward Hinnant    -> false_type;
14503e519524SHoward Hinnant
14513e519524SHoward Hinnanttemplate <class _Alloc>
14523e519524SHoward Hinnantstruct __has_select_on_container_copy_construction
14533e519524SHoward Hinnant    : integral_constant<bool,
14543e519524SHoward Hinnant        is_same<
14559f8fef95SEric Fiselier            decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
14563e519524SHoward Hinnant            true_type>::value>
14573e519524SHoward Hinnant{
14583e519524SHoward Hinnant};
14593e519524SHoward Hinnant
146054613ab4SEric Fiselier#else  // _LIBCPP_CXX03_LANG
14613e519524SHoward Hinnant
1462e7652f5cSVolodymyr Sapsaitemplate <class _Alloc, class _Pointer, class _Tp, class = void>
1463e7652f5cSVolodymyr Sapsaistruct __has_construct : std::false_type {};
14643e519524SHoward Hinnant
1465e7652f5cSVolodymyr Sapsaitemplate <class _Alloc, class _Pointer, class _Tp>
1466e7652f5cSVolodymyr Sapsaistruct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
1467e7652f5cSVolodymyr Sapsai    decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
1468e7652f5cSVolodymyr Sapsai>::type> : std::true_type {};
14693e519524SHoward Hinnant
1470e7652f5cSVolodymyr Sapsaitemplate <class _Alloc, class _Pointer, class = void>
1471e7652f5cSVolodymyr Sapsaistruct __has_destroy : false_type {};
14723e519524SHoward Hinnant
14733e519524SHoward Hinnanttemplate <class _Alloc, class _Pointer>
1474e7652f5cSVolodymyr Sapsaistruct __has_destroy<_Alloc, _Pointer, typename __void_t<
1475e7652f5cSVolodymyr Sapsai    decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
1476e7652f5cSVolodymyr Sapsai>::type> : std::true_type {};
14773e519524SHoward Hinnant
14783e519524SHoward Hinnanttemplate <class _Alloc>
14793e519524SHoward Hinnantstruct __has_max_size
14803e519524SHoward Hinnant    : true_type
14813e519524SHoward Hinnant{
14823e519524SHoward Hinnant};
14833e519524SHoward Hinnant
14843e519524SHoward Hinnanttemplate <class _Alloc>
14853e519524SHoward Hinnantstruct __has_select_on_container_copy_construction
14863e519524SHoward Hinnant    : false_type
14873e519524SHoward Hinnant{
14883e519524SHoward Hinnant};
14893e519524SHoward Hinnant
149054613ab4SEric Fiselier#endif  // _LIBCPP_CXX03_LANG
14913e519524SHoward Hinnant
1492a4a1ef1fSHoward Hinnanttemplate <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
1493a4a1ef1fSHoward Hinnantstruct __alloc_traits_difference_type
1494a4a1ef1fSHoward Hinnant{
149514d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
1496a4a1ef1fSHoward Hinnant};
1497a4a1ef1fSHoward Hinnant
1498a4a1ef1fSHoward Hinnanttemplate <class _Alloc, class _Ptr>
1499a4a1ef1fSHoward Hinnantstruct __alloc_traits_difference_type<_Alloc, _Ptr, true>
1500a4a1ef1fSHoward Hinnant{
150114d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
1502a4a1ef1fSHoward Hinnant};
1503a4a1ef1fSHoward Hinnant
1504fb0e1908SVolodymyr Sapsaitemplate <class _Tp>
1505fb0e1908SVolodymyr Sapsaistruct __is_default_allocator : false_type {};
1506fb0e1908SVolodymyr Sapsai
1507fb0e1908SVolodymyr Sapsaitemplate <class _Tp>
1508fb0e1908SVolodymyr Sapsaistruct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
1509fb0e1908SVolodymyr Sapsai
15103e519524SHoward Hinnanttemplate <class _Alloc>
1511e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS allocator_traits
15123e519524SHoward Hinnant{
15133e519524SHoward Hinnant    typedef _Alloc                              allocator_type;
15143e519524SHoward Hinnant    typedef typename allocator_type::value_type value_type;
15153e519524SHoward Hinnant
15163e519524SHoward Hinnant    typedef typename __pointer_type<value_type, allocator_type>::type pointer;
15173e519524SHoward Hinnant    typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
15183e519524SHoward Hinnant    typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
15193e519524SHoward Hinnant    typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
15203e519524SHoward Hinnant
1521a4a1ef1fSHoward Hinnant    typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
1522a4a1ef1fSHoward Hinnant    typedef typename __size_type<allocator_type, difference_type>::type size_type;
15233e519524SHoward Hinnant
15243e519524SHoward Hinnant    typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
15253e519524SHoward Hinnant                     propagate_on_container_copy_assignment;
15263e519524SHoward Hinnant    typedef typename __propagate_on_container_move_assignment<allocator_type>::type
15273e519524SHoward Hinnant                     propagate_on_container_move_assignment;
15283e519524SHoward Hinnant    typedef typename __propagate_on_container_swap<allocator_type>::type
15293e519524SHoward Hinnant                     propagate_on_container_swap;
153031a47313SMarshall Clow    typedef typename __is_always_equal<allocator_type>::type
153131a47313SMarshall Clow                     is_always_equal;
15323e519524SHoward Hinnant
153354613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
15343e519524SHoward Hinnant    template <class _Tp> using rebind_alloc =
1535f7384975SHoward Hinnant                  typename __allocator_traits_rebind<allocator_type, _Tp>::type;
15363e519524SHoward Hinnant    template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
153754613ab4SEric Fiselier#else  // _LIBCPP_CXX03_LANG
15383e519524SHoward Hinnant    template <class _Tp> struct rebind_alloc
15393e519524SHoward Hinnant        {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
15403e519524SHoward Hinnant    template <class _Tp> struct rebind_traits
15413e519524SHoward Hinnant        {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
154254613ab4SEric Fiselier#endif  // _LIBCPP_CXX03_LANG
15433e519524SHoward Hinnant
15443fddff51SMarshall Clow    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
15453e519524SHoward Hinnant    static pointer allocate(allocator_type& __a, size_type __n)
15463e519524SHoward Hinnant        {return __a.allocate(__n);}
15473fddff51SMarshall Clow    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
15483e519524SHoward Hinnant    static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
15499ffacf3dSEric Fiselier        {return __allocate(__a, __n, __hint,
15503e519524SHoward Hinnant            __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
15513e519524SHoward Hinnant
1552848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
15533739fe79SHoward Hinnant    static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
15543e519524SHoward Hinnant        {__a.deallocate(__p, __n);}
15553e519524SHoward Hinnant
15563e519524SHoward Hinnant#ifndef _LIBCPP_HAS_NO_VARIADICS
15573e519524SHoward Hinnant    template <class _Tp, class... _Args>
1558848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
15593e519524SHoward Hinnant        static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
1560981f31acSMarshall Clow            {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
1561ce48a113SHoward Hinnant                         __a, __p, _VSTD::forward<_Args>(__args)...);}
1562b3371f6fSHoward Hinnant#else  // _LIBCPP_HAS_NO_VARIADICS
15633e519524SHoward Hinnant    template <class _Tp>
1564848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1565a595b7f0SEric Fiselier        static void construct(allocator_type&, _Tp* __p)
15663e519524SHoward Hinnant            {
15673e519524SHoward Hinnant                ::new ((void*)__p) _Tp();
15683e519524SHoward Hinnant            }
15693e519524SHoward Hinnant    template <class _Tp, class _A0>
1570848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1571e7652f5cSVolodymyr Sapsai        static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
15723e519524SHoward Hinnant            {
1573e7652f5cSVolodymyr Sapsai                __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
1574e7652f5cSVolodymyr Sapsai                            __a, __p, __a0);
15753e519524SHoward Hinnant            }
15763e519524SHoward Hinnant    template <class _Tp, class _A0, class _A1>
1577848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1578a595b7f0SEric Fiselier        static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
15793e519524SHoward Hinnant                              const _A1& __a1)
15803e519524SHoward Hinnant            {
15813e519524SHoward Hinnant                ::new ((void*)__p) _Tp(__a0, __a1);
15823e519524SHoward Hinnant            }
15833e519524SHoward Hinnant    template <class _Tp, class _A0, class _A1, class _A2>
1584848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
1585a595b7f0SEric Fiselier        static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
15863e519524SHoward Hinnant                              const _A1& __a1, const _A2& __a2)
15873e519524SHoward Hinnant            {
15883e519524SHoward Hinnant                ::new ((void*)__p) _Tp(__a0, __a1, __a2);
15893e519524SHoward Hinnant            }
1590b3371f6fSHoward Hinnant#endif  // _LIBCPP_HAS_NO_VARIADICS
15913e519524SHoward Hinnant
15923e519524SHoward Hinnant    template <class _Tp>
1593848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
15943e519524SHoward Hinnant        static void destroy(allocator_type& __a, _Tp* __p)
15953e519524SHoward Hinnant            {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
15963e519524SHoward Hinnant
1597848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
159806fbed03SMarshall Clow    static size_type max_size(const allocator_type& __a) _NOEXCEPT
15993e519524SHoward Hinnant        {return __max_size(__has_max_size<const allocator_type>(), __a);}
16003e519524SHoward Hinnant
1601848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
16023e519524SHoward Hinnant    static allocator_type
16033e519524SHoward Hinnant        select_on_container_copy_construction(const allocator_type& __a)
16049ffacf3dSEric Fiselier            {return __select_on_container_copy_construction(
16053e519524SHoward Hinnant                __has_select_on_container_copy_construction<const allocator_type>(),
16063e519524SHoward Hinnant                __a);}
16073e519524SHoward Hinnant
16089741d6c9SHoward Hinnant    template <class _Ptr>
16099741d6c9SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
16109741d6c9SHoward Hinnant        static
16119741d6c9SHoward Hinnant        void
16129741d6c9SHoward Hinnant        __construct_forward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
16139741d6c9SHoward Hinnant        {
1614036a9a04SLouis Dionne            for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
16159741d6c9SHoward Hinnant                construct(__a, _VSTD::__to_raw_pointer(__begin2), _VSTD::move_if_noexcept(*__begin1));
16169741d6c9SHoward Hinnant        }
16179741d6c9SHoward Hinnant
16189741d6c9SHoward Hinnant    template <class _Tp>
16199741d6c9SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
16209741d6c9SHoward Hinnant        static
16219741d6c9SHoward Hinnant        typename enable_if
16229741d6c9SHoward Hinnant        <
1623fb0e1908SVolodymyr Sapsai            (__is_default_allocator<allocator_type>::value
16249741d6c9SHoward Hinnant                || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
16259741d6c9SHoward Hinnant             is_trivially_move_constructible<_Tp>::value,
16269741d6c9SHoward Hinnant            void
16279741d6c9SHoward Hinnant        >::type
1628fd838227SEric Fiselier        __construct_forward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
16299741d6c9SHoward Hinnant        {
16309741d6c9SHoward Hinnant            ptrdiff_t _Np = __end1 - __begin1;
163187601befSMarshall Clow            if (_Np > 0)
1632e6230742SMarshall Clow            {
16339741d6c9SHoward Hinnant                _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
16349741d6c9SHoward Hinnant                __begin2 += _Np;
16359741d6c9SHoward Hinnant            }
1636e6230742SMarshall Clow        }
16379741d6c9SHoward Hinnant
1638e782178eSEric Fiselier    template <class _Iter, class _Ptr>
1639e782178eSEric Fiselier        _LIBCPP_INLINE_VISIBILITY
1640e782178eSEric Fiselier        static
1641e782178eSEric Fiselier        void
1642e782178eSEric Fiselier        __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
1643e782178eSEric Fiselier        {
1644e782178eSEric Fiselier            for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
1645e782178eSEric Fiselier                construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
1646e782178eSEric Fiselier        }
1647e782178eSEric Fiselier
1648fb0e1908SVolodymyr Sapsai    template <class _SourceTp, class _DestTp,
1649fb0e1908SVolodymyr Sapsai              class _RawSourceTp = typename remove_const<_SourceTp>::type,
1650fb0e1908SVolodymyr Sapsai              class _RawDestTp = typename remove_const<_DestTp>::type>
1651e782178eSEric Fiselier        _LIBCPP_INLINE_VISIBILITY
1652e782178eSEric Fiselier        static
1653e782178eSEric Fiselier        typename enable_if
1654e782178eSEric Fiselier        <
1655fb0e1908SVolodymyr Sapsai            is_trivially_move_constructible<_DestTp>::value &&
1656fb0e1908SVolodymyr Sapsai            is_same<_RawSourceTp, _RawDestTp>::value &&
1657fb0e1908SVolodymyr Sapsai            (__is_default_allocator<allocator_type>::value ||
1658fb0e1908SVolodymyr Sapsai             !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
1659e782178eSEric Fiselier            void
1660e782178eSEric Fiselier        >::type
1661fb0e1908SVolodymyr Sapsai        __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
1662e782178eSEric Fiselier        {
1663e782178eSEric Fiselier            ptrdiff_t _Np = __end1 - __begin1;
166487601befSMarshall Clow            if (_Np > 0)
1665e6230742SMarshall Clow            {
1666fb0e1908SVolodymyr Sapsai                _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
1667e782178eSEric Fiselier                __begin2 += _Np;
1668e782178eSEric Fiselier            }
1669e6230742SMarshall Clow        }
1670e782178eSEric Fiselier
16719741d6c9SHoward Hinnant    template <class _Ptr>
16729741d6c9SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
16739741d6c9SHoward Hinnant        static
16749741d6c9SHoward Hinnant        void
16759741d6c9SHoward Hinnant        __construct_backward(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
16769741d6c9SHoward Hinnant        {
16779741d6c9SHoward Hinnant            while (__end1 != __begin1)
16788d9aec88SHoward Hinnant            {
16798d9aec88SHoward Hinnant                construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
16808d9aec88SHoward Hinnant                --__end2;
16818d9aec88SHoward Hinnant            }
16829741d6c9SHoward Hinnant        }
16839741d6c9SHoward Hinnant
16849741d6c9SHoward Hinnant    template <class _Tp>
16859741d6c9SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
16869741d6c9SHoward Hinnant        static
16879741d6c9SHoward Hinnant        typename enable_if
16889741d6c9SHoward Hinnant        <
1689fb0e1908SVolodymyr Sapsai            (__is_default_allocator<allocator_type>::value
16909741d6c9SHoward Hinnant                || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
16919741d6c9SHoward Hinnant             is_trivially_move_constructible<_Tp>::value,
16929741d6c9SHoward Hinnant            void
16939741d6c9SHoward Hinnant        >::type
1694fd838227SEric Fiselier        __construct_backward(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
16959741d6c9SHoward Hinnant        {
16969741d6c9SHoward Hinnant            ptrdiff_t _Np = __end1 - __begin1;
16979741d6c9SHoward Hinnant            __end2 -= _Np;
169887601befSMarshall Clow            if (_Np > 0)
16999741d6c9SHoward Hinnant                _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
17009741d6c9SHoward Hinnant        }
17019741d6c9SHoward Hinnant
17023e519524SHoward Hinnantprivate:
17033e519524SHoward Hinnant
1704848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
17059ffacf3dSEric Fiselier    static pointer __allocate(allocator_type& __a, size_type __n,
17063e519524SHoward Hinnant        const_void_pointer __hint, true_type)
17073e519524SHoward Hinnant        {return __a.allocate(__n, __hint);}
1708848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
17099ffacf3dSEric Fiselier    static pointer __allocate(allocator_type& __a, size_type __n,
1710c206366fSHoward Hinnant        const_void_pointer, false_type)
17113e519524SHoward Hinnant        {return __a.allocate(__n);}
17123e519524SHoward Hinnant
17133e519524SHoward Hinnant#ifndef _LIBCPP_HAS_NO_VARIADICS
17143e519524SHoward Hinnant    template <class _Tp, class... _Args>
1715848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
17163e519524SHoward Hinnant        static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
1717ce48a113SHoward Hinnant            {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
17183e519524SHoward Hinnant    template <class _Tp, class... _Args>
1719848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
17203e519524SHoward Hinnant        static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
17213e519524SHoward Hinnant            {
1722ce48a113SHoward Hinnant                ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
17233e519524SHoward Hinnant            }
1724e7652f5cSVolodymyr Sapsai#else  // _LIBCPP_HAS_NO_VARIADICS
1725e7652f5cSVolodymyr Sapsai    template <class _Tp, class _A0>
1726e7652f5cSVolodymyr Sapsai        _LIBCPP_INLINE_VISIBILITY
1727e7652f5cSVolodymyr Sapsai        static void __construct(true_type, allocator_type& __a, _Tp* __p,
1728e7652f5cSVolodymyr Sapsai                                const _A0& __a0)
1729e7652f5cSVolodymyr Sapsai            {__a.construct(__p, __a0);}
1730e7652f5cSVolodymyr Sapsai    template <class _Tp, class _A0>
1731e7652f5cSVolodymyr Sapsai        _LIBCPP_INLINE_VISIBILITY
1732e7652f5cSVolodymyr Sapsai        static void __construct(false_type, allocator_type&, _Tp* __p,
1733e7652f5cSVolodymyr Sapsai                                const _A0& __a0)
1734e7652f5cSVolodymyr Sapsai            {
1735e7652f5cSVolodymyr Sapsai                ::new ((void*)__p) _Tp(__a0);
1736e7652f5cSVolodymyr Sapsai            }
1737b3371f6fSHoward Hinnant#endif  // _LIBCPP_HAS_NO_VARIADICS
17383e519524SHoward Hinnant
17393e519524SHoward Hinnant    template <class _Tp>
1740848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
17413e519524SHoward Hinnant        static void __destroy(true_type, allocator_type& __a, _Tp* __p)
17423e519524SHoward Hinnant            {__a.destroy(__p);}
17433e519524SHoward Hinnant    template <class _Tp>
1744848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
17453e519524SHoward Hinnant        static void __destroy(false_type, allocator_type&, _Tp* __p)
17463e519524SHoward Hinnant            {
17473e519524SHoward Hinnant                __p->~_Tp();
17483e519524SHoward Hinnant            }
17493e519524SHoward Hinnant
1750848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1751fa40ee77SMarshall Clow    static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
17523e519524SHoward Hinnant            {return __a.max_size();}
1753848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
1754fa40ee77SMarshall Clow    static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
1755b631c243SMarshall Clow            {return numeric_limits<size_type>::max() / sizeof(value_type);}
17563e519524SHoward Hinnant
1757848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
17583e519524SHoward Hinnant    static allocator_type
17599ffacf3dSEric Fiselier        __select_on_container_copy_construction(true_type, const allocator_type& __a)
17603e519524SHoward Hinnant            {return __a.select_on_container_copy_construction();}
1761848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
17623e519524SHoward Hinnant    static allocator_type
17639ffacf3dSEric Fiselier        __select_on_container_copy_construction(false_type, const allocator_type& __a)
17643e519524SHoward Hinnant            {return __a;}
17653e519524SHoward Hinnant};
17663e519524SHoward Hinnant
17671f508014SMarshall Clowtemplate <class _Traits, class _Tp>
17681f508014SMarshall Clowstruct __rebind_alloc_helper
17691f508014SMarshall Clow{
177054613ab4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
177114d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp>        type;
17721f508014SMarshall Clow#else
17731f508014SMarshall Clow    typedef typename _Traits::template rebind_alloc<_Tp>::other type;
17741f508014SMarshall Clow#endif
17751f508014SMarshall Clow};
17761f508014SMarshall Clow
17773e519524SHoward Hinnant// allocator
17783e519524SHoward Hinnant
17793e519524SHoward Hinnanttemplate <class _Tp>
1780e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS allocator
17813e519524SHoward Hinnant{
17823e519524SHoward Hinnantpublic:
17833e519524SHoward Hinnant    typedef size_t            size_type;
17843e519524SHoward Hinnant    typedef ptrdiff_t         difference_type;
17853e519524SHoward Hinnant    typedef _Tp*              pointer;
17863e519524SHoward Hinnant    typedef const _Tp*        const_pointer;
17873e519524SHoward Hinnant    typedef _Tp&              reference;
17883e519524SHoward Hinnant    typedef const _Tp&        const_reference;
17893e519524SHoward Hinnant    typedef _Tp               value_type;
17903e519524SHoward Hinnant
1791b58f59cdSHoward Hinnant    typedef true_type propagate_on_container_move_assignment;
179231a47313SMarshall Clow    typedef true_type is_always_equal;
1793b58f59cdSHoward Hinnant
17943e519524SHoward Hinnant    template <class _Up> struct rebind {typedef allocator<_Up> other;};
17953e519524SHoward Hinnant
1796e0742c4aSMarshall Clow    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1797e0742c4aSMarshall Clow    allocator() _NOEXCEPT {}
1798e0742c4aSMarshall Clow
1799e0742c4aSMarshall Clow    template <class _Up>
1800e0742c4aSMarshall Clow    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1801e0742c4aSMarshall Clow    allocator(const allocator<_Up>&) _NOEXCEPT {}
1802e0742c4aSMarshall Clow
18033739fe79SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
1804ce48a113SHoward Hinnant        {return _VSTD::addressof(__x);}
18053739fe79SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
1806ce48a113SHoward Hinnant        {return _VSTD::addressof(__x);}
18073fddff51SMarshall Clow    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
18083fddff51SMarshall Clow    pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
18094cec709eSMarshall Clow        {
18104cec709eSMarshall Clow        if (__n > max_size())
18110fc8cec7SMarshall Clow            __throw_length_error("allocator<T>::allocate(size_t n)"
18120fc8cec7SMarshall Clow                                 " 'n' exceeds maximum supported size");
1813d108bf85SEric Fiselier        return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
18144cec709eSMarshall Clow        }
1815f25b75b9SEric Fiselier    _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
1816d108bf85SEric Fiselier        {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
18173739fe79SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
18183739fe79SHoward Hinnant        {return size_type(~0) / sizeof(_Tp);}
18197609c9b6SHoward Hinnant#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
18203e519524SHoward Hinnant    template <class _Up, class... _Args>
18213e519524SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
18223e519524SHoward Hinnant        void
18233e519524SHoward Hinnant        construct(_Up* __p, _Args&&... __args)
18243e519524SHoward Hinnant        {
1825ce48a113SHoward Hinnant            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
18263e519524SHoward Hinnant        }
18277609c9b6SHoward Hinnant#else  // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
18283e519524SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
18293e519524SHoward Hinnant        void
18303e519524SHoward Hinnant        construct(pointer __p)
18313e519524SHoward Hinnant        {
18323e519524SHoward Hinnant            ::new((void*)__p) _Tp();
18333e519524SHoward Hinnant        }
1834f5799be4SMichael J. Spencer# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1835bff1bfc6SHoward Hinnant
18363e519524SHoward Hinnant    template <class _A0>
18373e519524SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
18383e519524SHoward Hinnant        void
18393e519524SHoward Hinnant        construct(pointer __p, _A0& __a0)
18403e519524SHoward Hinnant        {
18413e519524SHoward Hinnant            ::new((void*)__p) _Tp(__a0);
18423e519524SHoward Hinnant        }
18433e519524SHoward Hinnant    template <class _A0>
18443e519524SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
18453e519524SHoward Hinnant        void
18463e519524SHoward Hinnant        construct(pointer __p, const _A0& __a0)
18473e519524SHoward Hinnant        {
18483e519524SHoward Hinnant            ::new((void*)__p) _Tp(__a0);
18493e519524SHoward Hinnant        }
1850f5799be4SMichael J. Spencer# endif  // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
18513e519524SHoward Hinnant    template <class _A0, class _A1>
18523e519524SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
18533e519524SHoward Hinnant        void
18543e519524SHoward Hinnant        construct(pointer __p, _A0& __a0, _A1& __a1)
18553e519524SHoward Hinnant        {
18563e519524SHoward Hinnant            ::new((void*)__p) _Tp(__a0, __a1);
18573e519524SHoward Hinnant        }
18583e519524SHoward Hinnant    template <class _A0, class _A1>
18593e519524SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
18603e519524SHoward Hinnant        void
18613e519524SHoward Hinnant        construct(pointer __p, const _A0& __a0, _A1& __a1)
18623e519524SHoward Hinnant        {
18633e519524SHoward Hinnant            ::new((void*)__p) _Tp(__a0, __a1);
18643e519524SHoward Hinnant        }
18653e519524SHoward Hinnant    template <class _A0, class _A1>
18663e519524SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
18673e519524SHoward Hinnant        void
18683e519524SHoward Hinnant        construct(pointer __p, _A0& __a0, const _A1& __a1)
18693e519524SHoward Hinnant        {
18703e519524SHoward Hinnant            ::new((void*)__p) _Tp(__a0, __a1);
18713e519524SHoward Hinnant        }
18723e519524SHoward Hinnant    template <class _A0, class _A1>
18733e519524SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
18743e519524SHoward Hinnant        void
18753e519524SHoward Hinnant        construct(pointer __p, const _A0& __a0, const _A1& __a1)
18763e519524SHoward Hinnant        {
18773e519524SHoward Hinnant            ::new((void*)__p) _Tp(__a0, __a1);
18783e519524SHoward Hinnant        }
18797609c9b6SHoward Hinnant#endif  // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
18803e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
18813e519524SHoward Hinnant};
18823e519524SHoward Hinnant
18838e25104bSHoward Hinnanttemplate <class _Tp>
1884e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
18858e25104bSHoward Hinnant{
18868e25104bSHoward Hinnantpublic:
18878e25104bSHoward Hinnant    typedef size_t            size_type;
18888e25104bSHoward Hinnant    typedef ptrdiff_t         difference_type;
18898e25104bSHoward Hinnant    typedef const _Tp*        pointer;
18908e25104bSHoward Hinnant    typedef const _Tp*        const_pointer;
18918e25104bSHoward Hinnant    typedef const _Tp&        reference;
18928e25104bSHoward Hinnant    typedef const _Tp&        const_reference;
189330444adcSHoward Hinnant    typedef const _Tp         value_type;
18948e25104bSHoward Hinnant
18958e25104bSHoward Hinnant    typedef true_type propagate_on_container_move_assignment;
1896f43a42d5SMarshall Clow    typedef true_type is_always_equal;
18978e25104bSHoward Hinnant
18988e25104bSHoward Hinnant    template <class _Up> struct rebind {typedef allocator<_Up> other;};
18998e25104bSHoward Hinnant
1900e0742c4aSMarshall Clow    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1901e0742c4aSMarshall Clow    allocator() _NOEXCEPT {}
1902e0742c4aSMarshall Clow
1903e0742c4aSMarshall Clow    template <class _Up>
1904e0742c4aSMarshall Clow    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1905e0742c4aSMarshall Clow    allocator(const allocator<_Up>&) _NOEXCEPT {}
1906e0742c4aSMarshall Clow
19078e25104bSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
19088e25104bSHoward Hinnant        {return _VSTD::addressof(__x);}
19098e25104bSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
19104cec709eSMarshall Clow    {
19114cec709eSMarshall Clow        if (__n > max_size())
19120fc8cec7SMarshall Clow            __throw_length_error("allocator<const T>::allocate(size_t n)"
19130fc8cec7SMarshall Clow                                 " 'n' exceeds maximum supported size");
1914d108bf85SEric Fiselier        return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
19154cec709eSMarshall Clow    }
1916f25b75b9SEric Fiselier    _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
1917d108bf85SEric Fiselier        {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
19188e25104bSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
19198e25104bSHoward Hinnant        {return size_type(~0) / sizeof(_Tp);}
19208e25104bSHoward Hinnant#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
19218e25104bSHoward Hinnant    template <class _Up, class... _Args>
19228e25104bSHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
19238e25104bSHoward Hinnant        void
19248e25104bSHoward Hinnant        construct(_Up* __p, _Args&&... __args)
19258e25104bSHoward Hinnant        {
19268e25104bSHoward Hinnant            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
19278e25104bSHoward Hinnant        }
19288e25104bSHoward Hinnant#else  // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
19298e25104bSHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
19308e25104bSHoward Hinnant        void
19318e25104bSHoward Hinnant        construct(pointer __p)
19328e25104bSHoward Hinnant        {
19334a911c86SMarshall Clow            ::new((void*) const_cast<_Tp *>(__p)) _Tp();
19348e25104bSHoward Hinnant        }
19358e25104bSHoward Hinnant# if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
1936bff1bfc6SHoward Hinnant
19378e25104bSHoward Hinnant    template <class _A0>
19388e25104bSHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
19398e25104bSHoward Hinnant        void
19408e25104bSHoward Hinnant        construct(pointer __p, _A0& __a0)
19418e25104bSHoward Hinnant        {
19424a911c86SMarshall Clow            ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
19438e25104bSHoward Hinnant        }
19448e25104bSHoward Hinnant    template <class _A0>
19458e25104bSHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
19468e25104bSHoward Hinnant        void
19478e25104bSHoward Hinnant        construct(pointer __p, const _A0& __a0)
19488e25104bSHoward Hinnant        {
19494a911c86SMarshall Clow            ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
19508e25104bSHoward Hinnant        }
19518e25104bSHoward Hinnant# endif  // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
19528e25104bSHoward Hinnant    template <class _A0, class _A1>
19538e25104bSHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
19548e25104bSHoward Hinnant        void
19558e25104bSHoward Hinnant        construct(pointer __p, _A0& __a0, _A1& __a1)
19568e25104bSHoward Hinnant        {
19574a911c86SMarshall Clow            ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
19588e25104bSHoward Hinnant        }
19598e25104bSHoward Hinnant    template <class _A0, class _A1>
19608e25104bSHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
19618e25104bSHoward Hinnant        void
19628e25104bSHoward Hinnant        construct(pointer __p, const _A0& __a0, _A1& __a1)
19638e25104bSHoward Hinnant        {
19644a911c86SMarshall Clow            ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
19658e25104bSHoward Hinnant        }
19668e25104bSHoward Hinnant    template <class _A0, class _A1>
19678e25104bSHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
19688e25104bSHoward Hinnant        void
19698e25104bSHoward Hinnant        construct(pointer __p, _A0& __a0, const _A1& __a1)
19708e25104bSHoward Hinnant        {
19714a911c86SMarshall Clow            ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
19728e25104bSHoward Hinnant        }
19738e25104bSHoward Hinnant    template <class _A0, class _A1>
19748e25104bSHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
19758e25104bSHoward Hinnant        void
19768e25104bSHoward Hinnant        construct(pointer __p, const _A0& __a0, const _A1& __a1)
19778e25104bSHoward Hinnant        {
19784a911c86SMarshall Clow            ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
19798e25104bSHoward Hinnant        }
19808e25104bSHoward Hinnant#endif  // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
19818e25104bSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
19828e25104bSHoward Hinnant};
19838e25104bSHoward Hinnant
19843e519524SHoward Hinnanttemplate <class _Tp, class _Up>
19853e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
19863739fe79SHoward Hinnantbool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
19873e519524SHoward Hinnant
19883e519524SHoward Hinnanttemplate <class _Tp, class _Up>
19893e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
19903739fe79SHoward Hinnantbool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
19913e519524SHoward Hinnant
19923e519524SHoward Hinnanttemplate <class _OutputIterator, class _Tp>
1993e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS raw_storage_iterator
19943e519524SHoward Hinnant    : public iterator<output_iterator_tag,
19953e519524SHoward Hinnant                      _Tp,                                         // purposefully not C++03
19963e519524SHoward Hinnant                      ptrdiff_t,                                   // purposefully not C++03
19973e519524SHoward Hinnant                      _Tp*,                                        // purposefully not C++03
19983e519524SHoward Hinnant                      raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
19993e519524SHoward Hinnant{
20003e519524SHoward Hinnantprivate:
20013e519524SHoward Hinnant    _OutputIterator __x_;
20023e519524SHoward Hinnantpublic:
20033e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
20043e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
20053e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
2006fa8ce345SMarshall Clow        {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
20072603b075SMarshall Clow#if _LIBCPP_STD_VER >= 14
20082603b075SMarshall Clow    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
2009fa8ce345SMarshall Clow        {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
20102603b075SMarshall Clow#endif
20113e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
20123e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY raw_storage_iterator  operator++(int)
20133e519524SHoward Hinnant        {raw_storage_iterator __t(*this); ++__x_; return __t;}
2014783b372aSMarshall Clow#if _LIBCPP_STD_VER >= 14
2015783b372aSMarshall Clow    _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
2016783b372aSMarshall Clow#endif
20173e519524SHoward Hinnant};
20183e519524SHoward Hinnant
20193e519524SHoward Hinnanttemplate <class _Tp>
2020c65d39a4SRoman Lebedev_LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
20213e519524SHoward Hinnantpair<_Tp*, ptrdiff_t>
20223739fe79SHoward Hinnantget_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
20233e519524SHoward Hinnant{
20243e519524SHoward Hinnant    pair<_Tp*, ptrdiff_t> __r(0, 0);
20253e519524SHoward Hinnant    const ptrdiff_t __m = (~ptrdiff_t(0) ^
20263e519524SHoward Hinnant                           ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
20273e519524SHoward Hinnant                           / sizeof(_Tp);
20283e519524SHoward Hinnant    if (__n > __m)
20293e519524SHoward Hinnant        __n = __m;
20303e519524SHoward Hinnant    while (__n > 0)
20313e519524SHoward Hinnant    {
203243245b1fSEric Fiselier#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
2033d108bf85SEric Fiselier    if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
20344e3195c9SRichard Smith        {
20354e3195c9SRichard Smith            std::align_val_t __al =
20364e3195c9SRichard Smith                std::align_val_t(std::alignment_of<_Tp>::value);
20374e3195c9SRichard Smith            __r.first = static_cast<_Tp*>(::operator new(
20384e3195c9SRichard Smith                __n * sizeof(_Tp), __al, nothrow));
20394e3195c9SRichard Smith        } else {
20404e3195c9SRichard Smith            __r.first = static_cast<_Tp*>(::operator new(
20414e3195c9SRichard Smith                __n * sizeof(_Tp), nothrow));
20424e3195c9SRichard Smith        }
20434e3195c9SRichard Smith#else
2044d108bf85SEric Fiselier    if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
20454e3195c9SRichard Smith        {
20464e3195c9SRichard Smith            // Since aligned operator new is unavailable, return an empty
20474e3195c9SRichard Smith            // buffer rather than one with invalid alignment.
20484e3195c9SRichard Smith            return __r;
20494e3195c9SRichard Smith        }
20504e3195c9SRichard Smith
20513e519524SHoward Hinnant        __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
20524e3195c9SRichard Smith#endif
20534e3195c9SRichard Smith
20543e519524SHoward Hinnant        if (__r.first)
20553e519524SHoward Hinnant        {
20563e519524SHoward Hinnant            __r.second = __n;
20573e519524SHoward Hinnant            break;
20583e519524SHoward Hinnant        }
20593e519524SHoward Hinnant        __n /= 2;
20603e519524SHoward Hinnant    }
20613e519524SHoward Hinnant    return __r;
20623e519524SHoward Hinnant}
20633e519524SHoward Hinnant
20643e519524SHoward Hinnanttemplate <class _Tp>
20653e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
20664e3195c9SRichard Smithvoid return_temporary_buffer(_Tp* __p) _NOEXCEPT
20674e3195c9SRichard Smith{
2068d108bf85SEric Fiselier  _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
20694e3195c9SRichard Smith}
20703e519524SHoward Hinnant
2071e67179bcSMarshall Clow#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
20723e519524SHoward Hinnanttemplate <class _Tp>
2073ea5cd3b4SLouis Dionnestruct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
20743e519524SHoward Hinnant{
20753e519524SHoward Hinnant    _Tp* __ptr_;
20763e519524SHoward Hinnant};
20773e519524SHoward Hinnant
20783e519524SHoward Hinnanttemplate<class _Tp>
2079ea5cd3b4SLouis Dionneclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
20803e519524SHoward Hinnant{
20813e519524SHoward Hinnantprivate:
20823e519524SHoward Hinnant    _Tp* __ptr_;
20833e519524SHoward Hinnantpublic:
20843e519524SHoward Hinnant    typedef _Tp element_type;
20853e519524SHoward Hinnant
20863e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
20873e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
20883e519524SHoward Hinnant    template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
20893e519524SHoward Hinnant        : __ptr_(__p.release()) {}
20903e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
20913e519524SHoward Hinnant        {reset(__p.release()); return *this;}
20923e519524SHoward Hinnant    template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
20933e519524SHoward Hinnant        {reset(__p.release()); return *this;}
20943e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
20953e519524SHoward Hinnant        {reset(__p.__ptr_); return *this;}
20963e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
20973e519524SHoward Hinnant
20983e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
20993e519524SHoward Hinnant        {return *__ptr_;}
21003e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
21013e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
21023e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
21033e519524SHoward Hinnant    {
21043e519524SHoward Hinnant        _Tp* __t = __ptr_;
21053e519524SHoward Hinnant        __ptr_ = 0;
21063e519524SHoward Hinnant        return __t;
21073e519524SHoward Hinnant    }
21083e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
21093e519524SHoward Hinnant    {
21103e519524SHoward Hinnant        if (__ptr_ != __p)
21113e519524SHoward Hinnant            delete __ptr_;
21123e519524SHoward Hinnant        __ptr_ = __p;
21133e519524SHoward Hinnant    }
21143e519524SHoward Hinnant
21153e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
21163e519524SHoward Hinnant    template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
21173e519524SHoward Hinnant        {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
21183e519524SHoward Hinnant    template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
21193e519524SHoward Hinnant        {return auto_ptr<_Up>(release());}
21203e519524SHoward Hinnant};
21213e519524SHoward Hinnant
21223e519524SHoward Hinnanttemplate <>
2123ea5cd3b4SLouis Dionneclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
21243e519524SHoward Hinnant{
21253e519524SHoward Hinnantpublic:
21263e519524SHoward Hinnant    typedef void element_type;
21273e519524SHoward Hinnant};
2128e67179bcSMarshall Clow#endif
21293e519524SHoward Hinnant
2130c88580c4SEric Fiseliertemplate <class _Tp, int _Idx,
2131c88580c4SEric Fiselier          bool _CanBeEmptyBase =
2132c88580c4SEric Fiselier              is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
2133c88580c4SEric Fiselierstruct __compressed_pair_elem {
2134c88580c4SEric Fiselier  typedef _Tp _ParamT;
2135c88580c4SEric Fiselier  typedef _Tp& reference;
2136c88580c4SEric Fiselier  typedef const _Tp& const_reference;
21373e519524SHoward Hinnant
2138c88580c4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
21399dae972cSAlex Lorenz  _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
21403e519524SHoward Hinnant
2141c88580c4SEric Fiselier  template <class _Up, class = typename enable_if<
21428948264eSEric Fiselier      !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
21438948264eSEric Fiselier  >::type>
21449dae972cSAlex Lorenz  _LIBCPP_INLINE_VISIBILITY
21458948264eSEric Fiselier  constexpr explicit
2146c88580c4SEric Fiselier  __compressed_pair_elem(_Up&& __u)
21470b485f3eSEric Fiselier      : __value_(_VSTD::forward<_Up>(__u))
21480b485f3eSEric Fiselier    {
21490b485f3eSEric Fiselier    }
21503e519524SHoward Hinnant
2151c88580c4SEric Fiselier  template <class... _Args, size_t... _Indexes>
2152c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2153c88580c4SEric Fiselier  __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2154c88580c4SEric Fiselier                         __tuple_indices<_Indexes...>)
2155c88580c4SEric Fiselier      : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2156c88580c4SEric Fiselier#else
21579dae972cSAlex Lorenz  _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
21589dae972cSAlex Lorenz  _LIBCPP_INLINE_VISIBILITY
2159c88580c4SEric Fiselier  __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
2160c88580c4SEric Fiselier#endif
21613e519524SHoward Hinnant
21629dae972cSAlex Lorenz  _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
21639dae972cSAlex Lorenz  _LIBCPP_INLINE_VISIBILITY
2164c88580c4SEric Fiselier  const_reference __get() const _NOEXCEPT { return __value_; }
21653e519524SHoward Hinnant
21663e519524SHoward Hinnantprivate:
2167c88580c4SEric Fiselier  _Tp __value_;
21683e519524SHoward Hinnant};
21693e519524SHoward Hinnant
2170c88580c4SEric Fiseliertemplate <class _Tp, int _Idx>
2171c88580c4SEric Fiselierstruct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
2172c88580c4SEric Fiselier  typedef _Tp _ParamT;
2173c88580c4SEric Fiselier  typedef _Tp& reference;
2174c88580c4SEric Fiselier  typedef const _Tp& const_reference;
2175c88580c4SEric Fiselier  typedef _Tp __value_type;
21763e519524SHoward Hinnant
2177c88580c4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
21789dae972cSAlex Lorenz  _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
21793e519524SHoward Hinnant
2180c88580c4SEric Fiselier  template <class _Up, class = typename enable_if<
21818948264eSEric Fiselier        !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
21828948264eSEric Fiselier  >::type>
21839dae972cSAlex Lorenz  _LIBCPP_INLINE_VISIBILITY
21848948264eSEric Fiselier  constexpr explicit
2185c88580c4SEric Fiselier  __compressed_pair_elem(_Up&& __u)
21860b485f3eSEric Fiselier      : __value_type(_VSTD::forward<_Up>(__u))
21870b485f3eSEric Fiselier  {}
21883e519524SHoward Hinnant
2189c88580c4SEric Fiselier  template <class... _Args, size_t... _Indexes>
2190c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2191c88580c4SEric Fiselier  __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
2192c88580c4SEric Fiselier                         __tuple_indices<_Indexes...>)
2193c88580c4SEric Fiselier      : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
2194c88580c4SEric Fiselier#else
21959dae972cSAlex Lorenz  _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
21969dae972cSAlex Lorenz  _LIBCPP_INLINE_VISIBILITY
2197c88580c4SEric Fiselier  __compressed_pair_elem(_ParamT __p)
2198c88580c4SEric Fiselier      : __value_type(std::forward<_ParamT>(__p)) {}
2199c88580c4SEric Fiselier#endif
22003e519524SHoward Hinnant
22019dae972cSAlex Lorenz  _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
22029dae972cSAlex Lorenz  _LIBCPP_INLINE_VISIBILITY
2203c88580c4SEric Fiselier  const_reference __get() const _NOEXCEPT { return *this; }
22043e519524SHoward Hinnant};
22053e519524SHoward Hinnant
2206c88580c4SEric Fiselier// Tag used to construct the second element of the compressed pair.
2207c88580c4SEric Fiselierstruct __second_tag {};
2208c88580c4SEric Fiselier
22093e519524SHoward Hinnanttemplate <class _T1, class _T2>
2210c88580c4SEric Fiselierclass __compressed_pair : private __compressed_pair_elem<_T1, 0>,
2211c88580c4SEric Fiselier                          private __compressed_pair_elem<_T2, 1> {
221214d48692SEric Fiselier  typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
221314d48692SEric Fiselier  typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
2214c88580c4SEric Fiselier
2215c88580c4SEric Fiselier  // NOTE: This static assert should never fire because __compressed_pair
2216c88580c4SEric Fiselier  // is *almost never* used in a scenario where it's possible for T1 == T2.
2217c88580c4SEric Fiselier  // (The exception is std::function where it is possible that the function
2218c88580c4SEric Fiselier  //  object and the allocator have the same type).
2219b411e9ceSEric Fiselier  static_assert((!is_same<_T1, _T2>::value),
2220c88580c4SEric Fiselier    "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
2221c88580c4SEric Fiselier    "The current implementation is NOT ABI-compatible with the previous "
2222c88580c4SEric Fiselier    "implementation for this configuration");
2223c88580c4SEric Fiselier
22243e519524SHoward Hinnantpublic:
2225c88580c4SEric Fiselier#ifndef _LIBCPP_CXX03_LANG
22268948264eSEric Fiselier  template <bool _Dummy = true,
22278948264eSEric Fiselier      class = typename enable_if<
22288948264eSEric Fiselier          __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
22298948264eSEric Fiselier          __dependent_type<is_default_constructible<_T2>, _Dummy>::value
22308948264eSEric Fiselier      >::type
22318948264eSEric Fiselier  >
2232a87b5e34SHoward Hinnant  _LIBCPP_INLINE_VISIBILITY
22338948264eSEric Fiselier  constexpr __compressed_pair() {}
2234a87b5e34SHoward Hinnant
2235c88580c4SEric Fiselier  template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
2236c88580c4SEric Fiselier                                                   __compressed_pair>::value,
2237c88580c4SEric Fiselier                                          bool>::type = true>
2238c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY constexpr explicit
2239c88580c4SEric Fiselier  __compressed_pair(_Tp&& __t)
2240c88580c4SEric Fiselier      : _Base1(std::forward<_Tp>(__t)), _Base2() {}
2241a87b5e34SHoward Hinnant
2242c88580c4SEric Fiselier  template <class _Tp>
2243c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY constexpr
2244c88580c4SEric Fiselier  __compressed_pair(__second_tag, _Tp&& __t)
2245c88580c4SEric Fiselier      : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
2246a87b5e34SHoward Hinnant
2247c88580c4SEric Fiselier  template <class _U1, class _U2>
2248c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY constexpr
2249c88580c4SEric Fiselier  __compressed_pair(_U1&& __t1, _U2&& __t2)
2250c88580c4SEric Fiselier      : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
2251a87b5e34SHoward Hinnant
2252a87b5e34SHoward Hinnant  template <class... _Args1, class... _Args2>
2253c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2254a87b5e34SHoward Hinnant  __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
2255a87b5e34SHoward Hinnant                    tuple<_Args2...> __second_args)
2256c88580c4SEric Fiselier      : _Base1(__pc, _VSTD::move(__first_args),
2257c88580c4SEric Fiselier               typename __make_tuple_indices<sizeof...(_Args1)>::type()),
2258c88580c4SEric Fiselier        _Base2(__pc, _VSTD::move(__second_args),
2259c88580c4SEric Fiselier               typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
2260a87b5e34SHoward Hinnant
2261c88580c4SEric Fiselier#else
2262c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2263c88580c4SEric Fiselier  __compressed_pair() {}
2264a87b5e34SHoward Hinnant
2265c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY explicit
2266c88580c4SEric Fiselier  __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
22673e519524SHoward Hinnant
2268c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2269c88580c4SEric Fiselier  __compressed_pair(__second_tag, _T2 __t2)
2270c88580c4SEric Fiselier      : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
22713e519524SHoward Hinnant
2272c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2273c88580c4SEric Fiselier  __compressed_pair(_T1 __t1, _T2 __t2)
2274c88580c4SEric Fiselier      : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
2275c88580c4SEric Fiselier#endif
2276c88580c4SEric Fiselier
2277c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2278c88580c4SEric Fiselier  typename _Base1::reference first() _NOEXCEPT {
2279c88580c4SEric Fiselier    return static_cast<_Base1&>(*this).__get();
2280c88580c4SEric Fiselier  }
2281c88580c4SEric Fiselier
2282c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2283c88580c4SEric Fiselier  typename _Base1::const_reference first() const _NOEXCEPT {
2284c88580c4SEric Fiselier    return static_cast<_Base1 const&>(*this).__get();
2285c88580c4SEric Fiselier  }
2286c88580c4SEric Fiselier
2287c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2288c88580c4SEric Fiselier  typename _Base2::reference second() _NOEXCEPT {
2289c88580c4SEric Fiselier    return static_cast<_Base2&>(*this).__get();
2290c88580c4SEric Fiselier  }
2291c88580c4SEric Fiselier
2292c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2293c88580c4SEric Fiselier  typename _Base2::const_reference second() const _NOEXCEPT {
2294c88580c4SEric Fiselier    return static_cast<_Base2 const&>(*this).__get();
2295c88580c4SEric Fiselier  }
2296c88580c4SEric Fiselier
2297c88580c4SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2298c88580c4SEric Fiselier  void swap(__compressed_pair& __x)
22993739fe79SHoward Hinnant    _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2300afa72ed4SMarshall Clow               __is_nothrow_swappable<_T2>::value)
2301c88580c4SEric Fiselier  {
2302c88580c4SEric Fiselier    using std::swap;
2303c88580c4SEric Fiselier    swap(first(), __x.first());
2304c88580c4SEric Fiselier    swap(second(), __x.second());
2305c88580c4SEric Fiselier  }
23063e519524SHoward Hinnant};
23073e519524SHoward Hinnant
23083e519524SHoward Hinnanttemplate <class _T1, class _T2>
23093e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2310c88580c4SEric Fiseliervoid swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
23113739fe79SHoward Hinnant    _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
2312c88580c4SEric Fiselier               __is_nothrow_swappable<_T2>::value) {
2313c88580c4SEric Fiselier  __x.swap(__y);
2314c88580c4SEric Fiselier}
23153e519524SHoward Hinnant
23168e25104bSHoward Hinnant// default_delete
23178e25104bSHoward Hinnant
23183e519524SHoward Hinnanttemplate <class _Tp>
2319030d19bdSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS default_delete {
232072d0d603SErik Pilkington    static_assert(!is_function<_Tp>::value,
232172d0d603SErik Pilkington                  "default_delete cannot be instantiated for function types");
2322030d19bdSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
2323*fb2bd4a9SEric Fiselier  _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
2324c0937e8aSHoward Hinnant#else
2325030d19bdSEric Fiselier  _LIBCPP_INLINE_VISIBILITY default_delete() {}
2326c0937e8aSHoward Hinnant#endif
23273e519524SHoward Hinnant  template <class _Up>
2328030d19bdSEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2329030d19bdSEric Fiselier  default_delete(const default_delete<_Up>&,
2330030d19bdSEric Fiselier                 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
2331030d19bdSEric Fiselier                     0) _NOEXCEPT {}
2332030d19bdSEric Fiselier
2333030d19bdSEric Fiselier  _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
2334030d19bdSEric Fiselier    static_assert(sizeof(_Tp) > 0,
2335030d19bdSEric Fiselier                  "default_delete can not delete incomplete type");
2336030d19bdSEric Fiselier    static_assert(!is_void<_Tp>::value,
2337030d19bdSEric Fiselier                  "default_delete can not delete incomplete type");
23383e519524SHoward Hinnant    delete __ptr;
23393e519524SHoward Hinnant  }
23403e519524SHoward Hinnant};
23413e519524SHoward Hinnant
23423e519524SHoward Hinnanttemplate <class _Tp>
2343030d19bdSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
2344030d19bdSEric Fiselierprivate:
2345e4097ad7SHoward Hinnant  template <class _Up>
2346030d19bdSEric Fiselier  struct _EnableIfConvertible
2347030d19bdSEric Fiselier      : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
2348030d19bdSEric Fiselier
2349030d19bdSEric Fiselierpublic:
2350030d19bdSEric Fiselier#ifndef _LIBCPP_CXX03_LANG
2351*fb2bd4a9SEric Fiselier  _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
2352030d19bdSEric Fiselier#else
2353030d19bdSEric Fiselier  _LIBCPP_INLINE_VISIBILITY default_delete() {}
2354030d19bdSEric Fiselier#endif
2355030d19bdSEric Fiselier
2356e4097ad7SHoward Hinnant  template <class _Up>
2357e4097ad7SHoward Hinnant  _LIBCPP_INLINE_VISIBILITY
2358030d19bdSEric Fiselier  default_delete(const default_delete<_Up[]>&,
2359030d19bdSEric Fiselier                 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
2360030d19bdSEric Fiselier
2361030d19bdSEric Fiselier  template <class _Up>
2362030d19bdSEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2363030d19bdSEric Fiselier  typename _EnableIfConvertible<_Up>::type
2364030d19bdSEric Fiselier  operator()(_Up* __ptr) const _NOEXCEPT {
2365030d19bdSEric Fiselier    static_assert(sizeof(_Tp) > 0,
2366030d19bdSEric Fiselier                  "default_delete can not delete incomplete type");
2367030d19bdSEric Fiselier    static_assert(!is_void<_Tp>::value,
2368030d19bdSEric Fiselier                  "default_delete can not delete void type");
23693e519524SHoward Hinnant    delete[] __ptr;
23703e519524SHoward Hinnant  }
23713e519524SHoward Hinnant};
23723e519524SHoward Hinnant
237347272722SEric Fiseliertemplate <class _Deleter>
237447272722SEric Fiselierstruct __unique_ptr_deleter_sfinae {
237547272722SEric Fiselier  static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
237647272722SEric Fiselier  typedef const _Deleter& __lval_ref_type;
237747272722SEric Fiselier  typedef _Deleter&& __good_rval_ref_type;
237847272722SEric Fiselier  typedef true_type __enable_rval_overload;
237947272722SEric Fiselier};
238047272722SEric Fiselier
238147272722SEric Fiseliertemplate <class _Deleter>
238247272722SEric Fiselierstruct __unique_ptr_deleter_sfinae<_Deleter const&> {
238347272722SEric Fiselier  typedef const _Deleter& __lval_ref_type;
238447272722SEric Fiselier  typedef const _Deleter&& __bad_rval_ref_type;
238547272722SEric Fiselier  typedef false_type __enable_rval_overload;
238647272722SEric Fiselier};
238747272722SEric Fiselier
238847272722SEric Fiseliertemplate <class _Deleter>
238947272722SEric Fiselierstruct __unique_ptr_deleter_sfinae<_Deleter&> {
239047272722SEric Fiselier  typedef _Deleter& __lval_ref_type;
239147272722SEric Fiselier  typedef _Deleter&& __bad_rval_ref_type;
239247272722SEric Fiselier  typedef false_type __enable_rval_overload;
239347272722SEric Fiselier};
239447272722SEric Fiselier
23953e519524SHoward Hinnanttemplate <class _Tp, class _Dp = default_delete<_Tp> >
239647272722SEric Fiselierclass _LIBCPP_TEMPLATE_VIS unique_ptr {
23973e519524SHoward Hinnantpublic:
23983e519524SHoward Hinnant  typedef _Tp element_type;
23993e519524SHoward Hinnant  typedef _Dp deleter_type;
240014d48692SEric Fiselier  typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
240147272722SEric Fiselier
240247272722SEric Fiselier  static_assert(!is_rvalue_reference<deleter_type>::value,
240347272722SEric Fiselier                "the specified deleter type cannot be an rvalue reference");
240447272722SEric Fiselier
24053e519524SHoward Hinnantprivate:
24063e519524SHoward Hinnant  __compressed_pair<pointer, deleter_type> __ptr_;
24073e519524SHoward Hinnant
24083e519524SHoward Hinnant  struct __nat { int __for_bool_; };
24093e519524SHoward Hinnant
241014d48692SEric Fiselier  typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
24113e519524SHoward Hinnant
241247272722SEric Fiselier  template <bool _Dummy>
241314d48692SEric Fiselier  using _LValRefType _LIBCPP_NODEBUG_TYPE =
241492e61e93SEric Fiselier      typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
241547272722SEric Fiselier
241647272722SEric Fiselier  template <bool _Dummy>
241714d48692SEric Fiselier  using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
241892e61e93SEric Fiselier      typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
241947272722SEric Fiselier
242047272722SEric Fiselier  template <bool _Dummy>
242114d48692SEric Fiselier  using _BadRValRefType _LIBCPP_NODEBUG_TYPE  =
242292e61e93SEric Fiselier      typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
242347272722SEric Fiselier
242447272722SEric Fiselier  template <bool _Dummy, class _Deleter = typename __dependent_type<
242547272722SEric Fiselier                             __identity<deleter_type>, _Dummy>::type>
242614d48692SEric Fiselier  using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
242747272722SEric Fiselier      typename enable_if<is_default_constructible<_Deleter>::value &&
242847272722SEric Fiselier                         !is_pointer<_Deleter>::value>::type;
242947272722SEric Fiselier
243047272722SEric Fiselier  template <class _ArgType>
243114d48692SEric Fiselier  using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE  =
243247272722SEric Fiselier      typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
243347272722SEric Fiselier
243447272722SEric Fiselier  template <class _UPtr, class _Up>
243514d48692SEric Fiselier  using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
243647272722SEric Fiselier      is_convertible<typename _UPtr::pointer, pointer>::value &&
243747272722SEric Fiselier      !is_array<_Up>::value
243847272722SEric Fiselier  >::type;
243947272722SEric Fiselier
244047272722SEric Fiselier  template <class _UDel>
244114d48692SEric Fiselier  using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
244247272722SEric Fiselier      (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
244347272722SEric Fiselier      (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
244447272722SEric Fiselier    >::type;
244547272722SEric Fiselier
244647272722SEric Fiselier  template <class _UDel>
244747272722SEric Fiselier  using _EnableIfDeleterAssignable = typename enable_if<
244847272722SEric Fiselier      is_assignable<_Dp&, _UDel&&>::value
244947272722SEric Fiselier    >::type;
245047272722SEric Fiselier
245147272722SEric Fiselierpublic:
245247272722SEric Fiselier  template <bool _Dummy = true,
245347272722SEric Fiselier            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
245447272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2455*fb2bd4a9SEric Fiselier  _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
245647272722SEric Fiselier
245747272722SEric Fiselier  template <bool _Dummy = true,
245847272722SEric Fiselier            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
245947272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2460*fb2bd4a9SEric Fiselier  _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
246147272722SEric Fiselier
246247272722SEric Fiselier  template <bool _Dummy = true,
246347272722SEric Fiselier            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
246447272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2465*fb2bd4a9SEric Fiselier  explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p) {}
246647272722SEric Fiselier
246747272722SEric Fiselier  template <bool _Dummy = true,
246847272722SEric Fiselier            class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
246947272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2470*fb2bd4a9SEric Fiselier  unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
24713e519524SHoward Hinnant      : __ptr_(__p, __d) {}
24723e519524SHoward Hinnant
247347272722SEric Fiselier  template <bool _Dummy = true,
247447272722SEric Fiselier            class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
24753e519524SHoward Hinnant  _LIBCPP_INLINE_VISIBILITY
2476*fb2bd4a9SEric Fiselier  unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
247747272722SEric Fiselier      : __ptr_(__p, _VSTD::move(__d)) {
247847272722SEric Fiselier    static_assert(!is_reference<deleter_type>::value,
247947272722SEric Fiselier                  "rvalue deleter bound to reference");
248047272722SEric Fiselier  }
248147272722SEric Fiselier
248247272722SEric Fiselier  template <bool _Dummy = true,
248347272722SEric Fiselier            class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
248447272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
248547272722SEric Fiselier  unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
248647272722SEric Fiselier
248747272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2488*fb2bd4a9SEric Fiselier  unique_ptr(unique_ptr&& __u) _NOEXCEPT
248947272722SEric Fiselier      : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
249047272722SEric Fiselier  }
249147272722SEric Fiselier
249247272722SEric Fiselier  template <class _Up, class _Ep,
249347272722SEric Fiselier      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
249447272722SEric Fiselier      class = _EnableIfDeleterConvertible<_Ep>
249547272722SEric Fiselier  >
249647272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
249747272722SEric Fiselier  unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
2498ce48a113SHoward Hinnant      : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
24993e519524SHoward Hinnant
2500e67179bcSMarshall Clow#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
25013e519524SHoward Hinnant  template <class _Up>
250247272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
250347272722SEric Fiselier  unique_ptr(auto_ptr<_Up>&& __p,
250447272722SEric Fiselier             typename enable_if<is_convertible<_Up*, _Tp*>::value &&
25053e519524SHoward Hinnant                                    is_same<_Dp, default_delete<_Tp> >::value,
250647272722SEric Fiselier                                __nat>::type = __nat()) _NOEXCEPT
250747272722SEric Fiselier      : __ptr_(__p.release()) {}
2508e67179bcSMarshall Clow#endif
25093e519524SHoward Hinnant
251047272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
251147272722SEric Fiselier  unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
25123e519524SHoward Hinnant    reset(__u.release());
2513ce48a113SHoward Hinnant    __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
25143e519524SHoward Hinnant    return *this;
25153e519524SHoward Hinnant  }
25163e519524SHoward Hinnant
251747272722SEric Fiselier  template <class _Up, class _Ep,
251847272722SEric Fiselier      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
251947272722SEric Fiselier      class = _EnableIfDeleterAssignable<_Ep>
252047272722SEric Fiselier  >
25213e519524SHoward Hinnant  _LIBCPP_INLINE_VISIBILITY
252247272722SEric Fiselier  unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
25233e519524SHoward Hinnant    reset(__u.release());
2524ce48a113SHoward Hinnant    __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
25253e519524SHoward Hinnant    return *this;
25263e519524SHoward Hinnant  }
25273e519524SHoward Hinnant
2528e67179bcSMarshall Clow#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
25293e519524SHoward Hinnant  template <class _Up>
25303e519524SHoward Hinnant  _LIBCPP_INLINE_VISIBILITY
253147272722SEric Fiselier      typename enable_if<is_convertible<_Up*, _Tp*>::value &&
25323e519524SHoward Hinnant                             is_same<_Dp, default_delete<_Tp> >::value,
253347272722SEric Fiselier                         unique_ptr&>::type
253447272722SEric Fiselier      operator=(auto_ptr<_Up> __p) {
253547272722SEric Fiselier    reset(__p.release());
253647272722SEric Fiselier    return *this;
253747272722SEric Fiselier  }
2538e67179bcSMarshall Clow#endif
25393e519524SHoward Hinnant
2540*fb2bd4a9SEric Fiselier#ifdef _LIBCPP_CXX03_LANG
2541*fb2bd4a9SEric Fiselier  unique_ptr(unique_ptr const&) = delete;
2542*fb2bd4a9SEric Fiselier  unique_ptr& operator=(unique_ptr const&) = delete;
2543*fb2bd4a9SEric Fiselier#endif
2544*fb2bd4a9SEric Fiselier
2545*fb2bd4a9SEric Fiselier
254647272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
254747272722SEric Fiselier  ~unique_ptr() { reset(); }
254847272722SEric Fiselier
254947272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
255047272722SEric Fiselier  unique_ptr& operator=(nullptr_t) _NOEXCEPT {
25513e519524SHoward Hinnant    reset();
25523e519524SHoward Hinnant    return *this;
25533e519524SHoward Hinnant  }
25543e519524SHoward Hinnant
2555f2f2d8b9SHoward Hinnant  _LIBCPP_INLINE_VISIBILITY
255647272722SEric Fiselier  typename add_lvalue_reference<_Tp>::type
255747272722SEric Fiselier  operator*() const {
255847272722SEric Fiselier    return *__ptr_.first();
255947272722SEric Fiselier  }
256047272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
256147272722SEric Fiselier  pointer operator->() const _NOEXCEPT {
256247272722SEric Fiselier    return __ptr_.first();
256347272722SEric Fiselier  }
256447272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
256547272722SEric Fiselier  pointer get() const _NOEXCEPT {
256647272722SEric Fiselier    return __ptr_.first();
256747272722SEric Fiselier  }
256847272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
256947272722SEric Fiselier  deleter_type& get_deleter() _NOEXCEPT {
257047272722SEric Fiselier    return __ptr_.second();
257147272722SEric Fiselier  }
257247272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
257347272722SEric Fiselier  const deleter_type& get_deleter() const _NOEXCEPT {
257447272722SEric Fiselier    return __ptr_.second();
257547272722SEric Fiselier  }
257647272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
257747272722SEric Fiselier  _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
257847272722SEric Fiselier    return __ptr_.first() != nullptr;
257947272722SEric Fiselier  }
25803e519524SHoward Hinnant
258147272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
258247272722SEric Fiselier  pointer release() _NOEXCEPT {
25833e519524SHoward Hinnant    pointer __t = __ptr_.first();
25843e519524SHoward Hinnant    __ptr_.first() = pointer();
25853e519524SHoward Hinnant    return __t;
25863e519524SHoward Hinnant  }
25873e519524SHoward Hinnant
258847272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
258947272722SEric Fiselier  void reset(pointer __p = pointer()) _NOEXCEPT {
25903e519524SHoward Hinnant    pointer __tmp = __ptr_.first();
25913e519524SHoward Hinnant    __ptr_.first() = __p;
25923e519524SHoward Hinnant    if (__tmp)
25933e519524SHoward Hinnant      __ptr_.second()(__tmp);
25943e519524SHoward Hinnant  }
25953e519524SHoward Hinnant
259647272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
259747272722SEric Fiselier  void swap(unique_ptr& __u) _NOEXCEPT {
259847272722SEric Fiselier    __ptr_.swap(__u.__ptr_);
259947272722SEric Fiselier  }
26003e519524SHoward Hinnant};
26013e519524SHoward Hinnant
260247272722SEric Fiselier
26033e519524SHoward Hinnanttemplate <class _Tp, class _Dp>
260447272722SEric Fiselierclass _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
26053e519524SHoward Hinnantpublic:
26063e519524SHoward Hinnant  typedef _Tp element_type;
26073e519524SHoward Hinnant  typedef _Dp deleter_type;
26083e519524SHoward Hinnant  typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
260947272722SEric Fiselier
26103e519524SHoward Hinnantprivate:
26113e519524SHoward Hinnant  __compressed_pair<pointer, deleter_type> __ptr_;
26123e519524SHoward Hinnant
261392e61e93SEric Fiselier  template <class _From>
261492e61e93SEric Fiselier  struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
26153e519524SHoward Hinnant
261692e61e93SEric Fiselier  template <class _FromElem>
261792e61e93SEric Fiselier  struct _CheckArrayPointerConversion<_FromElem*>
261892e61e93SEric Fiselier      : integral_constant<bool,
261992e61e93SEric Fiselier          is_same<_FromElem*, pointer>::value ||
262092e61e93SEric Fiselier            (is_same<pointer, element_type*>::value &&
262192e61e93SEric Fiselier             is_convertible<_FromElem(*)[], element_type(*)[]>::value)
262292e61e93SEric Fiselier      >
262392e61e93SEric Fiselier  {};
262447272722SEric Fiselier
262592e61e93SEric Fiselier  typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
262647272722SEric Fiselier
262747272722SEric Fiselier  template <bool _Dummy>
262814d48692SEric Fiselier  using _LValRefType _LIBCPP_NODEBUG_TYPE =
262992e61e93SEric Fiselier      typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
263047272722SEric Fiselier
263147272722SEric Fiselier  template <bool _Dummy>
263214d48692SEric Fiselier  using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
263392e61e93SEric Fiselier      typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
263447272722SEric Fiselier
263547272722SEric Fiselier  template <bool _Dummy>
263614d48692SEric Fiselier  using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
263792e61e93SEric Fiselier      typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
263847272722SEric Fiselier
263947272722SEric Fiselier  template <bool _Dummy, class _Deleter = typename __dependent_type<
264047272722SEric Fiselier                             __identity<deleter_type>, _Dummy>::type>
264114d48692SEric Fiselier  using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE  =
264247272722SEric Fiselier      typename enable_if<is_default_constructible<_Deleter>::value &&
264347272722SEric Fiselier                         !is_pointer<_Deleter>::value>::type;
264447272722SEric Fiselier
264547272722SEric Fiselier  template <class _ArgType>
264614d48692SEric Fiselier  using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE  =
264747272722SEric Fiselier      typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
26483e519524SHoward Hinnant
264922747e62SLogan Chien  template <class _Pp>
265014d48692SEric Fiselier  using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
265192e61e93SEric Fiselier      _CheckArrayPointerConversion<_Pp>::value
265247272722SEric Fiselier  >::type;
265347272722SEric Fiselier
265447272722SEric Fiselier  template <class _UPtr, class _Up,
265547272722SEric Fiselier        class _ElemT = typename _UPtr::element_type>
265614d48692SEric Fiselier  using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
265747272722SEric Fiselier      is_array<_Up>::value &&
265847272722SEric Fiselier      is_same<pointer, element_type*>::value &&
265947272722SEric Fiselier      is_same<typename _UPtr::pointer, _ElemT*>::value &&
266047272722SEric Fiselier      is_convertible<_ElemT(*)[], element_type(*)[]>::value
266147272722SEric Fiselier    >::type;
266247272722SEric Fiselier
266347272722SEric Fiselier  template <class _UDel>
266414d48692SEric Fiselier  using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE  = typename enable_if<
266547272722SEric Fiselier      (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
266647272722SEric Fiselier      (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
266747272722SEric Fiselier    >::type;
266847272722SEric Fiselier
266947272722SEric Fiselier  template <class _UDel>
267014d48692SEric Fiselier  using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE  = typename enable_if<
267147272722SEric Fiselier      is_assignable<_Dp&, _UDel&&>::value
267247272722SEric Fiselier    >::type;
267347272722SEric Fiselier
267447272722SEric Fiselierpublic:
267547272722SEric Fiselier  template <bool _Dummy = true,
267647272722SEric Fiselier            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
267747272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2678*fb2bd4a9SEric Fiselier  _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
267947272722SEric Fiselier
268047272722SEric Fiselier  template <bool _Dummy = true,
268147272722SEric Fiselier            class = _EnableIfDeleterDefaultConstructible<_Dummy> >
268247272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2683*fb2bd4a9SEric Fiselier  _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
268447272722SEric Fiselier
268547272722SEric Fiselier  template <class _Pp, bool _Dummy = true,
268647272722SEric Fiselier            class = _EnableIfDeleterDefaultConstructible<_Dummy>,
268747272722SEric Fiselier            class = _EnableIfPointerConvertible<_Pp> >
268847272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2689*fb2bd4a9SEric Fiselier  explicit unique_ptr(_Pp __p) _NOEXCEPT
269047272722SEric Fiselier      : __ptr_(__p) {}
269147272722SEric Fiselier
269247272722SEric Fiselier  template <class _Pp, bool _Dummy = true,
269347272722SEric Fiselier            class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
269447272722SEric Fiselier            class = _EnableIfPointerConvertible<_Pp> >
269547272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2696*fb2bd4a9SEric Fiselier  unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
26973e519524SHoward Hinnant      : __ptr_(__p, __d) {}
26983e519524SHoward Hinnant
269947272722SEric Fiselier  template <bool _Dummy = true,
270047272722SEric Fiselier            class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
270147272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2702*fb2bd4a9SEric Fiselier  unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
270347272722SEric Fiselier      : __ptr_(nullptr, __d) {}
27043e519524SHoward Hinnant
270547272722SEric Fiselier  template <class _Pp, bool _Dummy = true,
270647272722SEric Fiselier            class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
270747272722SEric Fiselier            class = _EnableIfPointerConvertible<_Pp> >
270847272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2709*fb2bd4a9SEric Fiselier  unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
271047272722SEric Fiselier      : __ptr_(__p, _VSTD::move(__d)) {
271147272722SEric Fiselier    static_assert(!is_reference<deleter_type>::value,
271247272722SEric Fiselier                  "rvalue deleter bound to reference");
27133e519524SHoward Hinnant  }
27143e519524SHoward Hinnant
271547272722SEric Fiselier  template <bool _Dummy = true,
271647272722SEric Fiselier            class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
271747272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2718*fb2bd4a9SEric Fiselier  unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
271947272722SEric Fiselier      : __ptr_(nullptr, _VSTD::move(__d)) {
272047272722SEric Fiselier    static_assert(!is_reference<deleter_type>::value,
272147272722SEric Fiselier                  "rvalue deleter bound to reference");
27223e519524SHoward Hinnant  }
27233e519524SHoward Hinnant
272447272722SEric Fiselier  template <class _Pp, bool _Dummy = true,
272547272722SEric Fiselier            class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
272647272722SEric Fiselier            class = _EnableIfPointerConvertible<_Pp> >
272747272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
272847272722SEric Fiselier  unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
27293e519524SHoward Hinnant
273047272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2731*fb2bd4a9SEric Fiselier  unique_ptr(unique_ptr&& __u) _NOEXCEPT
273247272722SEric Fiselier      : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
273347272722SEric Fiselier  }
273447272722SEric Fiselier
273547272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
2736*fb2bd4a9SEric Fiselier  unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
27373e519524SHoward Hinnant    reset(__u.release());
2738ce48a113SHoward Hinnant    __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
27393e519524SHoward Hinnant    return *this;
27403e519524SHoward Hinnant  }
2741e4097ad7SHoward Hinnant
274247272722SEric Fiselier  template <class _Up, class _Ep,
274347272722SEric Fiselier      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
274447272722SEric Fiselier      class = _EnableIfDeleterConvertible<_Ep>
274547272722SEric Fiselier  >
2746e4097ad7SHoward Hinnant  _LIBCPP_INLINE_VISIBILITY
2747*fb2bd4a9SEric Fiselier  unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
274886321b2bSEric Fiselier      : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
274947272722SEric Fiselier  }
2750e4097ad7SHoward Hinnant
275147272722SEric Fiselier  template <class _Up, class _Ep,
275247272722SEric Fiselier      class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
275347272722SEric Fiselier      class = _EnableIfDeleterAssignable<_Ep>
275447272722SEric Fiselier  >
2755e4097ad7SHoward Hinnant  _LIBCPP_INLINE_VISIBILITY
2756e4097ad7SHoward Hinnant  unique_ptr&
2757*fb2bd4a9SEric Fiselier  operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
2758e4097ad7SHoward Hinnant    reset(__u.release());
2759e4097ad7SHoward Hinnant    __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
2760e4097ad7SHoward Hinnant    return *this;
2761e4097ad7SHoward Hinnant  }
27623e519524SHoward Hinnant
2763*fb2bd4a9SEric Fiselier#ifdef _LIBCPP_CXX03_LANG
2764*fb2bd4a9SEric Fiselier  unique_ptr(unique_ptr const&) = delete;
2765*fb2bd4a9SEric Fiselier  unique_ptr& operator=(unique_ptr const&) = delete;
2766*fb2bd4a9SEric Fiselier#endif
27673e519524SHoward Hinnant
276847272722SEric Fiselierpublic:
276947272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
277047272722SEric Fiselier  ~unique_ptr() { reset(); }
277147272722SEric Fiselier
277247272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
277347272722SEric Fiselier  unique_ptr& operator=(nullptr_t) _NOEXCEPT {
27743e519524SHoward Hinnant    reset();
27753e519524SHoward Hinnant    return *this;
27763e519524SHoward Hinnant  }
27773e519524SHoward Hinnant
2778f2f2d8b9SHoward Hinnant  _LIBCPP_INLINE_VISIBILITY
277947272722SEric Fiselier  typename add_lvalue_reference<_Tp>::type
278047272722SEric Fiselier  operator[](size_t __i) const {
278147272722SEric Fiselier    return __ptr_.first()[__i];
278247272722SEric Fiselier  }
278347272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
278447272722SEric Fiselier  pointer get() const _NOEXCEPT {
278547272722SEric Fiselier    return __ptr_.first();
278647272722SEric Fiselier  }
27873e519524SHoward Hinnant
278847272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
278947272722SEric Fiselier  deleter_type& get_deleter() _NOEXCEPT {
279047272722SEric Fiselier    return __ptr_.second();
279147272722SEric Fiselier  }
279247272722SEric Fiselier
279347272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
279447272722SEric Fiselier  const deleter_type& get_deleter() const _NOEXCEPT {
279547272722SEric Fiselier    return __ptr_.second();
279647272722SEric Fiselier  }
279747272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
279847272722SEric Fiselier  _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
279947272722SEric Fiselier    return __ptr_.first() != nullptr;
280047272722SEric Fiselier  }
280147272722SEric Fiselier
280247272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
280347272722SEric Fiselier  pointer release() _NOEXCEPT {
28043e519524SHoward Hinnant    pointer __t = __ptr_.first();
28053e519524SHoward Hinnant    __ptr_.first() = pointer();
28063e519524SHoward Hinnant    return __t;
28073e519524SHoward Hinnant  }
28083e519524SHoward Hinnant
280922747e62SLogan Chien  template <class _Pp>
281022747e62SLogan Chien  _LIBCPP_INLINE_VISIBILITY
281147272722SEric Fiselier  typename enable_if<
281292e61e93SEric Fiselier      _CheckArrayPointerConversion<_Pp>::value
281347272722SEric Fiselier  >::type
281447272722SEric Fiselier  reset(_Pp __p) _NOEXCEPT {
28153e519524SHoward Hinnant    pointer __tmp = __ptr_.first();
28163e519524SHoward Hinnant    __ptr_.first() = __p;
28173e519524SHoward Hinnant    if (__tmp)
28183e519524SHoward Hinnant      __ptr_.second()(__tmp);
28193e519524SHoward Hinnant  }
282047272722SEric Fiselier
282147272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
282247272722SEric Fiselier  void reset(nullptr_t = nullptr) _NOEXCEPT {
28233e519524SHoward Hinnant    pointer __tmp = __ptr_.first();
28243e519524SHoward Hinnant    __ptr_.first() = nullptr;
28253e519524SHoward Hinnant    if (__tmp)
28263e519524SHoward Hinnant      __ptr_.second()(__tmp);
28273e519524SHoward Hinnant  }
28283e519524SHoward Hinnant
282947272722SEric Fiselier  _LIBCPP_INLINE_VISIBILITY
283047272722SEric Fiselier  void swap(unique_ptr& __u) _NOEXCEPT {
283147272722SEric Fiselier    __ptr_.swap(__u.__ptr_);
283247272722SEric Fiselier  }
28333e519524SHoward Hinnant
28343e519524SHoward Hinnant};
28353e519524SHoward Hinnant
28363e519524SHoward Hinnanttemplate <class _Tp, class _Dp>
28373e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
2838f07dd8d0SEric Fiseliertypename enable_if<
2839f07dd8d0SEric Fiselier    __is_swappable<_Dp>::value,
28403e519524SHoward Hinnant    void
2841f07dd8d0SEric Fiselier>::type
28423739fe79SHoward Hinnantswap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
28433e519524SHoward Hinnant
28443e519524SHoward Hinnanttemplate <class _T1, class _D1, class _T2, class _D2>
28453e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
28463e519524SHoward Hinnantbool
28473e519524SHoward Hinnantoperator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
28483e519524SHoward Hinnant
28493e519524SHoward Hinnanttemplate <class _T1, class _D1, class _T2, class _D2>
28503e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
28513e519524SHoward Hinnantbool
28523e519524SHoward Hinnantoperator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
28533e519524SHoward Hinnant
28543e519524SHoward Hinnanttemplate <class _T1, class _D1, class _T2, class _D2>
28553e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
28563e519524SHoward Hinnantbool
285767f39647SHoward Hinnantoperator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
285867f39647SHoward Hinnant{
285967f39647SHoward Hinnant    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
286067f39647SHoward Hinnant    typedef typename unique_ptr<_T2, _D2>::pointer _P2;
28616fe361c1SEric Fiselier    typedef typename common_type<_P1, _P2>::type _Vp;
28626fe361c1SEric Fiselier    return less<_Vp>()(__x.get(), __y.get());
286367f39647SHoward Hinnant}
28643e519524SHoward Hinnant
28653e519524SHoward Hinnanttemplate <class _T1, class _D1, class _T2, class _D2>
28663e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
28673e519524SHoward Hinnantbool
28683e519524SHoward Hinnantoperator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
28693e519524SHoward Hinnant
28703e519524SHoward Hinnanttemplate <class _T1, class _D1, class _T2, class _D2>
28713e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
28723e519524SHoward Hinnantbool
28733e519524SHoward Hinnantoperator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
28743e519524SHoward Hinnant
28753e519524SHoward Hinnanttemplate <class _T1, class _D1, class _T2, class _D2>
28763e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
28773e519524SHoward Hinnantbool
28783e519524SHoward Hinnantoperator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
28793e519524SHoward Hinnant
288067f39647SHoward Hinnanttemplate <class _T1, class _D1>
288167f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
288267f39647SHoward Hinnantbool
2883c0937e8aSHoward Hinnantoperator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
288467f39647SHoward Hinnant{
288567f39647SHoward Hinnant    return !__x;
288667f39647SHoward Hinnant}
288767f39647SHoward Hinnant
288867f39647SHoward Hinnanttemplate <class _T1, class _D1>
288967f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
289067f39647SHoward Hinnantbool
2891c0937e8aSHoward Hinnantoperator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
289267f39647SHoward Hinnant{
289367f39647SHoward Hinnant    return !__x;
289467f39647SHoward Hinnant}
289567f39647SHoward Hinnant
289667f39647SHoward Hinnanttemplate <class _T1, class _D1>
289767f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
289867f39647SHoward Hinnantbool
2899c0937e8aSHoward Hinnantoperator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
290067f39647SHoward Hinnant{
290167f39647SHoward Hinnant    return static_cast<bool>(__x);
290267f39647SHoward Hinnant}
290367f39647SHoward Hinnant
290467f39647SHoward Hinnanttemplate <class _T1, class _D1>
290567f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
290667f39647SHoward Hinnantbool
2907c0937e8aSHoward Hinnantoperator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
290867f39647SHoward Hinnant{
290967f39647SHoward Hinnant    return static_cast<bool>(__x);
291067f39647SHoward Hinnant}
291167f39647SHoward Hinnant
291267f39647SHoward Hinnanttemplate <class _T1, class _D1>
291367f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
291467f39647SHoward Hinnantbool
291567f39647SHoward Hinnantoperator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
291667f39647SHoward Hinnant{
291767f39647SHoward Hinnant    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
291867f39647SHoward Hinnant    return less<_P1>()(__x.get(), nullptr);
291967f39647SHoward Hinnant}
292067f39647SHoward Hinnant
292167f39647SHoward Hinnanttemplate <class _T1, class _D1>
292267f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
292367f39647SHoward Hinnantbool
292467f39647SHoward Hinnantoperator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
292567f39647SHoward Hinnant{
292667f39647SHoward Hinnant    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
292767f39647SHoward Hinnant    return less<_P1>()(nullptr, __x.get());
292867f39647SHoward Hinnant}
292967f39647SHoward Hinnant
293067f39647SHoward Hinnanttemplate <class _T1, class _D1>
293167f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
293267f39647SHoward Hinnantbool
293367f39647SHoward Hinnantoperator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
293467f39647SHoward Hinnant{
293567f39647SHoward Hinnant    return nullptr < __x;
293667f39647SHoward Hinnant}
293767f39647SHoward Hinnant
293867f39647SHoward Hinnanttemplate <class _T1, class _D1>
293967f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
294067f39647SHoward Hinnantbool
294167f39647SHoward Hinnantoperator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
294267f39647SHoward Hinnant{
294367f39647SHoward Hinnant    return __x < nullptr;
294467f39647SHoward Hinnant}
294567f39647SHoward Hinnant
294667f39647SHoward Hinnanttemplate <class _T1, class _D1>
294767f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
294867f39647SHoward Hinnantbool
294967f39647SHoward Hinnantoperator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
295067f39647SHoward Hinnant{
295167f39647SHoward Hinnant    return !(nullptr < __x);
295267f39647SHoward Hinnant}
295367f39647SHoward Hinnant
295467f39647SHoward Hinnanttemplate <class _T1, class _D1>
295567f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
295667f39647SHoward Hinnantbool
295767f39647SHoward Hinnantoperator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
295867f39647SHoward Hinnant{
295967f39647SHoward Hinnant    return !(__x < nullptr);
296067f39647SHoward Hinnant}
296167f39647SHoward Hinnant
296267f39647SHoward Hinnanttemplate <class _T1, class _D1>
296367f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
296467f39647SHoward Hinnantbool
296567f39647SHoward Hinnantoperator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
296667f39647SHoward Hinnant{
296767f39647SHoward Hinnant    return !(__x < nullptr);
296867f39647SHoward Hinnant}
296967f39647SHoward Hinnant
297067f39647SHoward Hinnanttemplate <class _T1, class _D1>
297167f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
297267f39647SHoward Hinnantbool
297367f39647SHoward Hinnantoperator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
297467f39647SHoward Hinnant{
297567f39647SHoward Hinnant    return !(nullptr < __x);
297667f39647SHoward Hinnant}
297767f39647SHoward Hinnant
297828d8ba5fSMarshall Clow#if _LIBCPP_STD_VER > 11
297928d8ba5fSMarshall Clow
298028d8ba5fSMarshall Clowtemplate<class _Tp>
298128d8ba5fSMarshall Clowstruct __unique_if
298228d8ba5fSMarshall Clow{
298328d8ba5fSMarshall Clow    typedef unique_ptr<_Tp> __unique_single;
298428d8ba5fSMarshall Clow};
298528d8ba5fSMarshall Clow
298628d8ba5fSMarshall Clowtemplate<class _Tp>
298728d8ba5fSMarshall Clowstruct __unique_if<_Tp[]>
298828d8ba5fSMarshall Clow{
298928d8ba5fSMarshall Clow    typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
299028d8ba5fSMarshall Clow};
299128d8ba5fSMarshall Clow
299228d8ba5fSMarshall Clowtemplate<class _Tp, size_t _Np>
299328d8ba5fSMarshall Clowstruct __unique_if<_Tp[_Np]>
299428d8ba5fSMarshall Clow{
299528d8ba5fSMarshall Clow    typedef void __unique_array_known_bound;
299628d8ba5fSMarshall Clow};
299728d8ba5fSMarshall Clow
299828d8ba5fSMarshall Clowtemplate<class _Tp, class... _Args>
299995ddb530SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY
300028d8ba5fSMarshall Clowtypename __unique_if<_Tp>::__unique_single
300128d8ba5fSMarshall Clowmake_unique(_Args&&... __args)
300228d8ba5fSMarshall Clow{
300328d8ba5fSMarshall Clow    return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
300428d8ba5fSMarshall Clow}
300528d8ba5fSMarshall Clow
300628d8ba5fSMarshall Clowtemplate<class _Tp>
300795ddb530SMarshall Clowinline _LIBCPP_INLINE_VISIBILITY
300828d8ba5fSMarshall Clowtypename __unique_if<_Tp>::__unique_array_unknown_bound
300928d8ba5fSMarshall Clowmake_unique(size_t __n)
301028d8ba5fSMarshall Clow{
301128d8ba5fSMarshall Clow    typedef typename remove_extent<_Tp>::type _Up;
301228d8ba5fSMarshall Clow    return unique_ptr<_Tp>(new _Up[__n]());
301328d8ba5fSMarshall Clow}
301428d8ba5fSMarshall Clow
301528d8ba5fSMarshall Clowtemplate<class _Tp, class... _Args>
301628d8ba5fSMarshall Clow    typename __unique_if<_Tp>::__unique_array_known_bound
301728d8ba5fSMarshall Clow    make_unique(_Args&&...) = delete;
301828d8ba5fSMarshall Clow
301928d8ba5fSMarshall Clow#endif  // _LIBCPP_STD_VER > 11
302028d8ba5fSMarshall Clow
3021d1803b6cSHoward Hinnanttemplate <class _Tp, class _Dp>
3022f9127593SEric Fiselier#ifdef _LIBCPP_CXX03_LANG
3023e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
3024f9127593SEric Fiselier#else
3025f9127593SEric Fiselierstruct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
3026f9127593SEric Fiselier    unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
3027f9127593SEric Fiselier#endif
3028d1803b6cSHoward Hinnant{
3029d1803b6cSHoward Hinnant    typedef unique_ptr<_Tp, _Dp> argument_type;
3030d1803b6cSHoward Hinnant    typedef size_t               result_type;
3031848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
30327c803385SMarshall Clow    result_type operator()(const argument_type& __ptr) const
3033d1803b6cSHoward Hinnant    {
3034d1803b6cSHoward Hinnant        typedef typename argument_type::pointer pointer;
3035d1803b6cSHoward Hinnant        return hash<pointer>()(__ptr.get());
3036d1803b6cSHoward Hinnant    }
3037d1803b6cSHoward Hinnant};
3038d1803b6cSHoward Hinnant
30393e519524SHoward Hinnantstruct __destruct_n
30403e519524SHoward Hinnant{
30413e519524SHoward Hinnantprivate:
30429ffacf3dSEric Fiselier    size_t __size_;
30433e519524SHoward Hinnant
30443e519524SHoward Hinnant    template <class _Tp>
30453739fe79SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
30469ffacf3dSEric Fiselier        {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
30473e519524SHoward Hinnant
30483e519524SHoward Hinnant    template <class _Tp>
30493739fe79SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
30503e519524SHoward Hinnant        {}
30513e519524SHoward Hinnant
30523739fe79SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
30539ffacf3dSEric Fiselier        {++__size_;}
30543739fe79SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
30553e519524SHoward Hinnant        {}
30563e519524SHoward Hinnant
30573739fe79SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
30589ffacf3dSEric Fiselier        {__size_ = __s;}
30593739fe79SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
30603e519524SHoward Hinnant        {}
30613e519524SHoward Hinnantpublic:
30623739fe79SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
30639ffacf3dSEric Fiselier        : __size_(__s) {}
30643e519524SHoward Hinnant
30653e519524SHoward Hinnant    template <class _Tp>
30663739fe79SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
3067ca740483SHoward Hinnant        {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
30683e519524SHoward Hinnant
30693e519524SHoward Hinnant    template <class _Tp>
30703739fe79SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
3071ca740483SHoward Hinnant        {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
30723e519524SHoward Hinnant
30733e519524SHoward Hinnant    template <class _Tp>
30743739fe79SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
3075ca740483SHoward Hinnant        {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
30763e519524SHoward Hinnant};
30773e519524SHoward Hinnant
30783e519524SHoward Hinnanttemplate <class _Alloc>
30793e519524SHoward Hinnantclass __allocator_destructor
30803e519524SHoward Hinnant{
308114d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
30823e519524SHoward Hinnantpublic:
308314d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
308414d48692SEric Fiselier    typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
30853e519524SHoward Hinnantprivate:
30863e519524SHoward Hinnant    _Alloc& __alloc_;
30873e519524SHoward Hinnant    size_type __s_;
30883e519524SHoward Hinnantpublic:
30893e519524SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
30903739fe79SHoward Hinnant             _NOEXCEPT
30913e519524SHoward Hinnant        : __alloc_(__a), __s_(__s) {}
3092848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
30933739fe79SHoward Hinnant    void operator()(pointer __p) _NOEXCEPT
30943739fe79SHoward Hinnant        {__alloc_traits::deallocate(__alloc_, __p, __s_);}
30953e519524SHoward Hinnant};
30963e519524SHoward Hinnant
30973e519524SHoward Hinnanttemplate <class _InputIterator, class _ForwardIterator>
30983e519524SHoward Hinnant_ForwardIterator
30993e519524SHoward Hinnantuninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
31003e519524SHoward Hinnant{
31013e519524SHoward Hinnant    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3102b34b4819SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
3103b34b4819SHoward Hinnant    _ForwardIterator __s = __r;
3104b34b4819SHoward Hinnant    try
3105b34b4819SHoward Hinnant    {
3106b34b4819SHoward Hinnant#endif
3107a00932bdSMarshall Clow        for (; __f != __l; ++__f, (void) ++__r)
3108a00932bdSMarshall Clow            ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
3109b34b4819SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
3110b34b4819SHoward Hinnant    }
3111b34b4819SHoward Hinnant    catch (...)
3112b34b4819SHoward Hinnant    {
3113b34b4819SHoward Hinnant        for (; __s != __r; ++__s)
3114b34b4819SHoward Hinnant            __s->~value_type();
3115b34b4819SHoward Hinnant        throw;
3116b34b4819SHoward Hinnant    }
3117b34b4819SHoward Hinnant#endif
31183e519524SHoward Hinnant    return __r;
31193e519524SHoward Hinnant}
31203e519524SHoward Hinnant
31213e519524SHoward Hinnanttemplate <class _InputIterator, class _Size, class _ForwardIterator>
31223e519524SHoward Hinnant_ForwardIterator
31233e519524SHoward Hinnantuninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
31243e519524SHoward Hinnant{
31253e519524SHoward Hinnant    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3126b34b4819SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
3127b34b4819SHoward Hinnant    _ForwardIterator __s = __r;
3128b34b4819SHoward Hinnant    try
3129b34b4819SHoward Hinnant    {
3130b34b4819SHoward Hinnant#endif
3131a00932bdSMarshall Clow        for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
3132a00932bdSMarshall Clow            ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
3133b34b4819SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
3134b34b4819SHoward Hinnant    }
3135b34b4819SHoward Hinnant    catch (...)
3136b34b4819SHoward Hinnant    {
3137b34b4819SHoward Hinnant        for (; __s != __r; ++__s)
3138b34b4819SHoward Hinnant            __s->~value_type();
3139b34b4819SHoward Hinnant        throw;
3140b34b4819SHoward Hinnant    }
3141b34b4819SHoward Hinnant#endif
31423e519524SHoward Hinnant    return __r;
31433e519524SHoward Hinnant}
31443e519524SHoward Hinnant
31453e519524SHoward Hinnanttemplate <class _ForwardIterator, class _Tp>
31463e519524SHoward Hinnantvoid
31473e519524SHoward Hinnantuninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
31483e519524SHoward Hinnant{
31493e519524SHoward Hinnant    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3150b34b4819SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
3151b34b4819SHoward Hinnant    _ForwardIterator __s = __f;
3152b34b4819SHoward Hinnant    try
3153b34b4819SHoward Hinnant    {
3154b34b4819SHoward Hinnant#endif
3155b34b4819SHoward Hinnant        for (; __f != __l; ++__f)
3156a00932bdSMarshall Clow            ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
3157b34b4819SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
3158b34b4819SHoward Hinnant    }
3159b34b4819SHoward Hinnant    catch (...)
3160b34b4819SHoward Hinnant    {
3161b34b4819SHoward Hinnant        for (; __s != __f; ++__s)
3162b34b4819SHoward Hinnant            __s->~value_type();
3163b34b4819SHoward Hinnant        throw;
3164b34b4819SHoward Hinnant    }
3165b34b4819SHoward Hinnant#endif
31663e519524SHoward Hinnant}
31673e519524SHoward Hinnant
31683e519524SHoward Hinnanttemplate <class _ForwardIterator, class _Size, class _Tp>
316948d05bd2SHoward Hinnant_ForwardIterator
31703e519524SHoward Hinnantuninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
31713e519524SHoward Hinnant{
31723e519524SHoward Hinnant    typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
3173b34b4819SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
3174b34b4819SHoward Hinnant    _ForwardIterator __s = __f;
3175b34b4819SHoward Hinnant    try
3176b34b4819SHoward Hinnant    {
3177b34b4819SHoward Hinnant#endif
3178a00932bdSMarshall Clow        for (; __n > 0; ++__f, (void) --__n)
3179a00932bdSMarshall Clow            ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
3180b34b4819SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
3181b34b4819SHoward Hinnant    }
3182b34b4819SHoward Hinnant    catch (...)
3183b34b4819SHoward Hinnant    {
3184b34b4819SHoward Hinnant        for (; __s != __f; ++__s)
3185b34b4819SHoward Hinnant            __s->~value_type();
3186b34b4819SHoward Hinnant        throw;
3187b34b4819SHoward Hinnant    }
3188b34b4819SHoward Hinnant#endif
318948d05bd2SHoward Hinnant    return __f;
31903e519524SHoward Hinnant}
31913e519524SHoward Hinnant
3192e4d9c316SEric Fiselier#if _LIBCPP_STD_VER > 14
3193e4d9c316SEric Fiselier
3194e4d9c316SEric Fiseliertemplate <class _Tp>
3195e4d9c316SEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
3196e4d9c316SEric Fiseliervoid destroy_at(_Tp* __loc) {
3197e4d9c316SEric Fiselier    _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
3198e4d9c316SEric Fiselier    __loc->~_Tp();
3199e4d9c316SEric Fiselier}
3200e4d9c316SEric Fiselier
3201e4d9c316SEric Fiseliertemplate <class _ForwardIterator>
3202e4d9c316SEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
3203e4d9c316SEric Fiseliervoid destroy(_ForwardIterator __first, _ForwardIterator __last) {
3204e4d9c316SEric Fiselier    for (; __first != __last; ++__first)
3205e4d9c316SEric Fiselier        _VSTD::destroy_at(_VSTD::addressof(*__first));
3206e4d9c316SEric Fiselier}
3207e4d9c316SEric Fiselier
3208e4d9c316SEric Fiseliertemplate <class _ForwardIterator, class _Size>
3209e4d9c316SEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
3210e4d9c316SEric Fiselier_ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
3211e4d9c316SEric Fiselier    for (; __n > 0; (void)++__first, --__n)
3212e4d9c316SEric Fiselier        _VSTD::destroy_at(_VSTD::addressof(*__first));
3213e4d9c316SEric Fiselier    return __first;
3214e4d9c316SEric Fiselier}
3215e4d9c316SEric Fiselier
3216e778d10cSEric Fiseliertemplate <class _ForwardIterator>
3217e778d10cSEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
3218e778d10cSEric Fiseliervoid uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
3219e778d10cSEric Fiselier    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3220e778d10cSEric Fiselier    auto __idx = __first;
3221e778d10cSEric Fiselier#ifndef _LIBCPP_NO_EXCEPTIONS
3222e778d10cSEric Fiselier    try {
3223e778d10cSEric Fiselier#endif
3224e778d10cSEric Fiselier    for (; __idx != __last; ++__idx)
3225e778d10cSEric Fiselier        ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3226e778d10cSEric Fiselier#ifndef _LIBCPP_NO_EXCEPTIONS
3227e778d10cSEric Fiselier    } catch (...) {
3228e778d10cSEric Fiselier        _VSTD::destroy(__first, __idx);
3229e778d10cSEric Fiselier        throw;
3230e778d10cSEric Fiselier    }
3231e778d10cSEric Fiselier#endif
3232e778d10cSEric Fiselier}
3233e778d10cSEric Fiselier
3234e778d10cSEric Fiseliertemplate <class _ForwardIterator, class _Size>
3235e778d10cSEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
3236e778d10cSEric Fiselier_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
3237e778d10cSEric Fiselier    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3238e778d10cSEric Fiselier    auto __idx = __first;
3239e778d10cSEric Fiselier#ifndef _LIBCPP_NO_EXCEPTIONS
3240e778d10cSEric Fiselier    try {
3241e778d10cSEric Fiselier#endif
3242e778d10cSEric Fiselier    for (; __n > 0; (void)++__idx, --__n)
3243e778d10cSEric Fiselier        ::new((void*)_VSTD::addressof(*__idx)) _Vt;
3244e778d10cSEric Fiselier    return __idx;
3245e778d10cSEric Fiselier#ifndef _LIBCPP_NO_EXCEPTIONS
3246e778d10cSEric Fiselier    } catch (...) {
3247e778d10cSEric Fiselier        _VSTD::destroy(__first, __idx);
3248e778d10cSEric Fiselier        throw;
3249e778d10cSEric Fiselier    }
3250e778d10cSEric Fiselier#endif
3251e778d10cSEric Fiselier}
3252e778d10cSEric Fiselier
3253e778d10cSEric Fiselier
3254e778d10cSEric Fiseliertemplate <class _ForwardIterator>
3255e778d10cSEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
3256e778d10cSEric Fiseliervoid uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
3257e778d10cSEric Fiselier    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3258e778d10cSEric Fiselier    auto __idx = __first;
3259e778d10cSEric Fiselier#ifndef _LIBCPP_NO_EXCEPTIONS
3260e778d10cSEric Fiselier    try {
3261e778d10cSEric Fiselier#endif
3262e778d10cSEric Fiselier    for (; __idx != __last; ++__idx)
3263e778d10cSEric Fiselier        ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3264e778d10cSEric Fiselier#ifndef _LIBCPP_NO_EXCEPTIONS
3265e778d10cSEric Fiselier    } catch (...) {
3266e778d10cSEric Fiselier        _VSTD::destroy(__first, __idx);
3267e778d10cSEric Fiselier        throw;
3268e778d10cSEric Fiselier    }
3269e778d10cSEric Fiselier#endif
3270e778d10cSEric Fiselier}
3271e778d10cSEric Fiselier
3272e778d10cSEric Fiseliertemplate <class _ForwardIterator, class _Size>
3273e778d10cSEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
3274e778d10cSEric Fiselier_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
3275e778d10cSEric Fiselier    using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
3276e778d10cSEric Fiselier    auto __idx = __first;
3277e778d10cSEric Fiselier#ifndef _LIBCPP_NO_EXCEPTIONS
3278e778d10cSEric Fiselier    try {
3279e778d10cSEric Fiselier#endif
3280e778d10cSEric Fiselier    for (; __n > 0; (void)++__idx, --__n)
3281e778d10cSEric Fiselier        ::new((void*)_VSTD::addressof(*__idx)) _Vt();
3282e778d10cSEric Fiselier    return __idx;
3283e778d10cSEric Fiselier#ifndef _LIBCPP_NO_EXCEPTIONS
3284e778d10cSEric Fiselier    } catch (...) {
3285e778d10cSEric Fiselier        _VSTD::destroy(__first, __idx);
3286e778d10cSEric Fiselier        throw;
3287e778d10cSEric Fiselier    }
3288e778d10cSEric Fiselier#endif
3289e778d10cSEric Fiselier}
3290e778d10cSEric Fiselier
3291e778d10cSEric Fiselier
3292e778d10cSEric Fiseliertemplate <class _InputIt, class _ForwardIt>
3293e778d10cSEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
3294e778d10cSEric Fiselier_ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
3295e778d10cSEric Fiselier    using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3296e778d10cSEric Fiselier    auto __idx = __first_res;
3297e778d10cSEric Fiselier#ifndef _LIBCPP_NO_EXCEPTIONS
3298e778d10cSEric Fiselier    try {
3299e778d10cSEric Fiselier#endif
3300e778d10cSEric Fiselier    for (; __first != __last; (void)++__idx, ++__first)
3301e778d10cSEric Fiselier        ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3302e778d10cSEric Fiselier    return __idx;
3303e778d10cSEric Fiselier#ifndef _LIBCPP_NO_EXCEPTIONS
3304e778d10cSEric Fiselier    } catch (...) {
3305e778d10cSEric Fiselier        _VSTD::destroy(__first_res, __idx);
3306e778d10cSEric Fiselier        throw;
3307e778d10cSEric Fiselier    }
3308e778d10cSEric Fiselier#endif
3309e778d10cSEric Fiselier}
3310e778d10cSEric Fiselier
3311e778d10cSEric Fiseliertemplate <class _InputIt, class _Size, class _ForwardIt>
3312e778d10cSEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
3313e778d10cSEric Fiselierpair<_InputIt, _ForwardIt>
3314e778d10cSEric Fiselieruninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
3315e778d10cSEric Fiselier    using _Vt = typename iterator_traits<_ForwardIt>::value_type;
3316e778d10cSEric Fiselier    auto __idx = __first_res;
3317e778d10cSEric Fiselier#ifndef _LIBCPP_NO_EXCEPTIONS
3318e778d10cSEric Fiselier    try {
3319e778d10cSEric Fiselier#endif
3320e778d10cSEric Fiselier    for (; __n > 0; ++__idx, (void)++__first, --__n)
3321e778d10cSEric Fiselier        ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
3322e778d10cSEric Fiselier    return {__first, __idx};
3323e778d10cSEric Fiselier#ifndef _LIBCPP_NO_EXCEPTIONS
3324e778d10cSEric Fiselier    } catch (...) {
3325e778d10cSEric Fiselier        _VSTD::destroy(__first_res, __idx);
3326e778d10cSEric Fiselier        throw;
3327e778d10cSEric Fiselier    }
3328e778d10cSEric Fiselier#endif
3329e778d10cSEric Fiselier}
3330e778d10cSEric Fiselier
3331e778d10cSEric Fiselier
3332e4d9c316SEric Fiselier#endif // _LIBCPP_STD_VER > 14
3333e4d9c316SEric Fiselier
3334f08de52dSKevin Hu// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
3335f08de52dSKevin Hu// should be sufficient for thread safety.
33363c35491fSEric Fiselier// See https://bugs.llvm.org/show_bug.cgi?id=22803
3337f08de52dSKevin Hu#if defined(__clang__) && __has_builtin(__atomic_add_fetch)          \
3338f08de52dSKevin Hu                       && defined(__ATOMIC_RELAXED)                  \
3339f08de52dSKevin Hu                       && defined(__ATOMIC_ACQ_REL)
3340f08de52dSKevin Hu#   define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3341ae62727aSRichard Smith#elif defined(_LIBCPP_COMPILER_GCC)
3342f08de52dSKevin Hu#   define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
3343f08de52dSKevin Hu#endif
3344f08de52dSKevin Hu
3345f08de52dSKevin Hutemplate <class _Tp>
3346f08de52dSKevin Huinline _LIBCPP_INLINE_VISIBILITY _Tp
3347f08de52dSKevin Hu__libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
3348f08de52dSKevin Hu{
3349f08de52dSKevin Hu#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3350f08de52dSKevin Hu    return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
3351f08de52dSKevin Hu#else
3352f08de52dSKevin Hu    return __t += 1;
3353f08de52dSKevin Hu#endif
3354f08de52dSKevin Hu}
3355f08de52dSKevin Hu
3356f08de52dSKevin Hutemplate <class _Tp>
3357f08de52dSKevin Huinline _LIBCPP_INLINE_VISIBILITY _Tp
3358f08de52dSKevin Hu__libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
3359f08de52dSKevin Hu{
3360f08de52dSKevin Hu#if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
3361f08de52dSKevin Hu    return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
3362f08de52dSKevin Hu#else
3363f08de52dSKevin Hu    return __t -= 1;
3364f08de52dSKevin Hu#endif
3365f08de52dSKevin Hu}
3366f08de52dSKevin Hu
3367848a5374SHoward Hinnantclass _LIBCPP_EXCEPTION_ABI bad_weak_ptr
33683e519524SHoward Hinnant    : public std::exception
33693e519524SHoward Hinnant{
33703e519524SHoward Hinnantpublic:
33713739fe79SHoward Hinnant    virtual ~bad_weak_ptr() _NOEXCEPT;
33723739fe79SHoward Hinnant    virtual const char* what() const  _NOEXCEPT;
33733e519524SHoward Hinnant};
33743e519524SHoward Hinnant
3375dc7200b4SLouis Dionne_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
3376d437fa5cSMarshall Clowvoid __throw_bad_weak_ptr()
3377d437fa5cSMarshall Clow{
3378d437fa5cSMarshall Clow#ifndef _LIBCPP_NO_EXCEPTIONS
3379d437fa5cSMarshall Clow    throw bad_weak_ptr();
3380d437fa5cSMarshall Clow#else
3381d437fa5cSMarshall Clow    _VSTD::abort();
3382d437fa5cSMarshall Clow#endif
3383d437fa5cSMarshall Clow}
3384d437fa5cSMarshall Clow
3385e2f2d1edSEric Fiseliertemplate<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
33863e519524SHoward Hinnant
3387f0544c20SHoward Hinnantclass _LIBCPP_TYPE_VIS __shared_count
33883e519524SHoward Hinnant{
33893e519524SHoward Hinnant    __shared_count(const __shared_count&);
33903e519524SHoward Hinnant    __shared_count& operator=(const __shared_count&);
33913e519524SHoward Hinnant
33923e519524SHoward Hinnantprotected:
33933e519524SHoward Hinnant    long __shared_owners_;
33943e519524SHoward Hinnant    virtual ~__shared_count();
33953e519524SHoward Hinnantprivate:
33963739fe79SHoward Hinnant    virtual void __on_zero_shared() _NOEXCEPT = 0;
33973e519524SHoward Hinnant
33983e519524SHoward Hinnantpublic:
3399848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
34003739fe79SHoward Hinnant    explicit __shared_count(long __refs = 0) _NOEXCEPT
34013e519524SHoward Hinnant        : __shared_owners_(__refs) {}
34023e519524SHoward Hinnant
3403c8e84ff2SLouis Dionne#if defined(_LIBCPP_BUILDING_LIBRARY) && \
340411f60453SEric Fiselier    defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
3405a480c28bSEric Fiselier    void __add_shared() _NOEXCEPT;
3406a480c28bSEric Fiselier    bool __release_shared() _NOEXCEPT;
3407f08de52dSKevin Hu#else
3408f08de52dSKevin Hu    _LIBCPP_INLINE_VISIBILITY
3409f08de52dSKevin Hu    void __add_shared() _NOEXCEPT {
3410f08de52dSKevin Hu      __libcpp_atomic_refcount_increment(__shared_owners_);
3411f08de52dSKevin Hu    }
3412f08de52dSKevin Hu    _LIBCPP_INLINE_VISIBILITY
3413f08de52dSKevin Hu    bool __release_shared() _NOEXCEPT {
3414f08de52dSKevin Hu      if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
3415f08de52dSKevin Hu        __on_zero_shared();
3416f08de52dSKevin Hu        return true;
3417f08de52dSKevin Hu      }
3418f08de52dSKevin Hu      return false;
3419f08de52dSKevin Hu    }
3420f08de52dSKevin Hu#endif
3421848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
34221faf289eSEric Fiselier    long use_count() const _NOEXCEPT {
34231faf289eSEric Fiselier        return __libcpp_relaxed_load(&__shared_owners_) + 1;
34241faf289eSEric Fiselier    }
34253e519524SHoward Hinnant};
34263e519524SHoward Hinnant
3427f0544c20SHoward Hinnantclass _LIBCPP_TYPE_VIS __shared_weak_count
34283e519524SHoward Hinnant    : private __shared_count
34293e519524SHoward Hinnant{
34303e519524SHoward Hinnant    long __shared_weak_owners_;
34313e519524SHoward Hinnant
34323e519524SHoward Hinnantpublic:
3433848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
34343739fe79SHoward Hinnant    explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
34353e519524SHoward Hinnant        : __shared_count(__refs),
34363e519524SHoward Hinnant          __shared_weak_owners_(__refs) {}
34373e519524SHoward Hinnantprotected:
34383e519524SHoward Hinnant    virtual ~__shared_weak_count();
34393e519524SHoward Hinnant
34403e519524SHoward Hinnantpublic:
3441c8e84ff2SLouis Dionne#if defined(_LIBCPP_BUILDING_LIBRARY) && \
344211f60453SEric Fiselier    defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
3443a480c28bSEric Fiselier    void __add_shared() _NOEXCEPT;
3444a480c28bSEric Fiselier    void __add_weak() _NOEXCEPT;
3445a480c28bSEric Fiselier    void __release_shared() _NOEXCEPT;
3446f08de52dSKevin Hu#else
3447f08de52dSKevin Hu    _LIBCPP_INLINE_VISIBILITY
3448f08de52dSKevin Hu    void __add_shared() _NOEXCEPT {
3449f08de52dSKevin Hu      __shared_count::__add_shared();
3450f08de52dSKevin Hu    }
3451f08de52dSKevin Hu    _LIBCPP_INLINE_VISIBILITY
3452f08de52dSKevin Hu    void __add_weak() _NOEXCEPT {
3453f08de52dSKevin Hu      __libcpp_atomic_refcount_increment(__shared_weak_owners_);
3454f08de52dSKevin Hu    }
3455f08de52dSKevin Hu    _LIBCPP_INLINE_VISIBILITY
3456f08de52dSKevin Hu    void __release_shared() _NOEXCEPT {
3457f08de52dSKevin Hu      if (__shared_count::__release_shared())
3458f08de52dSKevin Hu        __release_weak();
3459f08de52dSKevin Hu    }
3460f08de52dSKevin Hu#endif
34613739fe79SHoward Hinnant    void __release_weak() _NOEXCEPT;
3462848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
34633739fe79SHoward Hinnant    long use_count() const _NOEXCEPT {return __shared_count::use_count();}
34643739fe79SHoward Hinnant    __shared_weak_count* lock() _NOEXCEPT;
34653e519524SHoward Hinnant
3466a9f69800SHoward Hinnant    // Define the function out only if we build static libc++ without RTTI.
3467a9f69800SHoward Hinnant    // Otherwise we may break clients who need to compile their projects with
3468a9f69800SHoward Hinnant    // -fno-rtti and yet link against a libc++.dylib compiled
3469a9f69800SHoward Hinnant    // without -fno-rtti.
3470a9f69800SHoward Hinnant#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
34713739fe79SHoward Hinnant    virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
3472a9f69800SHoward Hinnant#endif
34733e519524SHoward Hinnantprivate:
34743739fe79SHoward Hinnant    virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
34753e519524SHoward Hinnant};
34763e519524SHoward Hinnant
34773e519524SHoward Hinnanttemplate <class _Tp, class _Dp, class _Alloc>
34783e519524SHoward Hinnantclass __shared_ptr_pointer
34793e519524SHoward Hinnant    : public __shared_weak_count
34803e519524SHoward Hinnant{
34813e519524SHoward Hinnant    __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
34823e519524SHoward Hinnantpublic:
3483848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
34843e519524SHoward Hinnant    __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
3485ce48a113SHoward Hinnant        :  __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
34863e519524SHoward Hinnant
348754b409fdSHoward Hinnant#ifndef _LIBCPP_NO_RTTI
34883739fe79SHoward Hinnant    virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
348954b409fdSHoward Hinnant#endif
34903e519524SHoward Hinnant
34913e519524SHoward Hinnantprivate:
34923739fe79SHoward Hinnant    virtual void __on_zero_shared() _NOEXCEPT;
34933739fe79SHoward Hinnant    virtual void __on_zero_shared_weak() _NOEXCEPT;
34943e519524SHoward Hinnant};
34953e519524SHoward Hinnant
349654b409fdSHoward Hinnant#ifndef _LIBCPP_NO_RTTI
349754b409fdSHoward Hinnant
34983e519524SHoward Hinnanttemplate <class _Tp, class _Dp, class _Alloc>
34993e519524SHoward Hinnantconst void*
35003739fe79SHoward Hinnant__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
35013e519524SHoward Hinnant{
35029c973b9fSEric Fiselier    return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
35033e519524SHoward Hinnant}
35043e519524SHoward Hinnant
3505b3371f6fSHoward Hinnant#endif  // _LIBCPP_NO_RTTI
350654b409fdSHoward Hinnant
35073e519524SHoward Hinnanttemplate <class _Tp, class _Dp, class _Alloc>
35083e519524SHoward Hinnantvoid
35093739fe79SHoward Hinnant__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
35103e519524SHoward Hinnant{
35113e519524SHoward Hinnant    __data_.first().second()(__data_.first().first());
35123e519524SHoward Hinnant    __data_.first().second().~_Dp();
35133e519524SHoward Hinnant}
35143e519524SHoward Hinnant
35153e519524SHoward Hinnanttemplate <class _Tp, class _Dp, class _Alloc>
35163e519524SHoward Hinnantvoid
35173739fe79SHoward Hinnant__shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
35183e519524SHoward Hinnant{
35196fe361c1SEric Fiselier    typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
35206fe361c1SEric Fiselier    typedef allocator_traits<_Al> _ATraits;
3521319be722SEric Fiselier    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
3522319be722SEric Fiselier
35236fe361c1SEric Fiselier    _Al __a(__data_.second());
35243e519524SHoward Hinnant    __data_.second().~_Alloc();
3525319be722SEric Fiselier    __a.deallocate(_PTraits::pointer_to(*this), 1);
35263e519524SHoward Hinnant}
35273e519524SHoward Hinnant
35283e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
35293e519524SHoward Hinnantclass __shared_ptr_emplace
35303e519524SHoward Hinnant    : public __shared_weak_count
35313e519524SHoward Hinnant{
35323e519524SHoward Hinnant    __compressed_pair<_Alloc, _Tp> __data_;
35333e519524SHoward Hinnantpublic:
35343e519524SHoward Hinnant#ifndef _LIBCPP_HAS_NO_VARIADICS
35353e519524SHoward Hinnant
3536848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
35373e519524SHoward Hinnant    __shared_ptr_emplace(_Alloc __a)
3538ce48a113SHoward Hinnant        :  __data_(_VSTD::move(__a)) {}
35393e519524SHoward Hinnant
35403e519524SHoward Hinnant    template <class ..._Args>
3541848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
35423e519524SHoward Hinnant        __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
3543a87b5e34SHoward Hinnant            :  __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
3544a87b5e34SHoward Hinnant                   _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
35453e519524SHoward Hinnant
35463e519524SHoward Hinnant#else  // _LIBCPP_HAS_NO_VARIADICS
35473e519524SHoward Hinnant
3548848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
35493e519524SHoward Hinnant    __shared_ptr_emplace(_Alloc __a)
35503e519524SHoward Hinnant        :  __data_(__a) {}
35513e519524SHoward Hinnant
35523e519524SHoward Hinnant    template <class _A0>
3553848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
35543e519524SHoward Hinnant        __shared_ptr_emplace(_Alloc __a, _A0& __a0)
35553e519524SHoward Hinnant            :  __data_(__a, _Tp(__a0)) {}
35563e519524SHoward Hinnant
35573e519524SHoward Hinnant    template <class _A0, class _A1>
3558848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
35593e519524SHoward Hinnant        __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
35603e519524SHoward Hinnant            :  __data_(__a, _Tp(__a0, __a1)) {}
35613e519524SHoward Hinnant
35623e519524SHoward Hinnant    template <class _A0, class _A1, class _A2>
3563848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
35643e519524SHoward Hinnant        __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
35653e519524SHoward Hinnant            :  __data_(__a, _Tp(__a0, __a1, __a2)) {}
35663e519524SHoward Hinnant
35673e519524SHoward Hinnant#endif  // _LIBCPP_HAS_NO_VARIADICS
35683e519524SHoward Hinnant
35693e519524SHoward Hinnantprivate:
35703739fe79SHoward Hinnant    virtual void __on_zero_shared() _NOEXCEPT;
35713739fe79SHoward Hinnant    virtual void __on_zero_shared_weak() _NOEXCEPT;
35723e519524SHoward Hinnantpublic:
3573848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
3574fa8ce345SMarshall Clow    _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
35753e519524SHoward Hinnant};
35763e519524SHoward Hinnant
35773e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
35783e519524SHoward Hinnantvoid
35793739fe79SHoward Hinnant__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
35803e519524SHoward Hinnant{
35813e519524SHoward Hinnant    __data_.second().~_Tp();
35823e519524SHoward Hinnant}
35833e519524SHoward Hinnant
35843e519524SHoward Hinnanttemplate <class _Tp, class _Alloc>
35853e519524SHoward Hinnantvoid
35863739fe79SHoward Hinnant__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
35873e519524SHoward Hinnant{
35886fe361c1SEric Fiselier    typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
35896fe361c1SEric Fiselier    typedef allocator_traits<_Al> _ATraits;
3590319be722SEric Fiselier    typedef pointer_traits<typename _ATraits::pointer> _PTraits;
35916fe361c1SEric Fiselier    _Al __a(__data_.first());
35923e519524SHoward Hinnant    __data_.first().~_Alloc();
3593319be722SEric Fiselier    __a.deallocate(_PTraits::pointer_to(*this), 1);
35943e519524SHoward Hinnant}
35953e519524SHoward Hinnant
359672d0d603SErik Pilkingtonstruct __shared_ptr_dummy_rebind_allocator_type;
359772d0d603SErik Pilkingtontemplate <>
359872d0d603SErik Pilkingtonclass _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
359972d0d603SErik Pilkington{
360072d0d603SErik Pilkingtonpublic:
360172d0d603SErik Pilkington    template <class _Other>
360272d0d603SErik Pilkington    struct rebind
360372d0d603SErik Pilkington    {
360472d0d603SErik Pilkington        typedef allocator<_Other> other;
360572d0d603SErik Pilkington    };
360672d0d603SErik Pilkington};
360772d0d603SErik Pilkington
3608e2f2d1edSEric Fiseliertemplate<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
36093e519524SHoward Hinnant
36103e519524SHoward Hinnanttemplate<class _Tp>
3611e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS shared_ptr
36123e519524SHoward Hinnant{
36133e519524SHoward Hinnantpublic:
36143e519524SHoward Hinnant    typedef _Tp element_type;
3615dc83e779SMarshall Clow
361668436a9bSEric Fiselier#if _LIBCPP_STD_VER > 14
361768436a9bSEric Fiselier    typedef weak_ptr<_Tp> weak_type;
361868436a9bSEric Fiselier#endif
36193e519524SHoward Hinnantprivate:
36203e519524SHoward Hinnant    element_type*      __ptr_;
36213e519524SHoward Hinnant    __shared_weak_count* __cntrl_;
36223e519524SHoward Hinnant
36233e519524SHoward Hinnant    struct __nat {int __for_bool_;};
36243e519524SHoward Hinnantpublic:
3625906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
3626c0937e8aSHoward Hinnant    _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
3627906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
3628c0937e8aSHoward Hinnant    _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
362922747e62SLogan Chien    template<class _Yp>
363022747e62SLogan Chien        explicit shared_ptr(_Yp* __p,
363122747e62SLogan Chien                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
363222747e62SLogan Chien    template<class _Yp, class _Dp>
363322747e62SLogan Chien        shared_ptr(_Yp* __p, _Dp __d,
363422747e62SLogan Chien                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
363522747e62SLogan Chien    template<class _Yp, class _Dp, class _Alloc>
363622747e62SLogan Chien        shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
363722747e62SLogan Chien                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
36383e519524SHoward Hinnant    template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
36393e519524SHoward Hinnant    template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
3640906c872dSEvgeniy Stepanov    template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
3641906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
36423739fe79SHoward Hinnant    shared_ptr(const shared_ptr& __r) _NOEXCEPT;
36433e519524SHoward Hinnant    template<class _Yp>
3644906c872dSEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
36453e519524SHoward Hinnant        shared_ptr(const shared_ptr<_Yp>& __r,
3646dc83e779SMarshall Clow                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
36473739fe79SHoward Hinnant                       _NOEXCEPT;
36487609c9b6SHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3649906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
36503739fe79SHoward Hinnant    shared_ptr(shared_ptr&& __r) _NOEXCEPT;
3651906c872dSEvgeniy Stepanov    template<class _Yp> _LIBCPP_INLINE_VISIBILITY  shared_ptr(shared_ptr<_Yp>&& __r,
3652dc83e779SMarshall Clow                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
36533739fe79SHoward Hinnant                       _NOEXCEPT;
36547609c9b6SHoward Hinnant#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
36553e519524SHoward Hinnant    template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
3656dc83e779SMarshall Clow                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
3657e67179bcSMarshall Clow#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
36587609c9b6SHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
365922747e62SLogan Chien    template<class _Yp>
366022747e62SLogan Chien        shared_ptr(auto_ptr<_Yp>&& __r,
366122747e62SLogan Chien                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
3662b3371f6fSHoward Hinnant#else
366322747e62SLogan Chien    template<class _Yp>
366422747e62SLogan Chien        shared_ptr(auto_ptr<_Yp> __r,
366522747e62SLogan Chien                   typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
36663e519524SHoward Hinnant#endif
3667e67179bcSMarshall Clow#endif
36687609c9b6SHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
366922747e62SLogan Chien    template <class _Yp, class _Dp>
36708e25104bSHoward Hinnant        shared_ptr(unique_ptr<_Yp, _Dp>&&,
367122747e62SLogan Chien                   typename enable_if
36728e25104bSHoward Hinnant                   <
367322747e62SLogan Chien                       !is_lvalue_reference<_Dp>::value &&
36748e25104bSHoward Hinnant                       !is_array<_Yp>::value &&
367522747e62SLogan Chien                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
367622747e62SLogan Chien                       __nat
367722747e62SLogan Chien                   >::type = __nat());
367822747e62SLogan Chien    template <class _Yp, class _Dp>
36798e25104bSHoward Hinnant        shared_ptr(unique_ptr<_Yp, _Dp>&&,
368022747e62SLogan Chien                   typename enable_if
368122747e62SLogan Chien                   <
368222747e62SLogan Chien                       is_lvalue_reference<_Dp>::value &&
368322747e62SLogan Chien                       !is_array<_Yp>::value &&
368422747e62SLogan Chien                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
368522747e62SLogan Chien                       __nat
368622747e62SLogan Chien                   >::type = __nat());
36877609c9b6SHoward Hinnant#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
368822747e62SLogan Chien    template <class _Yp, class _Dp>
36898e25104bSHoward Hinnant        shared_ptr(unique_ptr<_Yp, _Dp>,
369022747e62SLogan Chien                   typename enable_if
369122747e62SLogan Chien                   <
369222747e62SLogan Chien                       !is_lvalue_reference<_Dp>::value &&
369322747e62SLogan Chien                       !is_array<_Yp>::value &&
369422747e62SLogan Chien                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
369522747e62SLogan Chien                       __nat
369622747e62SLogan Chien                   >::type = __nat());
369722747e62SLogan Chien    template <class _Yp, class _Dp>
369822747e62SLogan Chien        shared_ptr(unique_ptr<_Yp, _Dp>,
369922747e62SLogan Chien                   typename enable_if
370022747e62SLogan Chien                   <
370122747e62SLogan Chien                       is_lvalue_reference<_Dp>::value &&
370222747e62SLogan Chien                       !is_array<_Yp>::value &&
370322747e62SLogan Chien                       is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
370422747e62SLogan Chien                       __nat
370522747e62SLogan Chien                   >::type = __nat());
37067609c9b6SHoward Hinnant#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
37073e519524SHoward Hinnant
37083e519524SHoward Hinnant    ~shared_ptr();
37093e519524SHoward Hinnant
3710906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
37113739fe79SHoward Hinnant    shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
37128e25104bSHoward Hinnant    template<class _Yp>
37138e25104bSHoward Hinnant        typename enable_if
37148e25104bSHoward Hinnant        <
37158e25104bSHoward Hinnant            is_convertible<_Yp*, element_type*>::value,
37168e25104bSHoward Hinnant            shared_ptr&
37178e25104bSHoward Hinnant        >::type
3718906c872dSEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
37198e25104bSHoward Hinnant        operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
37207609c9b6SHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3721906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
37223739fe79SHoward Hinnant    shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
37238e25104bSHoward Hinnant    template<class _Yp>
37248e25104bSHoward Hinnant        typename enable_if
37258e25104bSHoward Hinnant        <
37268e25104bSHoward Hinnant            is_convertible<_Yp*, element_type*>::value,
37278e25104bSHoward Hinnant            shared_ptr<_Tp>&
37288e25104bSHoward Hinnant        >::type
3729906c872dSEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
37308e25104bSHoward Hinnant        operator=(shared_ptr<_Yp>&& __r);
3731e67179bcSMarshall Clow#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
37328e25104bSHoward Hinnant    template<class _Yp>
373389dd1dd2SEric Fiselier        _LIBCPP_INLINE_VISIBILITY
37348e25104bSHoward Hinnant        typename enable_if
37358e25104bSHoward Hinnant        <
37368e25104bSHoward Hinnant            !is_array<_Yp>::value &&
37378e25104bSHoward Hinnant            is_convertible<_Yp*, element_type*>::value,
373884f996fbSHoward Hinnant            shared_ptr
373984f996fbSHoward Hinnant        >::type&
37408e25104bSHoward Hinnant        operator=(auto_ptr<_Yp>&& __r);
3741e67179bcSMarshall Clow#endif
37427609c9b6SHoward Hinnant#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3743e67179bcSMarshall Clow#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
37448e25104bSHoward Hinnant    template<class _Yp>
374589dd1dd2SEric Fiselier        _LIBCPP_INLINE_VISIBILITY
37468e25104bSHoward Hinnant        typename enable_if
37478e25104bSHoward Hinnant        <
37488e25104bSHoward Hinnant            !is_array<_Yp>::value &&
37498e25104bSHoward Hinnant            is_convertible<_Yp*, element_type*>::value,
37508e25104bSHoward Hinnant            shared_ptr&
37518e25104bSHoward Hinnant        >::type
37528e25104bSHoward Hinnant        operator=(auto_ptr<_Yp> __r);
37533e519524SHoward Hinnant#endif
3754e67179bcSMarshall Clow#endif
37558e25104bSHoward Hinnant    template <class _Yp, class _Dp>
37568e25104bSHoward Hinnant        typename enable_if
37578e25104bSHoward Hinnant        <
37588e25104bSHoward Hinnant            !is_array<_Yp>::value &&
37598e25104bSHoward Hinnant            is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
37608e25104bSHoward Hinnant            shared_ptr&
37618e25104bSHoward Hinnant        >::type
37627609c9b6SHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3763906c872dSEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
37648e25104bSHoward Hinnant        operator=(unique_ptr<_Yp, _Dp>&& __r);
37657609c9b6SHoward Hinnant#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
376602b8e949SEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
37678e25104bSHoward Hinnant        operator=(unique_ptr<_Yp, _Dp> __r);
37683e519524SHoward Hinnant#endif
37693e519524SHoward Hinnant
3770906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
37713739fe79SHoward Hinnant    void swap(shared_ptr& __r) _NOEXCEPT;
3772906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
37733739fe79SHoward Hinnant    void reset() _NOEXCEPT;
37748e25104bSHoward Hinnant    template<class _Yp>
37758e25104bSHoward Hinnant        typename enable_if
37768e25104bSHoward Hinnant        <
37778e25104bSHoward Hinnant            is_convertible<_Yp*, element_type*>::value,
37788e25104bSHoward Hinnant            void
37798e25104bSHoward Hinnant        >::type
3780906c872dSEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
37818e25104bSHoward Hinnant        reset(_Yp* __p);
37828e25104bSHoward Hinnant    template<class _Yp, class _Dp>
37838e25104bSHoward Hinnant        typename enable_if
37848e25104bSHoward Hinnant        <
37858e25104bSHoward Hinnant            is_convertible<_Yp*, element_type*>::value,
37868e25104bSHoward Hinnant            void
37878e25104bSHoward Hinnant        >::type
3788906c872dSEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
37898e25104bSHoward Hinnant        reset(_Yp* __p, _Dp __d);
37908e25104bSHoward Hinnant    template<class _Yp, class _Dp, class _Alloc>
37918e25104bSHoward Hinnant        typename enable_if
37928e25104bSHoward Hinnant        <
37938e25104bSHoward Hinnant            is_convertible<_Yp*, element_type*>::value,
37948e25104bSHoward Hinnant            void
37958e25104bSHoward Hinnant        >::type
3796906c872dSEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
37978e25104bSHoward Hinnant        reset(_Yp* __p, _Dp __d, _Alloc __a);
37983e519524SHoward Hinnant
3799848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
38003739fe79SHoward Hinnant    element_type* get() const _NOEXCEPT {return __ptr_;}
3801848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
38023739fe79SHoward Hinnant    typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
38033739fe79SHoward Hinnant        {return *__ptr_;}
3804848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
38053739fe79SHoward Hinnant    element_type* operator->() const _NOEXCEPT {return __ptr_;}
3806848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
38073739fe79SHoward Hinnant    long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
3808848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
38093739fe79SHoward Hinnant    bool unique() const _NOEXCEPT {return use_count() == 1;}
3810848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
3811f2f2d8b9SHoward Hinnant    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
3812c003db1fSHoward Hinnant    template <class _Up>
3813848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
381436bc7178SMarshall Clow        bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
38153e519524SHoward Hinnant        {return __cntrl_ < __p.__cntrl_;}
3816c003db1fSHoward Hinnant    template <class _Up>
3817848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
381836bc7178SMarshall Clow        bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
38193e519524SHoward Hinnant        {return __cntrl_ < __p.__cntrl_;}
3820d77851e8SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
3821d77851e8SHoward Hinnant    bool
3822d77851e8SHoward Hinnant    __owner_equivalent(const shared_ptr& __p) const
3823d77851e8SHoward Hinnant        {return __cntrl_ == __p.__cntrl_;}
38243e519524SHoward Hinnant
382554b409fdSHoward Hinnant#ifndef _LIBCPP_NO_RTTI
38263e519524SHoward Hinnant    template <class _Dp>
3827848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
38283739fe79SHoward Hinnant        _Dp* __get_deleter() const _NOEXCEPT
38294a911c86SMarshall Clow            {return static_cast<_Dp*>(__cntrl_
38304a911c86SMarshall Clow                    ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
38314a911c86SMarshall Clow                      : nullptr);}
3832b3371f6fSHoward Hinnant#endif  // _LIBCPP_NO_RTTI
38333e519524SHoward Hinnant
38343e519524SHoward Hinnant#ifndef _LIBCPP_HAS_NO_VARIADICS
38353e519524SHoward Hinnant
38363e519524SHoward Hinnant    template<class ..._Args>
38373e519524SHoward Hinnant        static
38383e519524SHoward Hinnant        shared_ptr<_Tp>
38393e519524SHoward Hinnant        make_shared(_Args&& ...__args);
38403e519524SHoward Hinnant
38413e519524SHoward Hinnant    template<class _Alloc, class ..._Args>
38423e519524SHoward Hinnant        static
38433e519524SHoward Hinnant        shared_ptr<_Tp>
38443e519524SHoward Hinnant        allocate_shared(const _Alloc& __a, _Args&& ...__args);
38453e519524SHoward Hinnant
38463e519524SHoward Hinnant#else  // _LIBCPP_HAS_NO_VARIADICS
38473e519524SHoward Hinnant
38483e519524SHoward Hinnant    static shared_ptr<_Tp> make_shared();
38493e519524SHoward Hinnant
38503e519524SHoward Hinnant    template<class _A0>
38513e519524SHoward Hinnant        static shared_ptr<_Tp> make_shared(_A0&);
38523e519524SHoward Hinnant
38533e519524SHoward Hinnant    template<class _A0, class _A1>
38543e519524SHoward Hinnant        static shared_ptr<_Tp> make_shared(_A0&, _A1&);
38553e519524SHoward Hinnant
38563e519524SHoward Hinnant    template<class _A0, class _A1, class _A2>
38573e519524SHoward Hinnant        static shared_ptr<_Tp> make_shared(_A0&, _A1&, _A2&);
38583e519524SHoward Hinnant
38593e519524SHoward Hinnant    template<class _Alloc>
38603e519524SHoward Hinnant        static shared_ptr<_Tp>
38613e519524SHoward Hinnant        allocate_shared(const _Alloc& __a);
38623e519524SHoward Hinnant
38633e519524SHoward Hinnant    template<class _Alloc, class _A0>
38643e519524SHoward Hinnant        static shared_ptr<_Tp>
38653e519524SHoward Hinnant        allocate_shared(const _Alloc& __a, _A0& __a0);
38663e519524SHoward Hinnant
38673e519524SHoward Hinnant    template<class _Alloc, class _A0, class _A1>
38683e519524SHoward Hinnant        static shared_ptr<_Tp>
38693e519524SHoward Hinnant        allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1);
38703e519524SHoward Hinnant
38713e519524SHoward Hinnant    template<class _Alloc, class _A0, class _A1, class _A2>
38723e519524SHoward Hinnant        static shared_ptr<_Tp>
38733e519524SHoward Hinnant        allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2);
38743e519524SHoward Hinnant
38753e519524SHoward Hinnant#endif  // _LIBCPP_HAS_NO_VARIADICS
38763e519524SHoward Hinnant
38773e519524SHoward Hinnantprivate:
387872d0d603SErik Pilkington    template <class _Yp, bool = is_function<_Yp>::value>
387972d0d603SErik Pilkington        struct __shared_ptr_default_allocator
388072d0d603SErik Pilkington        {
388172d0d603SErik Pilkington            typedef allocator<_Yp> type;
388272d0d603SErik Pilkington        };
388372d0d603SErik Pilkington
388472d0d603SErik Pilkington    template <class _Yp>
388572d0d603SErik Pilkington        struct __shared_ptr_default_allocator<_Yp, true>
388672d0d603SErik Pilkington        {
388772d0d603SErik Pilkington            typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
388872d0d603SErik Pilkington        };
38893e519524SHoward Hinnant
389039005d30SEric Fiselier    template <class _Yp, class _OrigPtr>
3891848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
3892f09df772SEric Fiselier        typename enable_if<is_convertible<_OrigPtr*,
3893f09df772SEric Fiselier                                          const enable_shared_from_this<_Yp>*
3894f09df772SEric Fiselier        >::value,
3895f09df772SEric Fiselier            void>::type
389639005d30SEric Fiselier        __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
389739005d30SEric Fiselier                           _OrigPtr* __ptr) _NOEXCEPT
38983e519524SHoward Hinnant        {
389939005d30SEric Fiselier            typedef typename remove_cv<_Yp>::type _RawYp;
390088f5bfdcSEric Fiselier            if (__e && __e->__weak_this_.expired())
390155112defSMarshall Clow            {
390239005d30SEric Fiselier                __e->__weak_this_ = shared_ptr<_RawYp>(*this,
390339005d30SEric Fiselier                    const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
390455112defSMarshall Clow            }
39053e519524SHoward Hinnant        }
39063e519524SHoward Hinnant
390772d0d603SErik Pilkington    _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
39083e519524SHoward Hinnant
3909e2f2d1edSEric Fiselier    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
3910e2f2d1edSEric Fiselier    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
39113e519524SHoward Hinnant};
39123e519524SHoward Hinnant
3913f09df772SEric Fiselier
39143e519524SHoward Hinnanttemplate<class _Tp>
3915906c872dSEvgeniy Stepanovinline
3916c0937e8aSHoward Hinnant_LIBCPP_CONSTEXPR
39173739fe79SHoward Hinnantshared_ptr<_Tp>::shared_ptr() _NOEXCEPT
39183e519524SHoward Hinnant    : __ptr_(0),
39193e519524SHoward Hinnant      __cntrl_(0)
39203e519524SHoward Hinnant{
39213e519524SHoward Hinnant}
39223e519524SHoward Hinnant
39233e519524SHoward Hinnanttemplate<class _Tp>
3924906c872dSEvgeniy Stepanovinline
3925c0937e8aSHoward Hinnant_LIBCPP_CONSTEXPR
39263739fe79SHoward Hinnantshared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
39273e519524SHoward Hinnant    : __ptr_(0),
39283e519524SHoward Hinnant      __cntrl_(0)
39293e519524SHoward Hinnant{
39303e519524SHoward Hinnant}
39313e519524SHoward Hinnant
39323e519524SHoward Hinnanttemplate<class _Tp>
393322747e62SLogan Chientemplate<class _Yp>
393422747e62SLogan Chienshared_ptr<_Tp>::shared_ptr(_Yp* __p,
393522747e62SLogan Chien                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
39363e519524SHoward Hinnant    : __ptr_(__p)
39373e519524SHoward Hinnant{
39383e519524SHoward Hinnant    unique_ptr<_Yp> __hold(__p);
393972d0d603SErik Pilkington    typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
394072d0d603SErik Pilkington    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
394172d0d603SErik Pilkington    __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
39423e519524SHoward Hinnant    __hold.release();
394339005d30SEric Fiselier    __enable_weak_this(__p, __p);
39443e519524SHoward Hinnant}
39453e519524SHoward Hinnant
39463e519524SHoward Hinnanttemplate<class _Tp>
394722747e62SLogan Chientemplate<class _Yp, class _Dp>
394822747e62SLogan Chienshared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
394922747e62SLogan Chien                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
39503e519524SHoward Hinnant    : __ptr_(__p)
39513e519524SHoward Hinnant{
39523e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
39533e519524SHoward Hinnant    try
39543e519524SHoward Hinnant    {
3955b3371f6fSHoward Hinnant#endif  // _LIBCPP_NO_EXCEPTIONS
395672d0d603SErik Pilkington        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
395772d0d603SErik Pilkington        typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
395872d0d603SErik Pilkington        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
395939005d30SEric Fiselier        __enable_weak_this(__p, __p);
39603e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
39613e519524SHoward Hinnant    }
39623e519524SHoward Hinnant    catch (...)
39633e519524SHoward Hinnant    {
39643e519524SHoward Hinnant        __d(__p);
39653e519524SHoward Hinnant        throw;
39663e519524SHoward Hinnant    }
3967b3371f6fSHoward Hinnant#endif  // _LIBCPP_NO_EXCEPTIONS
39683e519524SHoward Hinnant}
39693e519524SHoward Hinnant
39703e519524SHoward Hinnanttemplate<class _Tp>
39713e519524SHoward Hinnanttemplate<class _Dp>
39723e519524SHoward Hinnantshared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
39733e519524SHoward Hinnant    : __ptr_(0)
39743e519524SHoward Hinnant{
39753e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
39763e519524SHoward Hinnant    try
39773e519524SHoward Hinnant    {
3978b3371f6fSHoward Hinnant#endif  // _LIBCPP_NO_EXCEPTIONS
397972d0d603SErik Pilkington        typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
398072d0d603SErik Pilkington        typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
398172d0d603SErik Pilkington        __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
39823e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
39833e519524SHoward Hinnant    }
39843e519524SHoward Hinnant    catch (...)
39853e519524SHoward Hinnant    {
39863e519524SHoward Hinnant        __d(__p);
39873e519524SHoward Hinnant        throw;
39883e519524SHoward Hinnant    }
3989b3371f6fSHoward Hinnant#endif  // _LIBCPP_NO_EXCEPTIONS
39903e519524SHoward Hinnant}
39913e519524SHoward Hinnant
39923e519524SHoward Hinnanttemplate<class _Tp>
399322747e62SLogan Chientemplate<class _Yp, class _Dp, class _Alloc>
399422747e62SLogan Chienshared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
399522747e62SLogan Chien                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
39963e519524SHoward Hinnant    : __ptr_(__p)
39973e519524SHoward Hinnant{
39983e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
39993e519524SHoward Hinnant    try
40003e519524SHoward Hinnant    {
4001b3371f6fSHoward Hinnant#endif  // _LIBCPP_NO_EXCEPTIONS
40023e519524SHoward Hinnant        typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
4003319be722SEric Fiselier        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
40043e519524SHoward Hinnant        typedef __allocator_destructor<_A2> _D2;
40053e519524SHoward Hinnant        _A2 __a2(__a);
40063e519524SHoward Hinnant        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4007319be722SEric Fiselier        ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4008319be722SEric Fiselier            _CntrlBlk(__p, __d, __a);
4009319be722SEric Fiselier        __cntrl_ = _VSTD::addressof(*__hold2.release());
401039005d30SEric Fiselier        __enable_weak_this(__p, __p);
40113e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
40123e519524SHoward Hinnant    }
40133e519524SHoward Hinnant    catch (...)
40143e519524SHoward Hinnant    {
40153e519524SHoward Hinnant        __d(__p);
40163e519524SHoward Hinnant        throw;
40173e519524SHoward Hinnant    }
4018b3371f6fSHoward Hinnant#endif  // _LIBCPP_NO_EXCEPTIONS
40193e519524SHoward Hinnant}
40203e519524SHoward Hinnant
40213e519524SHoward Hinnanttemplate<class _Tp>
40223e519524SHoward Hinnanttemplate<class _Dp, class _Alloc>
40233e519524SHoward Hinnantshared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
40243e519524SHoward Hinnant    : __ptr_(0)
40253e519524SHoward Hinnant{
40263e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
40273e519524SHoward Hinnant    try
40283e519524SHoward Hinnant    {
4029b3371f6fSHoward Hinnant#endif  // _LIBCPP_NO_EXCEPTIONS
40303e519524SHoward Hinnant        typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
4031319be722SEric Fiselier        typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
40323e519524SHoward Hinnant        typedef __allocator_destructor<_A2> _D2;
40333e519524SHoward Hinnant        _A2 __a2(__a);
40343e519524SHoward Hinnant        unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4035319be722SEric Fiselier        ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4036319be722SEric Fiselier            _CntrlBlk(__p, __d, __a);
4037319be722SEric Fiselier        __cntrl_ = _VSTD::addressof(*__hold2.release());
40383e519524SHoward Hinnant#ifndef _LIBCPP_NO_EXCEPTIONS
40393e519524SHoward Hinnant    }
40403e519524SHoward Hinnant    catch (...)
40413e519524SHoward Hinnant    {
40423e519524SHoward Hinnant        __d(__p);
40433e519524SHoward Hinnant        throw;
40443e519524SHoward Hinnant    }
4045b3371f6fSHoward Hinnant#endif  // _LIBCPP_NO_EXCEPTIONS
40463e519524SHoward Hinnant}
40473e519524SHoward Hinnant
40483e519524SHoward Hinnanttemplate<class _Tp>
40493e519524SHoward Hinnanttemplate<class _Yp>
4050906c872dSEvgeniy Stepanovinline
40513739fe79SHoward Hinnantshared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
40523e519524SHoward Hinnant    : __ptr_(__p),
40533e519524SHoward Hinnant      __cntrl_(__r.__cntrl_)
40543e519524SHoward Hinnant{
40553e519524SHoward Hinnant    if (__cntrl_)
40563e519524SHoward Hinnant        __cntrl_->__add_shared();
40573e519524SHoward Hinnant}
40583e519524SHoward Hinnant
40593e519524SHoward Hinnanttemplate<class _Tp>
4060906c872dSEvgeniy Stepanovinline
40613739fe79SHoward Hinnantshared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
40623e519524SHoward Hinnant    : __ptr_(__r.__ptr_),
40633e519524SHoward Hinnant      __cntrl_(__r.__cntrl_)
40643e519524SHoward Hinnant{
40653e519524SHoward Hinnant    if (__cntrl_)
40663e519524SHoward Hinnant        __cntrl_->__add_shared();
40673e519524SHoward Hinnant}
40683e519524SHoward Hinnant
40693e519524SHoward Hinnanttemplate<class _Tp>
40703e519524SHoward Hinnanttemplate<class _Yp>
4071906c872dSEvgeniy Stepanovinline
40723e519524SHoward Hinnantshared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
4073dc83e779SMarshall Clow                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
40743739fe79SHoward Hinnant         _NOEXCEPT
40753e519524SHoward Hinnant    : __ptr_(__r.__ptr_),
40763e519524SHoward Hinnant      __cntrl_(__r.__cntrl_)
40773e519524SHoward Hinnant{
40783e519524SHoward Hinnant    if (__cntrl_)
40793e519524SHoward Hinnant        __cntrl_->__add_shared();
40803e519524SHoward Hinnant}
40813e519524SHoward Hinnant
40827609c9b6SHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
40833e519524SHoward Hinnant
40843e519524SHoward Hinnanttemplate<class _Tp>
4085906c872dSEvgeniy Stepanovinline
40863739fe79SHoward Hinnantshared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
40873e519524SHoward Hinnant    : __ptr_(__r.__ptr_),
40883e519524SHoward Hinnant      __cntrl_(__r.__cntrl_)
40893e519524SHoward Hinnant{
40903e519524SHoward Hinnant    __r.__ptr_ = 0;
40913e519524SHoward Hinnant    __r.__cntrl_ = 0;
40923e519524SHoward Hinnant}
40933e519524SHoward Hinnant
40943e519524SHoward Hinnanttemplate<class _Tp>
40953e519524SHoward Hinnanttemplate<class _Yp>
4096906c872dSEvgeniy Stepanovinline
40973e519524SHoward Hinnantshared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
4098dc83e779SMarshall Clow                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
40993739fe79SHoward Hinnant         _NOEXCEPT
41003e519524SHoward Hinnant    : __ptr_(__r.__ptr_),
41013e519524SHoward Hinnant      __cntrl_(__r.__cntrl_)
41023e519524SHoward Hinnant{
41033e519524SHoward Hinnant    __r.__ptr_ = 0;
41043e519524SHoward Hinnant    __r.__cntrl_ = 0;
41053e519524SHoward Hinnant}
41063e519524SHoward Hinnant
41077609c9b6SHoward Hinnant#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
41083e519524SHoward Hinnant
4109e67179bcSMarshall Clow#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
41103e519524SHoward Hinnanttemplate<class _Tp>
411122747e62SLogan Chientemplate<class _Yp>
41127609c9b6SHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
411322747e62SLogan Chienshared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
41143e519524SHoward Hinnant#else
411522747e62SLogan Chienshared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
41163e519524SHoward Hinnant#endif
411722747e62SLogan Chien                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
41183e519524SHoward Hinnant    : __ptr_(__r.get())
41193e519524SHoward Hinnant{
41203e519524SHoward Hinnant    typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
41213e519524SHoward Hinnant    __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
412239005d30SEric Fiselier    __enable_weak_this(__r.get(), __r.get());
41233e519524SHoward Hinnant    __r.release();
41243e519524SHoward Hinnant}
4125e67179bcSMarshall Clow#endif
41263e519524SHoward Hinnant
41273e519524SHoward Hinnanttemplate<class _Tp>
412822747e62SLogan Chientemplate <class _Yp, class _Dp>
41297609c9b6SHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
41303e519524SHoward Hinnantshared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
41313e519524SHoward Hinnant#else
41323e519524SHoward Hinnantshared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
41333e519524SHoward Hinnant#endif
413422747e62SLogan Chien                            typename enable_if
413522747e62SLogan Chien                            <
413622747e62SLogan Chien                                !is_lvalue_reference<_Dp>::value &&
413722747e62SLogan Chien                                !is_array<_Yp>::value &&
413822747e62SLogan Chien                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
413922747e62SLogan Chien                                __nat
414022747e62SLogan Chien                            >::type)
41413e519524SHoward Hinnant    : __ptr_(__r.get())
41423e519524SHoward Hinnant{
4143c34f847bSMarshall Clow#if _LIBCPP_STD_VER > 11
4144c34f847bSMarshall Clow    if (__ptr_ == nullptr)
4145c34f847bSMarshall Clow        __cntrl_ = nullptr;
4146c34f847bSMarshall Clow    else
4147c34f847bSMarshall Clow#endif
4148c34f847bSMarshall Clow    {
414972d0d603SErik Pilkington        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
415072d0d603SErik Pilkington        typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
415172d0d603SErik Pilkington        __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
415239005d30SEric Fiselier        __enable_weak_this(__r.get(), __r.get());
4153c34f847bSMarshall Clow    }
41543e519524SHoward Hinnant    __r.release();
41553e519524SHoward Hinnant}
41563e519524SHoward Hinnant
41573e519524SHoward Hinnanttemplate<class _Tp>
415822747e62SLogan Chientemplate <class _Yp, class _Dp>
41597609c9b6SHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
41603e519524SHoward Hinnantshared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
41613e519524SHoward Hinnant#else
41623e519524SHoward Hinnantshared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
41633e519524SHoward Hinnant#endif
416422747e62SLogan Chien                            typename enable_if
416522747e62SLogan Chien                            <
416622747e62SLogan Chien                                is_lvalue_reference<_Dp>::value &&
416722747e62SLogan Chien                                !is_array<_Yp>::value &&
416822747e62SLogan Chien                                is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
416922747e62SLogan Chien                                __nat
417022747e62SLogan Chien                            >::type)
41713e519524SHoward Hinnant    : __ptr_(__r.get())
41723e519524SHoward Hinnant{
4173c34f847bSMarshall Clow#if _LIBCPP_STD_VER > 11
4174c34f847bSMarshall Clow    if (__ptr_ == nullptr)
4175c34f847bSMarshall Clow        __cntrl_ = nullptr;
4176c34f847bSMarshall Clow    else
4177c34f847bSMarshall Clow#endif
4178c34f847bSMarshall Clow    {
417972d0d603SErik Pilkington        typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
41803e519524SHoward Hinnant        typedef __shared_ptr_pointer<_Yp*,
41813e519524SHoward Hinnant                                     reference_wrapper<typename remove_reference<_Dp>::type>,
418272d0d603SErik Pilkington                                     _AllocT > _CntrlBlk;
418372d0d603SErik Pilkington        __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
418439005d30SEric Fiselier        __enable_weak_this(__r.get(), __r.get());
4185c34f847bSMarshall Clow    }
41863e519524SHoward Hinnant    __r.release();
41873e519524SHoward Hinnant}
41883e519524SHoward Hinnant
41893e519524SHoward Hinnant#ifndef _LIBCPP_HAS_NO_VARIADICS
41903e519524SHoward Hinnant
41913e519524SHoward Hinnanttemplate<class _Tp>
41923e519524SHoward Hinnanttemplate<class ..._Args>
41933e519524SHoward Hinnantshared_ptr<_Tp>
41943e519524SHoward Hinnantshared_ptr<_Tp>::make_shared(_Args&& ...__args)
41953e519524SHoward Hinnant{
419677009129SMarshall Clow    static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared" );
41973e519524SHoward Hinnant    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
41983e519524SHoward Hinnant    typedef allocator<_CntrlBlk> _A2;
41993e519524SHoward Hinnant    typedef __allocator_destructor<_A2> _D2;
42003e519524SHoward Hinnant    _A2 __a2;
42013e519524SHoward Hinnant    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4202ce48a113SHoward Hinnant    ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
42033e519524SHoward Hinnant    shared_ptr<_Tp> __r;
42043e519524SHoward Hinnant    __r.__ptr_ = __hold2.get()->get();
42053e519524SHoward Hinnant    __r.__cntrl_ = __hold2.release();
420639005d30SEric Fiselier    __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
42073e519524SHoward Hinnant    return __r;
42083e519524SHoward Hinnant}
42093e519524SHoward Hinnant
42103e519524SHoward Hinnanttemplate<class _Tp>
42113e519524SHoward Hinnanttemplate<class _Alloc, class ..._Args>
42123e519524SHoward Hinnantshared_ptr<_Tp>
42133e519524SHoward Hinnantshared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
42143e519524SHoward Hinnant{
421577009129SMarshall Clow    static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared" );
42163e519524SHoward Hinnant    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4217319be722SEric Fiselier    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
42183e519524SHoward Hinnant    typedef __allocator_destructor<_A2> _D2;
42193e519524SHoward Hinnant    _A2 __a2(__a);
42203e519524SHoward Hinnant    unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
4221319be722SEric Fiselier    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4222319be722SEric Fiselier        _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
42233e519524SHoward Hinnant    shared_ptr<_Tp> __r;
42243e519524SHoward Hinnant    __r.__ptr_ = __hold2.get()->get();
4225319be722SEric Fiselier    __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
422639005d30SEric Fiselier    __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
42273e519524SHoward Hinnant    return __r;
42283e519524SHoward Hinnant}
42293e519524SHoward Hinnant
42303e519524SHoward Hinnant#else  // _LIBCPP_HAS_NO_VARIADICS
42313e519524SHoward Hinnant
42323e519524SHoward Hinnanttemplate<class _Tp>
42333e519524SHoward Hinnantshared_ptr<_Tp>
42343e519524SHoward Hinnantshared_ptr<_Tp>::make_shared()
42353e519524SHoward Hinnant{
423677009129SMarshall Clow    static_assert((is_constructible<_Tp>::value), "Can't construct object in make_shared" );
42373e519524SHoward Hinnant    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
42383e519524SHoward Hinnant    typedef allocator<_CntrlBlk> _Alloc2;
42393e519524SHoward Hinnant    typedef __allocator_destructor<_Alloc2> _D2;
42403e519524SHoward Hinnant    _Alloc2 __alloc2;
42413e519524SHoward Hinnant    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
42423e519524SHoward Hinnant    ::new(__hold2.get()) _CntrlBlk(__alloc2);
42433e519524SHoward Hinnant    shared_ptr<_Tp> __r;
42443e519524SHoward Hinnant    __r.__ptr_ = __hold2.get()->get();
42453e519524SHoward Hinnant    __r.__cntrl_ = __hold2.release();
424639005d30SEric Fiselier    __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
42473e519524SHoward Hinnant    return __r;
42483e519524SHoward Hinnant}
42493e519524SHoward Hinnant
42503e519524SHoward Hinnanttemplate<class _Tp>
42513e519524SHoward Hinnanttemplate<class _A0>
42523e519524SHoward Hinnantshared_ptr<_Tp>
42533e519524SHoward Hinnantshared_ptr<_Tp>::make_shared(_A0& __a0)
42543e519524SHoward Hinnant{
425577009129SMarshall Clow    static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in make_shared" );
42563e519524SHoward Hinnant    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
42573e519524SHoward Hinnant    typedef allocator<_CntrlBlk> _Alloc2;
42583e519524SHoward Hinnant    typedef __allocator_destructor<_Alloc2> _D2;
42593e519524SHoward Hinnant    _Alloc2 __alloc2;
42603e519524SHoward Hinnant    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
42613e519524SHoward Hinnant    ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0);
42623e519524SHoward Hinnant    shared_ptr<_Tp> __r;
42633e519524SHoward Hinnant    __r.__ptr_ = __hold2.get()->get();
42643e519524SHoward Hinnant    __r.__cntrl_ = __hold2.release();
426539005d30SEric Fiselier    __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
42663e519524SHoward Hinnant    return __r;
42673e519524SHoward Hinnant}
42683e519524SHoward Hinnant
42693e519524SHoward Hinnanttemplate<class _Tp>
42703e519524SHoward Hinnanttemplate<class _A0, class _A1>
42713e519524SHoward Hinnantshared_ptr<_Tp>
42723e519524SHoward Hinnantshared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1)
42733e519524SHoward Hinnant{
427477009129SMarshall Clow    static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in make_shared" );
42753e519524SHoward Hinnant    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
42763e519524SHoward Hinnant    typedef allocator<_CntrlBlk> _Alloc2;
42773e519524SHoward Hinnant    typedef __allocator_destructor<_Alloc2> _D2;
42783e519524SHoward Hinnant    _Alloc2 __alloc2;
42793e519524SHoward Hinnant    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
42803e519524SHoward Hinnant    ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1);
42813e519524SHoward Hinnant    shared_ptr<_Tp> __r;
42823e519524SHoward Hinnant    __r.__ptr_ = __hold2.get()->get();
42833e519524SHoward Hinnant    __r.__cntrl_ = __hold2.release();
428439005d30SEric Fiselier    __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
42853e519524SHoward Hinnant    return __r;
42863e519524SHoward Hinnant}
42873e519524SHoward Hinnant
42883e519524SHoward Hinnanttemplate<class _Tp>
42893e519524SHoward Hinnanttemplate<class _A0, class _A1, class _A2>
42903e519524SHoward Hinnantshared_ptr<_Tp>
42913e519524SHoward Hinnantshared_ptr<_Tp>::make_shared(_A0& __a0, _A1& __a1, _A2& __a2)
42923e519524SHoward Hinnant{
429377009129SMarshall Clow    static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in make_shared" );
42943e519524SHoward Hinnant    typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
42953e519524SHoward Hinnant    typedef allocator<_CntrlBlk> _Alloc2;
42963e519524SHoward Hinnant    typedef __allocator_destructor<_Alloc2> _D2;
42973e519524SHoward Hinnant    _Alloc2 __alloc2;
42983e519524SHoward Hinnant    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
42993e519524SHoward Hinnant    ::new(__hold2.get()) _CntrlBlk(__alloc2, __a0, __a1, __a2);
43003e519524SHoward Hinnant    shared_ptr<_Tp> __r;
43013e519524SHoward Hinnant    __r.__ptr_ = __hold2.get()->get();
43023e519524SHoward Hinnant    __r.__cntrl_ = __hold2.release();
430339005d30SEric Fiselier    __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
43043e519524SHoward Hinnant    return __r;
43053e519524SHoward Hinnant}
43063e519524SHoward Hinnant
43073e519524SHoward Hinnanttemplate<class _Tp>
43083e519524SHoward Hinnanttemplate<class _Alloc>
43093e519524SHoward Hinnantshared_ptr<_Tp>
43103e519524SHoward Hinnantshared_ptr<_Tp>::allocate_shared(const _Alloc& __a)
43113e519524SHoward Hinnant{
431277009129SMarshall Clow    static_assert((is_constructible<_Tp>::value), "Can't construct object in allocate_shared" );
43133e519524SHoward Hinnant    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4314319be722SEric Fiselier    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
43153e519524SHoward Hinnant    typedef __allocator_destructor<_Alloc2> _D2;
43163e519524SHoward Hinnant    _Alloc2 __alloc2(__a);
43173e519524SHoward Hinnant    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4318319be722SEric Fiselier    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4319319be722SEric Fiselier        _CntrlBlk(__a);
43203e519524SHoward Hinnant    shared_ptr<_Tp> __r;
43213e519524SHoward Hinnant    __r.__ptr_ = __hold2.get()->get();
4322319be722SEric Fiselier    __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
432339005d30SEric Fiselier    __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
43243e519524SHoward Hinnant    return __r;
43253e519524SHoward Hinnant}
43263e519524SHoward Hinnant
43273e519524SHoward Hinnanttemplate<class _Tp>
43283e519524SHoward Hinnanttemplate<class _Alloc, class _A0>
43293e519524SHoward Hinnantshared_ptr<_Tp>
43303e519524SHoward Hinnantshared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0)
43313e519524SHoward Hinnant{
433277009129SMarshall Clow    static_assert((is_constructible<_Tp, _A0>::value), "Can't construct object in allocate_shared" );
43333e519524SHoward Hinnant    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4334319be722SEric Fiselier    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
43353e519524SHoward Hinnant    typedef __allocator_destructor<_Alloc2> _D2;
43363e519524SHoward Hinnant    _Alloc2 __alloc2(__a);
43373e519524SHoward Hinnant    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4338319be722SEric Fiselier    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4339319be722SEric Fiselier        _CntrlBlk(__a, __a0);
43403e519524SHoward Hinnant    shared_ptr<_Tp> __r;
43413e519524SHoward Hinnant    __r.__ptr_ = __hold2.get()->get();
4342319be722SEric Fiselier    __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
434339005d30SEric Fiselier    __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
43443e519524SHoward Hinnant    return __r;
43453e519524SHoward Hinnant}
43463e519524SHoward Hinnant
43473e519524SHoward Hinnanttemplate<class _Tp>
43483e519524SHoward Hinnanttemplate<class _Alloc, class _A0, class _A1>
43493e519524SHoward Hinnantshared_ptr<_Tp>
43503e519524SHoward Hinnantshared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
43513e519524SHoward Hinnant{
435277009129SMarshall Clow    static_assert((is_constructible<_Tp, _A0, _A1>::value), "Can't construct object in allocate_shared" );
43533e519524SHoward Hinnant    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4354319be722SEric Fiselier    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
43553e519524SHoward Hinnant    typedef __allocator_destructor<_Alloc2> _D2;
43563e519524SHoward Hinnant    _Alloc2 __alloc2(__a);
43573e519524SHoward Hinnant    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4358319be722SEric Fiselier    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4359319be722SEric Fiselier        _CntrlBlk(__a, __a0, __a1);
43603e519524SHoward Hinnant    shared_ptr<_Tp> __r;
43613e519524SHoward Hinnant    __r.__ptr_ = __hold2.get()->get();
4362319be722SEric Fiselier    __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
436339005d30SEric Fiselier    __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
43643e519524SHoward Hinnant    return __r;
43653e519524SHoward Hinnant}
43663e519524SHoward Hinnant
43673e519524SHoward Hinnanttemplate<class _Tp>
43683e519524SHoward Hinnanttemplate<class _Alloc, class _A0, class _A1, class _A2>
43693e519524SHoward Hinnantshared_ptr<_Tp>
43703e519524SHoward Hinnantshared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
43713e519524SHoward Hinnant{
437277009129SMarshall Clow    static_assert((is_constructible<_Tp, _A0, _A1, _A2>::value), "Can't construct object in allocate_shared" );
43733e519524SHoward Hinnant    typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
4374319be722SEric Fiselier    typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _Alloc2;
43753e519524SHoward Hinnant    typedef __allocator_destructor<_Alloc2> _D2;
43763e519524SHoward Hinnant    _Alloc2 __alloc2(__a);
43773e519524SHoward Hinnant    unique_ptr<_CntrlBlk, _D2> __hold2(__alloc2.allocate(1), _D2(__alloc2, 1));
4378319be722SEric Fiselier    ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
4379319be722SEric Fiselier        _CntrlBlk(__a, __a0, __a1, __a2);
43803e519524SHoward Hinnant    shared_ptr<_Tp> __r;
43813e519524SHoward Hinnant    __r.__ptr_ = __hold2.get()->get();
4382319be722SEric Fiselier    __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
438339005d30SEric Fiselier    __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
43843e519524SHoward Hinnant    return __r;
43853e519524SHoward Hinnant}
43863e519524SHoward Hinnant
43873e519524SHoward Hinnant#endif  // _LIBCPP_HAS_NO_VARIADICS
43883e519524SHoward Hinnant
43893e519524SHoward Hinnanttemplate<class _Tp>
43903e519524SHoward Hinnantshared_ptr<_Tp>::~shared_ptr()
43913e519524SHoward Hinnant{
43923e519524SHoward Hinnant    if (__cntrl_)
43933e519524SHoward Hinnant        __cntrl_->__release_shared();
43943e519524SHoward Hinnant}
43953e519524SHoward Hinnant
43963e519524SHoward Hinnanttemplate<class _Tp>
4397906c872dSEvgeniy Stepanovinline
43983e519524SHoward Hinnantshared_ptr<_Tp>&
43993739fe79SHoward Hinnantshared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
44003e519524SHoward Hinnant{
44013e519524SHoward Hinnant    shared_ptr(__r).swap(*this);
44023e519524SHoward Hinnant    return *this;
44033e519524SHoward Hinnant}
44043e519524SHoward Hinnant
44053e519524SHoward Hinnanttemplate<class _Tp>
44063e519524SHoward Hinnanttemplate<class _Yp>
4407906c872dSEvgeniy Stepanovinline
44088e25104bSHoward Hinnanttypename enable_if
44098e25104bSHoward Hinnant<
4410dc83e779SMarshall Clow    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
44113e519524SHoward Hinnant    shared_ptr<_Tp>&
44128e25104bSHoward Hinnant>::type
44133739fe79SHoward Hinnantshared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
44143e519524SHoward Hinnant{
44153e519524SHoward Hinnant    shared_ptr(__r).swap(*this);
44163e519524SHoward Hinnant    return *this;
44173e519524SHoward Hinnant}
44183e519524SHoward Hinnant
44197609c9b6SHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
44203e519524SHoward Hinnant
44213e519524SHoward Hinnanttemplate<class _Tp>
4422906c872dSEvgeniy Stepanovinline
44233e519524SHoward Hinnantshared_ptr<_Tp>&
44243739fe79SHoward Hinnantshared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
44253e519524SHoward Hinnant{
4426ce48a113SHoward Hinnant    shared_ptr(_VSTD::move(__r)).swap(*this);
44273e519524SHoward Hinnant    return *this;
44283e519524SHoward Hinnant}
44293e519524SHoward Hinnant
44303e519524SHoward Hinnanttemplate<class _Tp>
44313e519524SHoward Hinnanttemplate<class _Yp>
4432906c872dSEvgeniy Stepanovinline
44338e25104bSHoward Hinnanttypename enable_if
44348e25104bSHoward Hinnant<
4435dc83e779SMarshall Clow    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
44363e519524SHoward Hinnant    shared_ptr<_Tp>&
44378e25104bSHoward Hinnant>::type
44383e519524SHoward Hinnantshared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
44393e519524SHoward Hinnant{
4440ce48a113SHoward Hinnant    shared_ptr(_VSTD::move(__r)).swap(*this);
44413e519524SHoward Hinnant    return *this;
44423e519524SHoward Hinnant}
44433e519524SHoward Hinnant
4444e67179bcSMarshall Clow#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
44453e519524SHoward Hinnanttemplate<class _Tp>
44463e519524SHoward Hinnanttemplate<class _Yp>
4447906c872dSEvgeniy Stepanovinline
44488e25104bSHoward Hinnanttypename enable_if
44498e25104bSHoward Hinnant<
44508e25104bSHoward Hinnant    !is_array<_Yp>::value &&
4451dc83e779SMarshall Clow    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
445284f996fbSHoward Hinnant    shared_ptr<_Tp>
445384f996fbSHoward Hinnant>::type&
44543e519524SHoward Hinnantshared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
44553e519524SHoward Hinnant{
4456ce48a113SHoward Hinnant    shared_ptr(_VSTD::move(__r)).swap(*this);
44573e519524SHoward Hinnant    return *this;
44583e519524SHoward Hinnant}
4459e67179bcSMarshall Clow#endif
44603e519524SHoward Hinnant
44613e519524SHoward Hinnanttemplate<class _Tp>
44623e519524SHoward Hinnanttemplate <class _Yp, class _Dp>
4463906c872dSEvgeniy Stepanovinline
44648e25104bSHoward Hinnanttypename enable_if
44658e25104bSHoward Hinnant<
44668e25104bSHoward Hinnant    !is_array<_Yp>::value &&
4467dc83e779SMarshall Clow    is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
4468dc83e779SMarshall Clow                   typename shared_ptr<_Tp>::element_type*>::value,
44693e519524SHoward Hinnant    shared_ptr<_Tp>&
44708e25104bSHoward Hinnant>::type
44713e519524SHoward Hinnantshared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
44723e519524SHoward Hinnant{
4473ce48a113SHoward Hinnant    shared_ptr(_VSTD::move(__r)).swap(*this);
44743e519524SHoward Hinnant    return *this;
44753e519524SHoward Hinnant}
44763e519524SHoward Hinnant
44777609c9b6SHoward Hinnant#else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
44783e519524SHoward Hinnant
4479e67179bcSMarshall Clow#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
44803e519524SHoward Hinnanttemplate<class _Tp>
44813e519524SHoward Hinnanttemplate<class _Yp>
44823e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
44838e25104bSHoward Hinnanttypename enable_if
44848e25104bSHoward Hinnant<
44858e25104bSHoward Hinnant    !is_array<_Yp>::value &&
44863a6474ecSMarshall Clow    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
44873e519524SHoward Hinnant    shared_ptr<_Tp>&
44888e25104bSHoward Hinnant>::type
4489b3371f6fSHoward Hinnantshared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
44903e519524SHoward Hinnant{
44913e519524SHoward Hinnant    shared_ptr(__r).swap(*this);
44923e519524SHoward Hinnant    return *this;
44933e519524SHoward Hinnant}
4494e67179bcSMarshall Clow#endif
44953e519524SHoward Hinnant
44963e519524SHoward Hinnanttemplate<class _Tp>
44973e519524SHoward Hinnanttemplate <class _Yp, class _Dp>
44983e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
44998e25104bSHoward Hinnanttypename enable_if
45008e25104bSHoward Hinnant<
45018e25104bSHoward Hinnant    !is_array<_Yp>::value &&
45023a6474ecSMarshall Clow    is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
45033a6474ecSMarshall Clow                   typename shared_ptr<_Tp>::element_type*>::value,
45043e519524SHoward Hinnant    shared_ptr<_Tp>&
45058e25104bSHoward Hinnant>::type
45063e519524SHoward Hinnantshared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
45073e519524SHoward Hinnant{
4508ce48a113SHoward Hinnant    shared_ptr(_VSTD::move(__r)).swap(*this);
45093e519524SHoward Hinnant    return *this;
45103e519524SHoward Hinnant}
45113e519524SHoward Hinnant
45127609c9b6SHoward Hinnant#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
45133e519524SHoward Hinnant
45143e519524SHoward Hinnanttemplate<class _Tp>
4515906c872dSEvgeniy Stepanovinline
45163e519524SHoward Hinnantvoid
45173739fe79SHoward Hinnantshared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
45183e519524SHoward Hinnant{
4519ce48a113SHoward Hinnant    _VSTD::swap(__ptr_, __r.__ptr_);
4520ce48a113SHoward Hinnant    _VSTD::swap(__cntrl_, __r.__cntrl_);
45213e519524SHoward Hinnant}
45223e519524SHoward Hinnant
45233e519524SHoward Hinnanttemplate<class _Tp>
4524906c872dSEvgeniy Stepanovinline
45253e519524SHoward Hinnantvoid
45263739fe79SHoward Hinnantshared_ptr<_Tp>::reset() _NOEXCEPT
45273e519524SHoward Hinnant{
45283e519524SHoward Hinnant    shared_ptr().swap(*this);
45293e519524SHoward Hinnant}
45303e519524SHoward Hinnant
45313e519524SHoward Hinnanttemplate<class _Tp>
45323e519524SHoward Hinnanttemplate<class _Yp>
4533906c872dSEvgeniy Stepanovinline
45348e25104bSHoward Hinnanttypename enable_if
45358e25104bSHoward Hinnant<
4536dc83e779SMarshall Clow    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
45373e519524SHoward Hinnant    void
45388e25104bSHoward Hinnant>::type
45393e519524SHoward Hinnantshared_ptr<_Tp>::reset(_Yp* __p)
45403e519524SHoward Hinnant{
45413e519524SHoward Hinnant    shared_ptr(__p).swap(*this);
45423e519524SHoward Hinnant}
45433e519524SHoward Hinnant
45443e519524SHoward Hinnanttemplate<class _Tp>
45453e519524SHoward Hinnanttemplate<class _Yp, class _Dp>
4546906c872dSEvgeniy Stepanovinline
45478e25104bSHoward Hinnanttypename enable_if
45488e25104bSHoward Hinnant<
4549dc83e779SMarshall Clow    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
45503e519524SHoward Hinnant    void
45518e25104bSHoward Hinnant>::type
45523e519524SHoward Hinnantshared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
45533e519524SHoward Hinnant{
45543e519524SHoward Hinnant    shared_ptr(__p, __d).swap(*this);
45553e519524SHoward Hinnant}
45563e519524SHoward Hinnant
45573e519524SHoward Hinnanttemplate<class _Tp>
45583e519524SHoward Hinnanttemplate<class _Yp, class _Dp, class _Alloc>
4559906c872dSEvgeniy Stepanovinline
45608e25104bSHoward Hinnanttypename enable_if
45618e25104bSHoward Hinnant<
4562dc83e779SMarshall Clow    is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
45633e519524SHoward Hinnant    void
45648e25104bSHoward Hinnant>::type
45653e519524SHoward Hinnantshared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
45663e519524SHoward Hinnant{
45673e519524SHoward Hinnant    shared_ptr(__p, __d, __a).swap(*this);
45683e519524SHoward Hinnant}
45693e519524SHoward Hinnant
45703e519524SHoward Hinnant#ifndef _LIBCPP_HAS_NO_VARIADICS
45713e519524SHoward Hinnant
45723e519524SHoward Hinnanttemplate<class _Tp, class ..._Args>
45733e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
45748e25104bSHoward Hinnanttypename enable_if
45758e25104bSHoward Hinnant<
45768e25104bSHoward Hinnant    !is_array<_Tp>::value,
45773e519524SHoward Hinnant    shared_ptr<_Tp>
45788e25104bSHoward Hinnant>::type
45793e519524SHoward Hinnantmake_shared(_Args&& ...__args)
45803e519524SHoward Hinnant{
4581ce48a113SHoward Hinnant    return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
45823e519524SHoward Hinnant}
45833e519524SHoward Hinnant
45843e519524SHoward Hinnanttemplate<class _Tp, class _Alloc, class ..._Args>
45853e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
45868e25104bSHoward Hinnanttypename enable_if
45878e25104bSHoward Hinnant<
45888e25104bSHoward Hinnant    !is_array<_Tp>::value,
45893e519524SHoward Hinnant    shared_ptr<_Tp>
45908e25104bSHoward Hinnant>::type
45913e519524SHoward Hinnantallocate_shared(const _Alloc& __a, _Args&& ...__args)
45923e519524SHoward Hinnant{
4593ce48a113SHoward Hinnant    return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
45943e519524SHoward Hinnant}
45953e519524SHoward Hinnant
45963e519524SHoward Hinnant#else  // _LIBCPP_HAS_NO_VARIADICS
45973e519524SHoward Hinnant
45983e519524SHoward Hinnanttemplate<class _Tp>
45993e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
46003e519524SHoward Hinnantshared_ptr<_Tp>
46013e519524SHoward Hinnantmake_shared()
46023e519524SHoward Hinnant{
46033e519524SHoward Hinnant    return shared_ptr<_Tp>::make_shared();
46043e519524SHoward Hinnant}
46053e519524SHoward Hinnant
46063e519524SHoward Hinnanttemplate<class _Tp, class _A0>
46073e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
46083e519524SHoward Hinnantshared_ptr<_Tp>
46093e519524SHoward Hinnantmake_shared(_A0& __a0)
46103e519524SHoward Hinnant{
46113e519524SHoward Hinnant    return shared_ptr<_Tp>::make_shared(__a0);
46123e519524SHoward Hinnant}
46133e519524SHoward Hinnant
46143e519524SHoward Hinnanttemplate<class _Tp, class _A0, class _A1>
46153e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
46163e519524SHoward Hinnantshared_ptr<_Tp>
46173e519524SHoward Hinnantmake_shared(_A0& __a0, _A1& __a1)
46183e519524SHoward Hinnant{
46193e519524SHoward Hinnant    return shared_ptr<_Tp>::make_shared(__a0, __a1);
46203e519524SHoward Hinnant}
46213e519524SHoward Hinnant
46223e519524SHoward Hinnanttemplate<class _Tp, class _A0, class _A1, class _A2>
46233e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
46243e519524SHoward Hinnantshared_ptr<_Tp>
46253e519524SHoward Hinnantmake_shared(_A0& __a0, _A1& __a1, _A2& __a2)
46263e519524SHoward Hinnant{
46273e519524SHoward Hinnant    return shared_ptr<_Tp>::make_shared(__a0, __a1, __a2);
46283e519524SHoward Hinnant}
46293e519524SHoward Hinnant
46303e519524SHoward Hinnanttemplate<class _Tp, class _Alloc>
46313e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
46323e519524SHoward Hinnantshared_ptr<_Tp>
46333e519524SHoward Hinnantallocate_shared(const _Alloc& __a)
46343e519524SHoward Hinnant{
46353e519524SHoward Hinnant    return shared_ptr<_Tp>::allocate_shared(__a);
46363e519524SHoward Hinnant}
46373e519524SHoward Hinnant
46383e519524SHoward Hinnanttemplate<class _Tp, class _Alloc, class _A0>
46393e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
46403e519524SHoward Hinnantshared_ptr<_Tp>
46413e519524SHoward Hinnantallocate_shared(const _Alloc& __a, _A0& __a0)
46423e519524SHoward Hinnant{
46433e519524SHoward Hinnant    return shared_ptr<_Tp>::allocate_shared(__a, __a0);
46443e519524SHoward Hinnant}
46453e519524SHoward Hinnant
46463e519524SHoward Hinnanttemplate<class _Tp, class _Alloc, class _A0, class _A1>
46473e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
46483e519524SHoward Hinnantshared_ptr<_Tp>
46493e519524SHoward Hinnantallocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1)
46503e519524SHoward Hinnant{
46513e519524SHoward Hinnant    return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1);
46523e519524SHoward Hinnant}
46533e519524SHoward Hinnant
46543e519524SHoward Hinnanttemplate<class _Tp, class _Alloc, class _A0, class _A1, class _A2>
46553e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
46563e519524SHoward Hinnantshared_ptr<_Tp>
46573e519524SHoward Hinnantallocate_shared(const _Alloc& __a, _A0& __a0, _A1& __a1, _A2& __a2)
46583e519524SHoward Hinnant{
46593e519524SHoward Hinnant    return shared_ptr<_Tp>::allocate_shared(__a, __a0, __a1, __a2);
46603e519524SHoward Hinnant}
46613e519524SHoward Hinnant
46623e519524SHoward Hinnant#endif  // _LIBCPP_HAS_NO_VARIADICS
46633e519524SHoward Hinnant
46643e519524SHoward Hinnanttemplate<class _Tp, class _Up>
46653e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
46663e519524SHoward Hinnantbool
46673739fe79SHoward Hinnantoperator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
46683e519524SHoward Hinnant{
46693e519524SHoward Hinnant    return __x.get() == __y.get();
46703e519524SHoward Hinnant}
46713e519524SHoward Hinnant
46723e519524SHoward Hinnanttemplate<class _Tp, class _Up>
46733e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
46743e519524SHoward Hinnantbool
46753739fe79SHoward Hinnantoperator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
46763e519524SHoward Hinnant{
46773e519524SHoward Hinnant    return !(__x == __y);
46783e519524SHoward Hinnant}
46793e519524SHoward Hinnant
46803e519524SHoward Hinnanttemplate<class _Tp, class _Up>
46813e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
46823e519524SHoward Hinnantbool
46833739fe79SHoward Hinnantoperator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
46843e519524SHoward Hinnant{
4685c0db0729SMarshall Clow#if _LIBCPP_STD_VER <= 11
46866fe361c1SEric Fiselier    typedef typename common_type<_Tp*, _Up*>::type _Vp;
46876fe361c1SEric Fiselier    return less<_Vp>()(__x.get(), __y.get());
4688c0db0729SMarshall Clow#else
4689c0db0729SMarshall Clow    return less<>()(__x.get(), __y.get());
4690c0db0729SMarshall Clow#endif
4691c0db0729SMarshall Clow
469267f39647SHoward Hinnant}
469367f39647SHoward Hinnant
469467f39647SHoward Hinnanttemplate<class _Tp, class _Up>
469567f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
469667f39647SHoward Hinnantbool
469767f39647SHoward Hinnantoperator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
469867f39647SHoward Hinnant{
469967f39647SHoward Hinnant    return __y < __x;
470067f39647SHoward Hinnant}
470167f39647SHoward Hinnant
470267f39647SHoward Hinnanttemplate<class _Tp, class _Up>
470367f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
470467f39647SHoward Hinnantbool
470567f39647SHoward Hinnantoperator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
470667f39647SHoward Hinnant{
470767f39647SHoward Hinnant    return !(__y < __x);
470867f39647SHoward Hinnant}
470967f39647SHoward Hinnant
471067f39647SHoward Hinnanttemplate<class _Tp, class _Up>
471167f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
471267f39647SHoward Hinnantbool
471367f39647SHoward Hinnantoperator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
471467f39647SHoward Hinnant{
471567f39647SHoward Hinnant    return !(__x < __y);
471667f39647SHoward Hinnant}
471767f39647SHoward Hinnant
471867f39647SHoward Hinnanttemplate<class _Tp>
471967f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
472067f39647SHoward Hinnantbool
472167f39647SHoward Hinnantoperator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
472267f39647SHoward Hinnant{
472367f39647SHoward Hinnant    return !__x;
472467f39647SHoward Hinnant}
472567f39647SHoward Hinnant
472667f39647SHoward Hinnanttemplate<class _Tp>
472767f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
472867f39647SHoward Hinnantbool
472967f39647SHoward Hinnantoperator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
473067f39647SHoward Hinnant{
473167f39647SHoward Hinnant    return !__x;
473267f39647SHoward Hinnant}
473367f39647SHoward Hinnant
473467f39647SHoward Hinnanttemplate<class _Tp>
473567f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
473667f39647SHoward Hinnantbool
473767f39647SHoward Hinnantoperator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
473867f39647SHoward Hinnant{
473967f39647SHoward Hinnant    return static_cast<bool>(__x);
474067f39647SHoward Hinnant}
474167f39647SHoward Hinnant
474267f39647SHoward Hinnanttemplate<class _Tp>
474367f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
474467f39647SHoward Hinnantbool
474567f39647SHoward Hinnantoperator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
474667f39647SHoward Hinnant{
474767f39647SHoward Hinnant    return static_cast<bool>(__x);
474867f39647SHoward Hinnant}
474967f39647SHoward Hinnant
475067f39647SHoward Hinnanttemplate<class _Tp>
475167f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
475267f39647SHoward Hinnantbool
475367f39647SHoward Hinnantoperator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
475467f39647SHoward Hinnant{
475567f39647SHoward Hinnant    return less<_Tp*>()(__x.get(), nullptr);
475667f39647SHoward Hinnant}
475767f39647SHoward Hinnant
475867f39647SHoward Hinnanttemplate<class _Tp>
475967f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
476067f39647SHoward Hinnantbool
476167f39647SHoward Hinnantoperator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
476267f39647SHoward Hinnant{
476367f39647SHoward Hinnant    return less<_Tp*>()(nullptr, __x.get());
476467f39647SHoward Hinnant}
476567f39647SHoward Hinnant
476667f39647SHoward Hinnanttemplate<class _Tp>
476767f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
476867f39647SHoward Hinnantbool
476967f39647SHoward Hinnantoperator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
477067f39647SHoward Hinnant{
477167f39647SHoward Hinnant    return nullptr < __x;
477267f39647SHoward Hinnant}
477367f39647SHoward Hinnant
477467f39647SHoward Hinnanttemplate<class _Tp>
477567f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
477667f39647SHoward Hinnantbool
477767f39647SHoward Hinnantoperator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
477867f39647SHoward Hinnant{
477967f39647SHoward Hinnant    return __x < nullptr;
478067f39647SHoward Hinnant}
478167f39647SHoward Hinnant
478267f39647SHoward Hinnanttemplate<class _Tp>
478367f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
478467f39647SHoward Hinnantbool
478567f39647SHoward Hinnantoperator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
478667f39647SHoward Hinnant{
478767f39647SHoward Hinnant    return !(nullptr < __x);
478867f39647SHoward Hinnant}
478967f39647SHoward Hinnant
479067f39647SHoward Hinnanttemplate<class _Tp>
479167f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
479267f39647SHoward Hinnantbool
479367f39647SHoward Hinnantoperator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
479467f39647SHoward Hinnant{
479567f39647SHoward Hinnant    return !(__x < nullptr);
479667f39647SHoward Hinnant}
479767f39647SHoward Hinnant
479867f39647SHoward Hinnanttemplate<class _Tp>
479967f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
480067f39647SHoward Hinnantbool
480167f39647SHoward Hinnantoperator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
480267f39647SHoward Hinnant{
480367f39647SHoward Hinnant    return !(__x < nullptr);
480467f39647SHoward Hinnant}
480567f39647SHoward Hinnant
480667f39647SHoward Hinnanttemplate<class _Tp>
480767f39647SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
480867f39647SHoward Hinnantbool
480967f39647SHoward Hinnantoperator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
481067f39647SHoward Hinnant{
481167f39647SHoward Hinnant    return !(nullptr < __x);
48123e519524SHoward Hinnant}
48133e519524SHoward Hinnant
48143e519524SHoward Hinnanttemplate<class _Tp>
48153e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
48163e519524SHoward Hinnantvoid
48173739fe79SHoward Hinnantswap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
48183e519524SHoward Hinnant{
48193e519524SHoward Hinnant    __x.swap(__y);
48203e519524SHoward Hinnant}
48213e519524SHoward Hinnant
48223e519524SHoward Hinnanttemplate<class _Tp, class _Up>
48233e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
48248e25104bSHoward Hinnanttypename enable_if
48258e25104bSHoward Hinnant<
48268e25104bSHoward Hinnant    !is_array<_Tp>::value && !is_array<_Up>::value,
48273e519524SHoward Hinnant    shared_ptr<_Tp>
48288e25104bSHoward Hinnant>::type
48293739fe79SHoward Hinnantstatic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
48303e519524SHoward Hinnant{
48313e519524SHoward Hinnant    return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
48323e519524SHoward Hinnant}
48333e519524SHoward Hinnant
48343e519524SHoward Hinnanttemplate<class _Tp, class _Up>
48353e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
48368e25104bSHoward Hinnanttypename enable_if
48378e25104bSHoward Hinnant<
48388e25104bSHoward Hinnant    !is_array<_Tp>::value && !is_array<_Up>::value,
48393e519524SHoward Hinnant    shared_ptr<_Tp>
48408e25104bSHoward Hinnant>::type
48413739fe79SHoward Hinnantdynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
48423e519524SHoward Hinnant{
48433e519524SHoward Hinnant    _Tp* __p = dynamic_cast<_Tp*>(__r.get());
48443e519524SHoward Hinnant    return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
48453e519524SHoward Hinnant}
48463e519524SHoward Hinnant
48473e519524SHoward Hinnanttemplate<class _Tp, class _Up>
48488e25104bSHoward Hinnanttypename enable_if
48498e25104bSHoward Hinnant<
48508e25104bSHoward Hinnant    is_array<_Tp>::value == is_array<_Up>::value,
48513e519524SHoward Hinnant    shared_ptr<_Tp>
48528e25104bSHoward Hinnant>::type
48533739fe79SHoward Hinnantconst_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
48543e519524SHoward Hinnant{
48558e25104bSHoward Hinnant    typedef typename remove_extent<_Tp>::type _RTp;
48568e25104bSHoward Hinnant    return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
48573e519524SHoward Hinnant}
48583e519524SHoward Hinnant
485954b409fdSHoward Hinnant#ifndef _LIBCPP_NO_RTTI
486054b409fdSHoward Hinnant
48613e519524SHoward Hinnanttemplate<class _Dp, class _Tp>
48623e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
48633e519524SHoward Hinnant_Dp*
48643739fe79SHoward Hinnantget_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
48653e519524SHoward Hinnant{
48663e519524SHoward Hinnant    return __p.template __get_deleter<_Dp>();
48673e519524SHoward Hinnant}
48683e519524SHoward Hinnant
4869b3371f6fSHoward Hinnant#endif  // _LIBCPP_NO_RTTI
487054b409fdSHoward Hinnant
48713e519524SHoward Hinnanttemplate<class _Tp>
4872e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS weak_ptr
48733e519524SHoward Hinnant{
48743e519524SHoward Hinnantpublic:
48753e519524SHoward Hinnant    typedef _Tp element_type;
48763e519524SHoward Hinnantprivate:
48773e519524SHoward Hinnant    element_type*        __ptr_;
48783e519524SHoward Hinnant    __shared_weak_count* __cntrl_;
48793e519524SHoward Hinnant
48803e519524SHoward Hinnantpublic:
4881906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
4882c0937e8aSHoward Hinnant    _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
4883906c872dSEvgeniy Stepanov    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
48843739fe79SHoward Hinnant                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
48853739fe79SHoward Hinnant                        _NOEXCEPT;
4886906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
48873739fe79SHoward Hinnant    weak_ptr(weak_ptr const& __r) _NOEXCEPT;
4888906c872dSEvgeniy Stepanov    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
48893739fe79SHoward Hinnant                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
48903739fe79SHoward Hinnant                         _NOEXCEPT;
48913e519524SHoward Hinnant
48928e25104bSHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4893906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
48948e25104bSHoward Hinnant    weak_ptr(weak_ptr&& __r) _NOEXCEPT;
4895906c872dSEvgeniy Stepanov    template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
48968e25104bSHoward Hinnant                   typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
48978e25104bSHoward Hinnant                         _NOEXCEPT;
48988e25104bSHoward Hinnant#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
48993e519524SHoward Hinnant    ~weak_ptr();
49003e519524SHoward Hinnant
4901906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
49023739fe79SHoward Hinnant    weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
49038e25104bSHoward Hinnant    template<class _Yp>
49048e25104bSHoward Hinnant        typename enable_if
49058e25104bSHoward Hinnant        <
49068e25104bSHoward Hinnant            is_convertible<_Yp*, element_type*>::value,
49078e25104bSHoward Hinnant            weak_ptr&
49088e25104bSHoward Hinnant        >::type
4909906c872dSEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
49108e25104bSHoward Hinnant        operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
49118e25104bSHoward Hinnant
49128e25104bSHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
49138e25104bSHoward Hinnant
4914906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
49158e25104bSHoward Hinnant    weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
49168e25104bSHoward Hinnant    template<class _Yp>
49178e25104bSHoward Hinnant        typename enable_if
49188e25104bSHoward Hinnant        <
49198e25104bSHoward Hinnant            is_convertible<_Yp*, element_type*>::value,
49208e25104bSHoward Hinnant            weak_ptr&
49218e25104bSHoward Hinnant        >::type
4922906c872dSEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
49238e25104bSHoward Hinnant        operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
49248e25104bSHoward Hinnant
49258e25104bSHoward Hinnant#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
49268e25104bSHoward Hinnant
49278e25104bSHoward Hinnant    template<class _Yp>
49288e25104bSHoward Hinnant        typename enable_if
49298e25104bSHoward Hinnant        <
49308e25104bSHoward Hinnant            is_convertible<_Yp*, element_type*>::value,
49318e25104bSHoward Hinnant            weak_ptr&
49328e25104bSHoward Hinnant        >::type
4933906c872dSEvgeniy Stepanov        _LIBCPP_INLINE_VISIBILITY
49348e25104bSHoward Hinnant        operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
49353e519524SHoward Hinnant
4936906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
49373739fe79SHoward Hinnant    void swap(weak_ptr& __r) _NOEXCEPT;
4938906c872dSEvgeniy Stepanov    _LIBCPP_INLINE_VISIBILITY
49393739fe79SHoward Hinnant    void reset() _NOEXCEPT;
49403e519524SHoward Hinnant
4941848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
49423739fe79SHoward Hinnant    long use_count() const _NOEXCEPT
49433739fe79SHoward Hinnant        {return __cntrl_ ? __cntrl_->use_count() : 0;}
4944848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
49453739fe79SHoward Hinnant    bool expired() const _NOEXCEPT
49463739fe79SHoward Hinnant        {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
49473739fe79SHoward Hinnant    shared_ptr<_Tp> lock() const _NOEXCEPT;
4948848a5374SHoward Hinnant    template<class _Up>
4949848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
495036bc7178SMarshall Clow        bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
49513e519524SHoward Hinnant        {return __cntrl_ < __r.__cntrl_;}
4952848a5374SHoward Hinnant    template<class _Up>
4953848a5374SHoward Hinnant        _LIBCPP_INLINE_VISIBILITY
495436bc7178SMarshall Clow        bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
49553e519524SHoward Hinnant        {return __cntrl_ < __r.__cntrl_;}
49563e519524SHoward Hinnant
4957e2f2d1edSEric Fiselier    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
4958e2f2d1edSEric Fiselier    template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
49593e519524SHoward Hinnant};
49603e519524SHoward Hinnant
49613e519524SHoward Hinnanttemplate<class _Tp>
4962906c872dSEvgeniy Stepanovinline
4963c0937e8aSHoward Hinnant_LIBCPP_CONSTEXPR
49643739fe79SHoward Hinnantweak_ptr<_Tp>::weak_ptr() _NOEXCEPT
49653e519524SHoward Hinnant    : __ptr_(0),
49663e519524SHoward Hinnant      __cntrl_(0)
49673e519524SHoward Hinnant{
49683e519524SHoward Hinnant}
49693e519524SHoward Hinnant
49703e519524SHoward Hinnanttemplate<class _Tp>
4971906c872dSEvgeniy Stepanovinline
49723739fe79SHoward Hinnantweak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
49733e519524SHoward Hinnant    : __ptr_(__r.__ptr_),
49743e519524SHoward Hinnant      __cntrl_(__r.__cntrl_)
49753e519524SHoward Hinnant{
49763e519524SHoward Hinnant    if (__cntrl_)
49773e519524SHoward Hinnant        __cntrl_->__add_weak();
49783e519524SHoward Hinnant}
49793e519524SHoward Hinnant
49803e519524SHoward Hinnanttemplate<class _Tp>
49813e519524SHoward Hinnanttemplate<class _Yp>
4982906c872dSEvgeniy Stepanovinline
49833e519524SHoward Hinnantweak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
49845eb6bdfaSHoward Hinnant                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
49853739fe79SHoward Hinnant                         _NOEXCEPT
49863e519524SHoward Hinnant    : __ptr_(__r.__ptr_),
49873e519524SHoward Hinnant      __cntrl_(__r.__cntrl_)
49883e519524SHoward Hinnant{
49893e519524SHoward Hinnant    if (__cntrl_)
49903e519524SHoward Hinnant        __cntrl_->__add_weak();
49913e519524SHoward Hinnant}
49923e519524SHoward Hinnant
49933e519524SHoward Hinnanttemplate<class _Tp>
49943e519524SHoward Hinnanttemplate<class _Yp>
4995906c872dSEvgeniy Stepanovinline
49963e519524SHoward Hinnantweak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
49975eb6bdfaSHoward Hinnant                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
49983739fe79SHoward Hinnant         _NOEXCEPT
49993e519524SHoward Hinnant    : __ptr_(__r.__ptr_),
50003e519524SHoward Hinnant      __cntrl_(__r.__cntrl_)
50013e519524SHoward Hinnant{
50023e519524SHoward Hinnant    if (__cntrl_)
50033e519524SHoward Hinnant        __cntrl_->__add_weak();
50043e519524SHoward Hinnant}
50053e519524SHoward Hinnant
50068e25104bSHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
50078e25104bSHoward Hinnant
50088e25104bSHoward Hinnanttemplate<class _Tp>
5009906c872dSEvgeniy Stepanovinline
50108e25104bSHoward Hinnantweak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
50118e25104bSHoward Hinnant    : __ptr_(__r.__ptr_),
50128e25104bSHoward Hinnant      __cntrl_(__r.__cntrl_)
50138e25104bSHoward Hinnant{
50148e25104bSHoward Hinnant    __r.__ptr_ = 0;
50158e25104bSHoward Hinnant    __r.__cntrl_ = 0;
50168e25104bSHoward Hinnant}
50178e25104bSHoward Hinnant
50188e25104bSHoward Hinnanttemplate<class _Tp>
50198e25104bSHoward Hinnanttemplate<class _Yp>
5020906c872dSEvgeniy Stepanovinline
50218e25104bSHoward Hinnantweak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
50228e25104bSHoward Hinnant                        typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
50238e25104bSHoward Hinnant         _NOEXCEPT
50248e25104bSHoward Hinnant    : __ptr_(__r.__ptr_),
50258e25104bSHoward Hinnant      __cntrl_(__r.__cntrl_)
50268e25104bSHoward Hinnant{
50278e25104bSHoward Hinnant    __r.__ptr_ = 0;
50288e25104bSHoward Hinnant    __r.__cntrl_ = 0;
50298e25104bSHoward Hinnant}
50308e25104bSHoward Hinnant
50318e25104bSHoward Hinnant#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
50328e25104bSHoward Hinnant
50333e519524SHoward Hinnanttemplate<class _Tp>
50343e519524SHoward Hinnantweak_ptr<_Tp>::~weak_ptr()
50353e519524SHoward Hinnant{
50363e519524SHoward Hinnant    if (__cntrl_)
50373e519524SHoward Hinnant        __cntrl_->__release_weak();
50383e519524SHoward Hinnant}
50393e519524SHoward Hinnant
50403e519524SHoward Hinnanttemplate<class _Tp>
5041906c872dSEvgeniy Stepanovinline
50423e519524SHoward Hinnantweak_ptr<_Tp>&
50433739fe79SHoward Hinnantweak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
50443e519524SHoward Hinnant{
50453e519524SHoward Hinnant    weak_ptr(__r).swap(*this);
50463e519524SHoward Hinnant    return *this;
50473e519524SHoward Hinnant}
50483e519524SHoward Hinnant
50493e519524SHoward Hinnanttemplate<class _Tp>
50503e519524SHoward Hinnanttemplate<class _Yp>
5051906c872dSEvgeniy Stepanovinline
50528e25104bSHoward Hinnanttypename enable_if
50538e25104bSHoward Hinnant<
50548e25104bSHoward Hinnant    is_convertible<_Yp*, _Tp*>::value,
50553e519524SHoward Hinnant    weak_ptr<_Tp>&
50568e25104bSHoward Hinnant>::type
50573739fe79SHoward Hinnantweak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
50583e519524SHoward Hinnant{
50593e519524SHoward Hinnant    weak_ptr(__r).swap(*this);
50603e519524SHoward Hinnant    return *this;
50613e519524SHoward Hinnant}
50623e519524SHoward Hinnant
50638e25104bSHoward Hinnant#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
50648e25104bSHoward Hinnant
50658e25104bSHoward Hinnanttemplate<class _Tp>
5066906c872dSEvgeniy Stepanovinline
50678e25104bSHoward Hinnantweak_ptr<_Tp>&
50688e25104bSHoward Hinnantweak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
50698e25104bSHoward Hinnant{
50708e25104bSHoward Hinnant    weak_ptr(_VSTD::move(__r)).swap(*this);
50718e25104bSHoward Hinnant    return *this;
50728e25104bSHoward Hinnant}
50738e25104bSHoward Hinnant
50743e519524SHoward Hinnanttemplate<class _Tp>
50753e519524SHoward Hinnanttemplate<class _Yp>
5076906c872dSEvgeniy Stepanovinline
50778e25104bSHoward Hinnanttypename enable_if
50788e25104bSHoward Hinnant<
50798e25104bSHoward Hinnant    is_convertible<_Yp*, _Tp*>::value,
50803e519524SHoward Hinnant    weak_ptr<_Tp>&
50818e25104bSHoward Hinnant>::type
50828e25104bSHoward Hinnantweak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
50838e25104bSHoward Hinnant{
50848e25104bSHoward Hinnant    weak_ptr(_VSTD::move(__r)).swap(*this);
50858e25104bSHoward Hinnant    return *this;
50868e25104bSHoward Hinnant}
50878e25104bSHoward Hinnant
50888e25104bSHoward Hinnant#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
50898e25104bSHoward Hinnant
50908e25104bSHoward Hinnanttemplate<class _Tp>
50918e25104bSHoward Hinnanttemplate<class _Yp>
5092906c872dSEvgeniy Stepanovinline
50938e25104bSHoward Hinnanttypename enable_if
50948e25104bSHoward Hinnant<
50958e25104bSHoward Hinnant    is_convertible<_Yp*, _Tp*>::value,
50968e25104bSHoward Hinnant    weak_ptr<_Tp>&
50978e25104bSHoward Hinnant>::type
50983739fe79SHoward Hinnantweak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
50993e519524SHoward Hinnant{
51003e519524SHoward Hinnant    weak_ptr(__r).swap(*this);
51013e519524SHoward Hinnant    return *this;
51023e519524SHoward Hinnant}
51033e519524SHoward Hinnant
51043e519524SHoward Hinnanttemplate<class _Tp>
5105906c872dSEvgeniy Stepanovinline
51063e519524SHoward Hinnantvoid
51073739fe79SHoward Hinnantweak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
51083e519524SHoward Hinnant{
5109ce48a113SHoward Hinnant    _VSTD::swap(__ptr_, __r.__ptr_);
5110ce48a113SHoward Hinnant    _VSTD::swap(__cntrl_, __r.__cntrl_);
51113e519524SHoward Hinnant}
51123e519524SHoward Hinnant
51133e519524SHoward Hinnanttemplate<class _Tp>
51143e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
51153e519524SHoward Hinnantvoid
51163739fe79SHoward Hinnantswap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
51173e519524SHoward Hinnant{
51183e519524SHoward Hinnant    __x.swap(__y);
51193e519524SHoward Hinnant}
51203e519524SHoward Hinnant
51213e519524SHoward Hinnanttemplate<class _Tp>
5122906c872dSEvgeniy Stepanovinline
51233e519524SHoward Hinnantvoid
51243739fe79SHoward Hinnantweak_ptr<_Tp>::reset() _NOEXCEPT
51253e519524SHoward Hinnant{
51263e519524SHoward Hinnant    weak_ptr().swap(*this);
51273e519524SHoward Hinnant}
51283e519524SHoward Hinnant
51293e519524SHoward Hinnanttemplate<class _Tp>
51303e519524SHoward Hinnanttemplate<class _Yp>
51313e519524SHoward Hinnantshared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
5132dc83e779SMarshall Clow                            typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
51333e519524SHoward Hinnant    : __ptr_(__r.__ptr_),
51343e519524SHoward Hinnant      __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
51353e519524SHoward Hinnant{
51363e519524SHoward Hinnant    if (__cntrl_ == 0)
5137d437fa5cSMarshall Clow        __throw_bad_weak_ptr();
51383e519524SHoward Hinnant}
51393e519524SHoward Hinnant
51403e519524SHoward Hinnanttemplate<class _Tp>
51413e519524SHoward Hinnantshared_ptr<_Tp>
51423739fe79SHoward Hinnantweak_ptr<_Tp>::lock() const _NOEXCEPT
51433e519524SHoward Hinnant{
51443e519524SHoward Hinnant    shared_ptr<_Tp> __r;
51453e519524SHoward Hinnant    __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
51463e519524SHoward Hinnant    if (__r.__cntrl_)
51473e519524SHoward Hinnant        __r.__ptr_ = __ptr_;
51483e519524SHoward Hinnant    return __r;
51493e519524SHoward Hinnant}
51503e519524SHoward Hinnant
5151183c010cSMarshall Clow#if _LIBCPP_STD_VER > 14
5152183c010cSMarshall Clowtemplate <class _Tp = void> struct owner_less;
5153183c010cSMarshall Clow#else
51543e519524SHoward Hinnanttemplate <class _Tp> struct owner_less;
5155183c010cSMarshall Clow#endif
51563e519524SHoward Hinnant
51573e519524SHoward Hinnanttemplate <class _Tp>
5158e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
51593e519524SHoward Hinnant    : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
51603e519524SHoward Hinnant{
51613e519524SHoward Hinnant    typedef bool result_type;
5162848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
516336bc7178SMarshall Clow    bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
51643e519524SHoward Hinnant        {return __x.owner_before(__y);}
5165848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
516636bc7178SMarshall Clow    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
51673e519524SHoward Hinnant        {return __x.owner_before(__y);}
5168848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
516936bc7178SMarshall Clow    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
51703e519524SHoward Hinnant        {return __x.owner_before(__y);}
51713e519524SHoward Hinnant};
51723e519524SHoward Hinnant
51733e519524SHoward Hinnanttemplate <class _Tp>
5174e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
51753e519524SHoward Hinnant    : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
51763e519524SHoward Hinnant{
51773e519524SHoward Hinnant    typedef bool result_type;
5178848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
517936bc7178SMarshall Clow    bool operator()(  weak_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
51803e519524SHoward Hinnant        {return __x.owner_before(__y);}
5181848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
518236bc7178SMarshall Clow    bool operator()(shared_ptr<_Tp> const& __x,   weak_ptr<_Tp> const& __y) const _NOEXCEPT
51833e519524SHoward Hinnant        {return __x.owner_before(__y);}
5184848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
518536bc7178SMarshall Clow    bool operator()(  weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
51863e519524SHoward Hinnant        {return __x.owner_before(__y);}
51873e519524SHoward Hinnant};
51883e519524SHoward Hinnant
5189183c010cSMarshall Clow#if _LIBCPP_STD_VER > 14
5190183c010cSMarshall Clowtemplate <>
5191e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS owner_less<void>
5192183c010cSMarshall Clow{
5193183c010cSMarshall Clow    template <class _Tp, class _Up>
5194183c010cSMarshall Clow    _LIBCPP_INLINE_VISIBILITY
519536bc7178SMarshall Clow    bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
5196183c010cSMarshall Clow        {return __x.owner_before(__y);}
5197183c010cSMarshall Clow    template <class _Tp, class _Up>
5198183c010cSMarshall Clow    _LIBCPP_INLINE_VISIBILITY
519936bc7178SMarshall Clow    bool operator()( shared_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
5200183c010cSMarshall Clow        {return __x.owner_before(__y);}
5201183c010cSMarshall Clow    template <class _Tp, class _Up>
5202183c010cSMarshall Clow    _LIBCPP_INLINE_VISIBILITY
520336bc7178SMarshall Clow    bool operator()(   weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
5204183c010cSMarshall Clow        {return __x.owner_before(__y);}
5205183c010cSMarshall Clow    template <class _Tp, class _Up>
5206183c010cSMarshall Clow    _LIBCPP_INLINE_VISIBILITY
520736bc7178SMarshall Clow    bool operator()(   weak_ptr<_Tp> const& __x,   weak_ptr<_Up> const& __y) const _NOEXCEPT
5208183c010cSMarshall Clow        {return __x.owner_before(__y);}
5209183c010cSMarshall Clow    typedef void is_transparent;
5210183c010cSMarshall Clow};
5211183c010cSMarshall Clow#endif
5212183c010cSMarshall Clow
52133e519524SHoward Hinnanttemplate<class _Tp>
5214e2f2d1edSEric Fiselierclass _LIBCPP_TEMPLATE_VIS enable_shared_from_this
52153e519524SHoward Hinnant{
52163e519524SHoward Hinnant    mutable weak_ptr<_Tp> __weak_this_;
52173e519524SHoward Hinnantprotected:
5218c0937e8aSHoward Hinnant    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
52193739fe79SHoward Hinnant    enable_shared_from_this() _NOEXCEPT {}
5220848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
52213739fe79SHoward Hinnant    enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
5222848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
52233739fe79SHoward Hinnant    enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
52243739fe79SHoward Hinnant        {return *this;}
5225848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
52263e519524SHoward Hinnant    ~enable_shared_from_this() {}
52273e519524SHoward Hinnantpublic:
5228848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
52293739fe79SHoward Hinnant    shared_ptr<_Tp> shared_from_this()
52303739fe79SHoward Hinnant        {return shared_ptr<_Tp>(__weak_this_);}
5231848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
52323739fe79SHoward Hinnant    shared_ptr<_Tp const> shared_from_this() const
52333739fe79SHoward Hinnant        {return shared_ptr<const _Tp>(__weak_this_);}
52343e519524SHoward Hinnant
523588f5bfdcSEric Fiselier#if _LIBCPP_STD_VER > 14
523688f5bfdcSEric Fiselier    _LIBCPP_INLINE_VISIBILITY
523788f5bfdcSEric Fiselier    weak_ptr<_Tp> weak_from_this() _NOEXCEPT
523888f5bfdcSEric Fiselier       { return __weak_this_; }
523988f5bfdcSEric Fiselier
524088f5bfdcSEric Fiselier    _LIBCPP_INLINE_VISIBILITY
524188f5bfdcSEric Fiselier    weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
524288f5bfdcSEric Fiselier        { return __weak_this_; }
524388f5bfdcSEric Fiselier#endif // _LIBCPP_STD_VER > 14
524488f5bfdcSEric Fiselier
52453e519524SHoward Hinnant    template <class _Up> friend class shared_ptr;
52463e519524SHoward Hinnant};
52473e519524SHoward Hinnant
5248d1803b6cSHoward Hinnanttemplate <class _Tp>
5249e2f2d1edSEric Fiselierstruct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
5250d1803b6cSHoward Hinnant{
5251d1803b6cSHoward Hinnant    typedef shared_ptr<_Tp>      argument_type;
5252d1803b6cSHoward Hinnant    typedef size_t               result_type;
5253f9127593SEric Fiselier
5254848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
52553739fe79SHoward Hinnant    result_type operator()(const argument_type& __ptr) const _NOEXCEPT
5256d1803b6cSHoward Hinnant    {
5257d1803b6cSHoward Hinnant        return hash<_Tp*>()(__ptr.get());
5258d1803b6cSHoward Hinnant    }
5259d1803b6cSHoward Hinnant};
5260d1803b6cSHoward Hinnant
5261c003db1fSHoward Hinnanttemplate<class _CharT, class _Traits, class _Yp>
5262e3163f5aSHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
5263e3163f5aSHoward Hinnantbasic_ostream<_CharT, _Traits>&
5264c003db1fSHoward Hinnantoperator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
5265e3163f5aSHoward Hinnant
5266df93bad1SEric Fiselier
5267df93bad1SEric Fiselier#if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
5268d77851e8SHoward Hinnant
5269f0544c20SHoward Hinnantclass _LIBCPP_TYPE_VIS __sp_mut
5270d77851e8SHoward Hinnant{
527154d333a6SHoward Hinnant    void* __lx;
5272d77851e8SHoward Hinnantpublic:
5273d77851e8SHoward Hinnant    void lock() _NOEXCEPT;
5274d77851e8SHoward Hinnant    void unlock() _NOEXCEPT;
5275d77851e8SHoward Hinnant
5276d77851e8SHoward Hinnantprivate:
5277d77851e8SHoward Hinnant    _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
5278d77851e8SHoward Hinnant    __sp_mut(const __sp_mut&);
5279d77851e8SHoward Hinnant    __sp_mut& operator=(const __sp_mut&);
5280d77851e8SHoward Hinnant
52816e41256fSHoward Hinnant    friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
5282d77851e8SHoward Hinnant};
5283d77851e8SHoward Hinnant
5284e9c66ad9SMehdi Amini_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5285e9c66ad9SMehdi Amini__sp_mut& __get_sp_mut(const void*);
5286d77851e8SHoward Hinnant
5287d77851e8SHoward Hinnanttemplate <class _Tp>
5288d77851e8SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
5289d77851e8SHoward Hinnantbool
5290d77851e8SHoward Hinnantatomic_is_lock_free(const shared_ptr<_Tp>*)
5291d77851e8SHoward Hinnant{
5292d77851e8SHoward Hinnant    return false;
5293d77851e8SHoward Hinnant}
5294d77851e8SHoward Hinnant
5295d77851e8SHoward Hinnanttemplate <class _Tp>
5296e9c66ad9SMehdi Amini_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5297d77851e8SHoward Hinnantshared_ptr<_Tp>
5298d77851e8SHoward Hinnantatomic_load(const shared_ptr<_Tp>* __p)
5299d77851e8SHoward Hinnant{
5300d77851e8SHoward Hinnant    __sp_mut& __m = __get_sp_mut(__p);
5301d77851e8SHoward Hinnant    __m.lock();
5302d77851e8SHoward Hinnant    shared_ptr<_Tp> __q = *__p;
5303d77851e8SHoward Hinnant    __m.unlock();
5304d77851e8SHoward Hinnant    return __q;
5305d77851e8SHoward Hinnant}
5306d77851e8SHoward Hinnant
5307d77851e8SHoward Hinnanttemplate <class _Tp>
5308d77851e8SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
5309e9c66ad9SMehdi Amini_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5310d77851e8SHoward Hinnantshared_ptr<_Tp>
5311d77851e8SHoward Hinnantatomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
5312d77851e8SHoward Hinnant{
5313d77851e8SHoward Hinnant    return atomic_load(__p);
5314d77851e8SHoward Hinnant}
5315d77851e8SHoward Hinnant
5316d77851e8SHoward Hinnanttemplate <class _Tp>
5317e9c66ad9SMehdi Amini_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5318d77851e8SHoward Hinnantvoid
5319d77851e8SHoward Hinnantatomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5320d77851e8SHoward Hinnant{
5321d77851e8SHoward Hinnant    __sp_mut& __m = __get_sp_mut(__p);
5322d77851e8SHoward Hinnant    __m.lock();
5323d77851e8SHoward Hinnant    __p->swap(__r);
5324d77851e8SHoward Hinnant    __m.unlock();
5325d77851e8SHoward Hinnant}
5326d77851e8SHoward Hinnant
5327d77851e8SHoward Hinnanttemplate <class _Tp>
5328d77851e8SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
5329e9c66ad9SMehdi Amini_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5330d77851e8SHoward Hinnantvoid
5331d77851e8SHoward Hinnantatomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5332d77851e8SHoward Hinnant{
5333d77851e8SHoward Hinnant    atomic_store(__p, __r);
5334d77851e8SHoward Hinnant}
5335d77851e8SHoward Hinnant
5336d77851e8SHoward Hinnanttemplate <class _Tp>
5337e9c66ad9SMehdi Amini_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5338d77851e8SHoward Hinnantshared_ptr<_Tp>
5339d77851e8SHoward Hinnantatomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
5340d77851e8SHoward Hinnant{
5341d77851e8SHoward Hinnant    __sp_mut& __m = __get_sp_mut(__p);
5342d77851e8SHoward Hinnant    __m.lock();
5343d77851e8SHoward Hinnant    __p->swap(__r);
5344d77851e8SHoward Hinnant    __m.unlock();
5345d77851e8SHoward Hinnant    return __r;
5346d77851e8SHoward Hinnant}
5347d77851e8SHoward Hinnant
5348d77851e8SHoward Hinnanttemplate <class _Tp>
5349d77851e8SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
5350e9c66ad9SMehdi Amini_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5351d77851e8SHoward Hinnantshared_ptr<_Tp>
5352d77851e8SHoward Hinnantatomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
5353d77851e8SHoward Hinnant{
5354d77851e8SHoward Hinnant    return atomic_exchange(__p, __r);
5355d77851e8SHoward Hinnant}
5356d77851e8SHoward Hinnant
5357d77851e8SHoward Hinnanttemplate <class _Tp>
5358e9c66ad9SMehdi Amini_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5359d77851e8SHoward Hinnantbool
5360d77851e8SHoward Hinnantatomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5361d77851e8SHoward Hinnant{
53623379baebSMarshall Clow    shared_ptr<_Tp> __temp;
5363d77851e8SHoward Hinnant    __sp_mut& __m = __get_sp_mut(__p);
5364d77851e8SHoward Hinnant    __m.lock();
5365d77851e8SHoward Hinnant    if (__p->__owner_equivalent(*__v))
5366d77851e8SHoward Hinnant    {
53673379baebSMarshall Clow        _VSTD::swap(__temp, *__p);
5368d77851e8SHoward Hinnant        *__p = __w;
5369d77851e8SHoward Hinnant        __m.unlock();
5370d77851e8SHoward Hinnant        return true;
5371d77851e8SHoward Hinnant    }
53723379baebSMarshall Clow    _VSTD::swap(__temp, *__v);
5373d77851e8SHoward Hinnant    *__v = *__p;
5374d77851e8SHoward Hinnant    __m.unlock();
5375d77851e8SHoward Hinnant    return false;
5376d77851e8SHoward Hinnant}
5377d77851e8SHoward Hinnant
5378d77851e8SHoward Hinnanttemplate <class _Tp>
5379d77851e8SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
5380e9c66ad9SMehdi Amini_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5381d77851e8SHoward Hinnantbool
5382d77851e8SHoward Hinnantatomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
5383d77851e8SHoward Hinnant{
5384d77851e8SHoward Hinnant    return atomic_compare_exchange_strong(__p, __v, __w);
5385d77851e8SHoward Hinnant}
5386d77851e8SHoward Hinnant
5387d77851e8SHoward Hinnanttemplate <class _Tp>
5388d77851e8SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
5389e9c66ad9SMehdi Amini_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5390d77851e8SHoward Hinnantbool
5391d77851e8SHoward Hinnantatomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5392d77851e8SHoward Hinnant                                        shared_ptr<_Tp> __w, memory_order, memory_order)
5393d77851e8SHoward Hinnant{
5394d77851e8SHoward Hinnant    return atomic_compare_exchange_strong(__p, __v, __w);
5395d77851e8SHoward Hinnant}
5396d77851e8SHoward Hinnant
5397d77851e8SHoward Hinnanttemplate <class _Tp>
5398d77851e8SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
5399e9c66ad9SMehdi Amini_LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
5400d77851e8SHoward Hinnantbool
5401d77851e8SHoward Hinnantatomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
5402d77851e8SHoward Hinnant                                      shared_ptr<_Tp> __w, memory_order, memory_order)
5403d77851e8SHoward Hinnant{
5404d77851e8SHoward Hinnant    return atomic_compare_exchange_weak(__p, __v, __w);
5405d77851e8SHoward Hinnant}
5406d77851e8SHoward Hinnant
5407df93bad1SEric Fiselier#endif  // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
5408d77851e8SHoward Hinnant
54093e519524SHoward Hinnant//enum class
5410528600c4SEric Fiselier#if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
5411528600c4SEric Fiselier# ifndef _LIBCPP_CXX03_LANG
5412528600c4SEric Fiselierenum class pointer_safety : unsigned char {
5413528600c4SEric Fiselier  relaxed,
5414528600c4SEric Fiselier  preferred,
5415528600c4SEric Fiselier  strict
5416528600c4SEric Fiselier};
5417528600c4SEric Fiselier# endif
5418528600c4SEric Fiselier#else
54196e41256fSHoward Hinnantstruct _LIBCPP_TYPE_VIS pointer_safety
54203e519524SHoward Hinnant{
542154d333a6SHoward Hinnant    enum __lx
54223e519524SHoward Hinnant    {
54233e519524SHoward Hinnant        relaxed,
54243e519524SHoward Hinnant        preferred,
54253e519524SHoward Hinnant        strict
54263e519524SHoward Hinnant    };
54273e519524SHoward Hinnant
542854d333a6SHoward Hinnant    __lx __v_;
54293e519524SHoward Hinnant
5430848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
543176a01ea3SEric Fiselier    pointer_safety() : __v_() {}
543276a01ea3SEric Fiselier
543376a01ea3SEric Fiselier    _LIBCPP_INLINE_VISIBILITY
543454d333a6SHoward Hinnant    pointer_safety(__lx __v) : __v_(__v) {}
5435848a5374SHoward Hinnant    _LIBCPP_INLINE_VISIBILITY
54363e519524SHoward Hinnant    operator int() const {return __v_;}
54373e519524SHoward Hinnant};
5438528600c4SEric Fiselier#endif
5439528600c4SEric Fiselier
5440528600c4SEric Fiselier#if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
5441c8e84ff2SLouis Dionne    defined(_LIBCPP_BUILDING_LIBRARY)
5442528600c4SEric Fiselier_LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
5443528600c4SEric Fiselier#else
5444528600c4SEric Fiselier// This function is only offered in C++03 under ABI v1.
5445528600c4SEric Fiselier# if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
5446528600c4SEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
5447528600c4SEric Fiselierpointer_safety get_pointer_safety() _NOEXCEPT {
5448528600c4SEric Fiselier  return pointer_safety::relaxed;
5449528600c4SEric Fiselier}
5450528600c4SEric Fiselier# endif
5451528600c4SEric Fiselier#endif
5452528600c4SEric Fiselier
54533e519524SHoward Hinnant
5454f0544c20SHoward Hinnant_LIBCPP_FUNC_VIS void declare_reachable(void* __p);
5455f0544c20SHoward Hinnant_LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
5456f0544c20SHoward Hinnant_LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
5457f0544c20SHoward Hinnant_LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
54583e519524SHoward Hinnant
54593e519524SHoward Hinnanttemplate <class _Tp>
54603e519524SHoward Hinnantinline _LIBCPP_INLINE_VISIBILITY
54613e519524SHoward Hinnant_Tp*
54623e519524SHoward Hinnantundeclare_reachable(_Tp* __p)
54633e519524SHoward Hinnant{
54643e519524SHoward Hinnant    return static_cast<_Tp*>(__undeclare_reachable(__p));
54653e519524SHoward Hinnant}
54663e519524SHoward Hinnant
5467f0544c20SHoward Hinnant_LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
54683e519524SHoward Hinnant
5469e3fbe143SMarshall Clow// --- Helper for container swap --
5470e3fbe143SMarshall Clowtemplate <typename _Alloc>
547189dd1dd2SEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
5472e3fbe143SMarshall Clowvoid __swap_allocator(_Alloc & __a1, _Alloc & __a2)
5473e3fbe143SMarshall Clow#if _LIBCPP_STD_VER >= 14
5474e3fbe143SMarshall Clow    _NOEXCEPT
5475e3fbe143SMarshall Clow#else
5476e3fbe143SMarshall Clow    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5477e3fbe143SMarshall Clow#endif
5478e3fbe143SMarshall Clow{
5479e3fbe143SMarshall Clow    __swap_allocator(__a1, __a2,
5480e3fbe143SMarshall Clow      integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
5481e3fbe143SMarshall Clow}
5482e3fbe143SMarshall Clow
5483e3fbe143SMarshall Clowtemplate <typename _Alloc>
5484e3fbe143SMarshall Clow_LIBCPP_INLINE_VISIBILITY
5485e3fbe143SMarshall Clowvoid __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
5486e3fbe143SMarshall Clow#if _LIBCPP_STD_VER >= 14
5487e3fbe143SMarshall Clow    _NOEXCEPT
5488e3fbe143SMarshall Clow#else
5489e3fbe143SMarshall Clow    _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
5490e3fbe143SMarshall Clow#endif
5491e3fbe143SMarshall Clow{
5492e3fbe143SMarshall Clow    using _VSTD::swap;
5493e3fbe143SMarshall Clow    swap(__a1, __a2);
5494e3fbe143SMarshall Clow}
5495e3fbe143SMarshall Clow
5496e3fbe143SMarshall Clowtemplate <typename _Alloc>
549789dd1dd2SEric Fiselierinline _LIBCPP_INLINE_VISIBILITY
5498e3fbe143SMarshall Clowvoid __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
5499e3fbe143SMarshall Clow
55001378a5aeSMarshall Clowtemplate <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
55011378a5aeSMarshall Clowstruct __noexcept_move_assign_container : public integral_constant<bool,
55021378a5aeSMarshall Clow    _Traits::propagate_on_container_move_assignment::value
55031378a5aeSMarshall Clow#if _LIBCPP_STD_VER > 14
55041378a5aeSMarshall Clow        || _Traits::is_always_equal::value
55051378a5aeSMarshall Clow#else
55061378a5aeSMarshall Clow        && is_nothrow_move_assignable<_Alloc>::value
55071378a5aeSMarshall Clow#endif
55081378a5aeSMarshall Clow    > {};
5509e3fbe143SMarshall Clow
5510dc3eb83dSMarshall Clow
5511dc3eb83dSMarshall Clow#ifndef _LIBCPP_HAS_NO_VARIADICS
5512dc3eb83dSMarshall Clowtemplate <class _Tp, class _Alloc>
5513dc3eb83dSMarshall Clowstruct __temp_value {
5514dc3eb83dSMarshall Clow    typedef allocator_traits<_Alloc> _Traits;
5515dc3eb83dSMarshall Clow
5516d108bf85SEric Fiselier    typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
5517dc3eb83dSMarshall Clow    _Alloc &__a;
5518dc3eb83dSMarshall Clow
5519dc3eb83dSMarshall Clow    _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
5520dc3eb83dSMarshall Clow    _Tp &   get() { return *__addr(); }
5521dc3eb83dSMarshall Clow
5522dc3eb83dSMarshall Clow    template<class... _Args>
5523f11c00d7SPeter Collingbourne    _LIBCPP_NO_CFI
5524f11c00d7SPeter Collingbourne    __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
5525f11c00d7SPeter Collingbourne      _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
5526f11c00d7SPeter Collingbourne                         _VSTD::forward<_Args>(__args)...);
5527f11c00d7SPeter Collingbourne    }
5528dc3eb83dSMarshall Clow
5529dc3eb83dSMarshall Clow    ~__temp_value() { _Traits::destroy(__a, __addr()); }
5530dc3eb83dSMarshall Clow    };
5531dc3eb83dSMarshall Clow#endif
5532dc3eb83dSMarshall Clow
553376b26852SMarshall Clowtemplate<typename _Alloc, typename = void, typename = void>
55340e22bf8cSMarshall Clowstruct __is_allocator : false_type {};
55350e22bf8cSMarshall Clow
55360e22bf8cSMarshall Clowtemplate<typename _Alloc>
55370e22bf8cSMarshall Clowstruct __is_allocator<_Alloc,
553876b26852SMarshall Clow       typename __void_t<typename _Alloc::value_type>::type,
553976b26852SMarshall Clow       typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
554076b26852SMarshall Clow     >
55410e22bf8cSMarshall Clow   : true_type {};
55420e22bf8cSMarshall Clow
5543d63dd874SEric Fiselier// __builtin_new_allocator -- A non-templated helper for allocating and
5544d63dd874SEric Fiselier// deallocating memory using __builtin_operator_new and
5545d63dd874SEric Fiselier// __builtin_operator_delete. It should be used in preference to
5546d63dd874SEric Fiselier// `std::allocator<T>` to avoid additional instantiations.
5547d63dd874SEric Fiselierstruct __builtin_new_allocator {
5548d63dd874SEric Fiselier  struct __builtin_new_deleter {
5549d63dd874SEric Fiselier    typedef void* pointer_type;
5550d63dd874SEric Fiselier
5551d63dd874SEric Fiselier    _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
5552d63dd874SEric Fiselier        : __size_(__size), __align_(__align) {}
5553d63dd874SEric Fiselier
5554d63dd874SEric Fiselier    void operator()(void* p) const _NOEXCEPT {
5555d63dd874SEric Fiselier        std::__libcpp_deallocate(p, __size_, __align_);
5556d63dd874SEric Fiselier    }
5557d63dd874SEric Fiselier
5558d63dd874SEric Fiselier   private:
5559d63dd874SEric Fiselier    size_t __size_;
5560d63dd874SEric Fiselier    size_t __align_;
5561d63dd874SEric Fiselier  };
5562d63dd874SEric Fiselier
5563d63dd874SEric Fiselier  typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
5564d63dd874SEric Fiselier
5565d63dd874SEric Fiselier  static __holder_t __allocate_bytes(size_t __s, size_t __align) {
5566d63dd874SEric Fiselier      return __holder_t(std::__libcpp_allocate(__s, __align),
5567d63dd874SEric Fiselier                     __builtin_new_deleter(__s, __align));
5568d63dd874SEric Fiselier  }
5569d63dd874SEric Fiselier
5570d63dd874SEric Fiselier  static void __deallocate_bytes(void* __p, size_t __s,
5571d63dd874SEric Fiselier                                 size_t __align) _NOEXCEPT {
5572d63dd874SEric Fiselier      std::__libcpp_deallocate(__p, __s, __align);
5573d63dd874SEric Fiselier  }
5574d63dd874SEric Fiselier
5575d63dd874SEric Fiselier  template <class _Tp>
5576d63dd874SEric Fiselier  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5577d63dd874SEric Fiselier  static __holder_t __allocate_type(size_t __n) {
5578d63dd874SEric Fiselier      return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5579d63dd874SEric Fiselier  }
5580d63dd874SEric Fiselier
5581d63dd874SEric Fiselier  template <class _Tp>
5582d63dd874SEric Fiselier  _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
5583d63dd874SEric Fiselier  static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
5584d63dd874SEric Fiselier      __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
5585d63dd874SEric Fiselier  }
5586d63dd874SEric Fiselier};
5587d63dd874SEric Fiselier
5588d63dd874SEric Fiselier
55893e519524SHoward Hinnant_LIBCPP_END_NAMESPACE_STD
55903e519524SHoward Hinnant
5591a016efb1SEric Fiselier_LIBCPP_POP_MACROS
5592a016efb1SEric Fiselier
55933e519524SHoward Hinnant#endif  // _LIBCPP_MEMORY
5594