1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___MEMORY_UNIQUE_PTR_H
11 #define _LIBCPP___MEMORY_UNIQUE_PTR_H
12 
13 #include <__config>
14 #include <__functional/hash.h>
15 #include <__functional/operations.h>
16 #include <__memory/allocator_traits.h> // __pointer
17 #include <__memory/compressed_pair.h>
18 #include <__utility/forward.h>
19 #include <__utility/move.h>
20 #include <cstddef>
21 #include <type_traits>
22 
23 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
24 #   include <__memory/auto_ptr.h>
25 #endif
26 
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 #  pragma GCC system_header
29 #endif
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 template <class _Tp>
34 struct _LIBCPP_TEMPLATE_VIS default_delete {
35     static_assert(!is_function<_Tp>::value,
36                   "default_delete cannot be instantiated for function types");
37 #ifndef _LIBCPP_CXX03_LANG
38   _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
39 #else
40   _LIBCPP_INLINE_VISIBILITY default_delete() {}
41 #endif
42   template <class _Up>
43   _LIBCPP_INLINE_VISIBILITY
44   default_delete(const default_delete<_Up>&,
45                  typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
46                      0) _NOEXCEPT {}
47 
48   _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
49     static_assert(sizeof(_Tp) > 0,
50                   "default_delete can not delete incomplete type");
51     static_assert(!is_void<_Tp>::value,
52                   "default_delete can not delete incomplete type");
53     delete __ptr;
54   }
55 };
56 
57 template <class _Tp>
58 struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
59 private:
60   template <class _Up>
61   struct _EnableIfConvertible
62       : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
63 
64 public:
65 #ifndef _LIBCPP_CXX03_LANG
66   _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
67 #else
68   _LIBCPP_INLINE_VISIBILITY default_delete() {}
69 #endif
70 
71   template <class _Up>
72   _LIBCPP_INLINE_VISIBILITY
73   default_delete(const default_delete<_Up[]>&,
74                  typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
75 
76   template <class _Up>
77   _LIBCPP_INLINE_VISIBILITY
78   typename _EnableIfConvertible<_Up>::type
79   operator()(_Up* __ptr) const _NOEXCEPT {
80     static_assert(sizeof(_Tp) > 0,
81                   "default_delete can not delete incomplete type");
82     static_assert(!is_void<_Tp>::value,
83                   "default_delete can not delete void type");
84     delete[] __ptr;
85   }
86 };
87 
88 template <class _Deleter>
89 struct __unique_ptr_deleter_sfinae {
90   static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
91   typedef const _Deleter& __lval_ref_type;
92   typedef _Deleter&& __good_rval_ref_type;
93   typedef true_type __enable_rval_overload;
94 };
95 
96 template <class _Deleter>
97 struct __unique_ptr_deleter_sfinae<_Deleter const&> {
98   typedef const _Deleter& __lval_ref_type;
99   typedef const _Deleter&& __bad_rval_ref_type;
100   typedef false_type __enable_rval_overload;
101 };
102 
103 template <class _Deleter>
104 struct __unique_ptr_deleter_sfinae<_Deleter&> {
105   typedef _Deleter& __lval_ref_type;
106   typedef _Deleter&& __bad_rval_ref_type;
107   typedef false_type __enable_rval_overload;
108 };
109 
110 #if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
111 #  define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
112 #else
113 #  define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
114 #endif
115 
116 template <class _Tp, class _Dp = default_delete<_Tp> >
117 class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
118 public:
119   typedef _Tp element_type;
120   typedef _Dp deleter_type;
121   typedef _LIBCPP_NODEBUG typename __pointer<_Tp, deleter_type>::type pointer;
122 
123   static_assert(!is_rvalue_reference<deleter_type>::value,
124                 "the specified deleter type cannot be an rvalue reference");
125 
126 private:
127   __compressed_pair<pointer, deleter_type> __ptr_;
128 
129   struct __nat { int __for_bool_; };
130 
131   typedef _LIBCPP_NODEBUG __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
132 
133   template <bool _Dummy>
134   using _LValRefType _LIBCPP_NODEBUG =
135       typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
136 
137   template <bool _Dummy>
138   using _GoodRValRefType _LIBCPP_NODEBUG =
139       typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
140 
141   template <bool _Dummy>
142   using _BadRValRefType _LIBCPP_NODEBUG =
143       typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
144 
145   template <bool _Dummy, class _Deleter = typename __dependent_type<
146                              __identity<deleter_type>, _Dummy>::type>
147   using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG =
148       typename enable_if<is_default_constructible<_Deleter>::value &&
149                          !is_pointer<_Deleter>::value>::type;
150 
151   template <class _ArgType>
152   using _EnableIfDeleterConstructible _LIBCPP_NODEBUG =
153       typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
154 
155   template <class _UPtr, class _Up>
156   using _EnableIfMoveConvertible _LIBCPP_NODEBUG = typename enable_if<
157       is_convertible<typename _UPtr::pointer, pointer>::value &&
158       !is_array<_Up>::value
159   >::type;
160 
161   template <class _UDel>
162   using _EnableIfDeleterConvertible _LIBCPP_NODEBUG = typename enable_if<
163       (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
164       (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
165     >::type;
166 
167   template <class _UDel>
168   using _EnableIfDeleterAssignable = typename enable_if<
169       is_assignable<_Dp&, _UDel&&>::value
170     >::type;
171 
172 public:
173   template <bool _Dummy = true,
174             class = _EnableIfDeleterDefaultConstructible<_Dummy> >
175   _LIBCPP_INLINE_VISIBILITY
176   _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(__value_init_tag(), __value_init_tag()) {}
177 
178   template <bool _Dummy = true,
179             class = _EnableIfDeleterDefaultConstructible<_Dummy> >
180   _LIBCPP_INLINE_VISIBILITY
181   _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(__value_init_tag(), __value_init_tag()) {}
182 
183   template <bool _Dummy = true,
184             class = _EnableIfDeleterDefaultConstructible<_Dummy> >
185   _LIBCPP_INLINE_VISIBILITY
186   explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __value_init_tag()) {}
187 
188   template <bool _Dummy = true,
189             class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
190   _LIBCPP_INLINE_VISIBILITY
191   unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
192       : __ptr_(__p, __d) {}
193 
194   template <bool _Dummy = true,
195             class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
196   _LIBCPP_INLINE_VISIBILITY
197   unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
198       : __ptr_(__p, _VSTD::move(__d)) {
199     static_assert(!is_reference<deleter_type>::value,
200                   "rvalue deleter bound to reference");
201   }
202 
203   template <bool _Dummy = true,
204             class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
205   _LIBCPP_INLINE_VISIBILITY
206   unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
207 
208   _LIBCPP_INLINE_VISIBILITY
209   unique_ptr(unique_ptr&& __u) _NOEXCEPT
210       : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
211   }
212 
213   template <class _Up, class _Ep,
214       class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
215       class = _EnableIfDeleterConvertible<_Ep>
216   >
217   _LIBCPP_INLINE_VISIBILITY
218   unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
219       : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
220 
221 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
222   template <class _Up>
223   _LIBCPP_INLINE_VISIBILITY
224   unique_ptr(auto_ptr<_Up>&& __p,
225              typename enable_if<is_convertible<_Up*, _Tp*>::value &&
226                                     is_same<_Dp, default_delete<_Tp> >::value,
227                                 __nat>::type = __nat()) _NOEXCEPT
228       : __ptr_(__p.release(), __value_init_tag()) {}
229 #endif
230 
231   _LIBCPP_INLINE_VISIBILITY
232   unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
233     reset(__u.release());
234     __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
235     return *this;
236   }
237 
238   template <class _Up, class _Ep,
239       class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
240       class = _EnableIfDeleterAssignable<_Ep>
241   >
242   _LIBCPP_INLINE_VISIBILITY
243   unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
244     reset(__u.release());
245     __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
246     return *this;
247   }
248 
249 #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
250   template <class _Up>
251   _LIBCPP_INLINE_VISIBILITY
252       typename enable_if<is_convertible<_Up*, _Tp*>::value &&
253                              is_same<_Dp, default_delete<_Tp> >::value,
254                          unique_ptr&>::type
255       operator=(auto_ptr<_Up> __p) {
256     reset(__p.release());
257     return *this;
258   }
259 #endif
260 
261 #ifdef _LIBCPP_CXX03_LANG
262   unique_ptr(unique_ptr const&) = delete;
263   unique_ptr& operator=(unique_ptr const&) = delete;
264 #endif
265 
266 
267   _LIBCPP_INLINE_VISIBILITY
268   ~unique_ptr() { reset(); }
269 
270   _LIBCPP_INLINE_VISIBILITY
271   unique_ptr& operator=(nullptr_t) _NOEXCEPT {
272     reset();
273     return *this;
274   }
275 
276   _LIBCPP_INLINE_VISIBILITY
277   typename add_lvalue_reference<_Tp>::type
278   operator*() const {
279     return *__ptr_.first();
280   }
281   _LIBCPP_INLINE_VISIBILITY
282   pointer operator->() const _NOEXCEPT {
283     return __ptr_.first();
284   }
285   _LIBCPP_INLINE_VISIBILITY
286   pointer get() const _NOEXCEPT {
287     return __ptr_.first();
288   }
289   _LIBCPP_INLINE_VISIBILITY
290   deleter_type& get_deleter() _NOEXCEPT {
291     return __ptr_.second();
292   }
293   _LIBCPP_INLINE_VISIBILITY
294   const deleter_type& get_deleter() const _NOEXCEPT {
295     return __ptr_.second();
296   }
297   _LIBCPP_INLINE_VISIBILITY
298   explicit operator bool() const _NOEXCEPT {
299     return __ptr_.first() != nullptr;
300   }
301 
302   _LIBCPP_INLINE_VISIBILITY
303   pointer release() _NOEXCEPT {
304     pointer __t = __ptr_.first();
305     __ptr_.first() = pointer();
306     return __t;
307   }
308 
309   _LIBCPP_INLINE_VISIBILITY
310   void reset(pointer __p = pointer()) _NOEXCEPT {
311     pointer __tmp = __ptr_.first();
312     __ptr_.first() = __p;
313     if (__tmp)
314       __ptr_.second()(__tmp);
315   }
316 
317   _LIBCPP_INLINE_VISIBILITY
318   void swap(unique_ptr& __u) _NOEXCEPT {
319     __ptr_.swap(__u.__ptr_);
320   }
321 };
322 
323 
324 template <class _Tp, class _Dp>
325 class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
326 public:
327   typedef _Tp element_type;
328   typedef _Dp deleter_type;
329   typedef typename __pointer<_Tp, deleter_type>::type pointer;
330 
331 private:
332   __compressed_pair<pointer, deleter_type> __ptr_;
333 
334   template <class _From>
335   struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
336 
337   template <class _FromElem>
338   struct _CheckArrayPointerConversion<_FromElem*>
339       : integral_constant<bool,
340           is_same<_FromElem*, pointer>::value ||
341             (is_same<pointer, element_type*>::value &&
342              is_convertible<_FromElem(*)[], element_type(*)[]>::value)
343       >
344   {};
345 
346   typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
347 
348   template <bool _Dummy>
349   using _LValRefType _LIBCPP_NODEBUG =
350       typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
351 
352   template <bool _Dummy>
353   using _GoodRValRefType _LIBCPP_NODEBUG =
354       typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
355 
356   template <bool _Dummy>
357   using _BadRValRefType _LIBCPP_NODEBUG =
358       typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
359 
360   template <bool _Dummy, class _Deleter = typename __dependent_type<
361                              __identity<deleter_type>, _Dummy>::type>
362   using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG =
363       typename enable_if<is_default_constructible<_Deleter>::value &&
364                          !is_pointer<_Deleter>::value>::type;
365 
366   template <class _ArgType>
367   using _EnableIfDeleterConstructible _LIBCPP_NODEBUG =
368       typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
369 
370   template <class _Pp>
371   using _EnableIfPointerConvertible _LIBCPP_NODEBUG = typename enable_if<
372       _CheckArrayPointerConversion<_Pp>::value
373   >::type;
374 
375   template <class _UPtr, class _Up,
376         class _ElemT = typename _UPtr::element_type>
377   using _EnableIfMoveConvertible _LIBCPP_NODEBUG = typename enable_if<
378       is_array<_Up>::value &&
379       is_same<pointer, element_type*>::value &&
380       is_same<typename _UPtr::pointer, _ElemT*>::value &&
381       is_convertible<_ElemT(*)[], element_type(*)[]>::value
382     >::type;
383 
384   template <class _UDel>
385   using _EnableIfDeleterConvertible _LIBCPP_NODEBUG = typename enable_if<
386       (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
387       (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
388     >::type;
389 
390   template <class _UDel>
391   using _EnableIfDeleterAssignable _LIBCPP_NODEBUG = typename enable_if<
392       is_assignable<_Dp&, _UDel&&>::value
393     >::type;
394 
395 public:
396   template <bool _Dummy = true,
397             class = _EnableIfDeleterDefaultConstructible<_Dummy> >
398   _LIBCPP_INLINE_VISIBILITY
399   _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(__value_init_tag(), __value_init_tag()) {}
400 
401   template <bool _Dummy = true,
402             class = _EnableIfDeleterDefaultConstructible<_Dummy> >
403   _LIBCPP_INLINE_VISIBILITY
404   _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(__value_init_tag(), __value_init_tag()) {}
405 
406   template <class _Pp, bool _Dummy = true,
407             class = _EnableIfDeleterDefaultConstructible<_Dummy>,
408             class = _EnableIfPointerConvertible<_Pp> >
409   _LIBCPP_INLINE_VISIBILITY
410   explicit unique_ptr(_Pp __p) _NOEXCEPT
411       : __ptr_(__p, __value_init_tag()) {}
412 
413   template <class _Pp, bool _Dummy = true,
414             class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
415             class = _EnableIfPointerConvertible<_Pp> >
416   _LIBCPP_INLINE_VISIBILITY
417   unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
418       : __ptr_(__p, __d) {}
419 
420   template <bool _Dummy = true,
421             class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
422   _LIBCPP_INLINE_VISIBILITY
423   unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
424       : __ptr_(nullptr, __d) {}
425 
426   template <class _Pp, bool _Dummy = true,
427             class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
428             class = _EnableIfPointerConvertible<_Pp> >
429   _LIBCPP_INLINE_VISIBILITY
430   unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
431       : __ptr_(__p, _VSTD::move(__d)) {
432     static_assert(!is_reference<deleter_type>::value,
433                   "rvalue deleter bound to reference");
434   }
435 
436   template <bool _Dummy = true,
437             class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
438   _LIBCPP_INLINE_VISIBILITY
439   unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
440       : __ptr_(nullptr, _VSTD::move(__d)) {
441     static_assert(!is_reference<deleter_type>::value,
442                   "rvalue deleter bound to reference");
443   }
444 
445   template <class _Pp, bool _Dummy = true,
446             class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
447             class = _EnableIfPointerConvertible<_Pp> >
448   _LIBCPP_INLINE_VISIBILITY
449   unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
450 
451   _LIBCPP_INLINE_VISIBILITY
452   unique_ptr(unique_ptr&& __u) _NOEXCEPT
453       : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
454   }
455 
456   _LIBCPP_INLINE_VISIBILITY
457   unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
458     reset(__u.release());
459     __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
460     return *this;
461   }
462 
463   template <class _Up, class _Ep,
464       class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
465       class = _EnableIfDeleterConvertible<_Ep>
466   >
467   _LIBCPP_INLINE_VISIBILITY
468   unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
469       : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
470   }
471 
472   template <class _Up, class _Ep,
473       class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
474       class = _EnableIfDeleterAssignable<_Ep>
475   >
476   _LIBCPP_INLINE_VISIBILITY
477   unique_ptr&
478   operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
479     reset(__u.release());
480     __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
481     return *this;
482   }
483 
484 #ifdef _LIBCPP_CXX03_LANG
485   unique_ptr(unique_ptr const&) = delete;
486   unique_ptr& operator=(unique_ptr const&) = delete;
487 #endif
488 
489 public:
490   _LIBCPP_INLINE_VISIBILITY
491   ~unique_ptr() { reset(); }
492 
493   _LIBCPP_INLINE_VISIBILITY
494   unique_ptr& operator=(nullptr_t) _NOEXCEPT {
495     reset();
496     return *this;
497   }
498 
499   _LIBCPP_INLINE_VISIBILITY
500   typename add_lvalue_reference<_Tp>::type
501   operator[](size_t __i) const {
502     return __ptr_.first()[__i];
503   }
504   _LIBCPP_INLINE_VISIBILITY
505   pointer get() const _NOEXCEPT {
506     return __ptr_.first();
507   }
508 
509   _LIBCPP_INLINE_VISIBILITY
510   deleter_type& get_deleter() _NOEXCEPT {
511     return __ptr_.second();
512   }
513 
514   _LIBCPP_INLINE_VISIBILITY
515   const deleter_type& get_deleter() const _NOEXCEPT {
516     return __ptr_.second();
517   }
518   _LIBCPP_INLINE_VISIBILITY
519   explicit operator bool() const _NOEXCEPT {
520     return __ptr_.first() != nullptr;
521   }
522 
523   _LIBCPP_INLINE_VISIBILITY
524   pointer release() _NOEXCEPT {
525     pointer __t = __ptr_.first();
526     __ptr_.first() = pointer();
527     return __t;
528   }
529 
530   template <class _Pp>
531   _LIBCPP_INLINE_VISIBILITY
532   typename enable_if<
533       _CheckArrayPointerConversion<_Pp>::value
534   >::type
535   reset(_Pp __p) _NOEXCEPT {
536     pointer __tmp = __ptr_.first();
537     __ptr_.first() = __p;
538     if (__tmp)
539       __ptr_.second()(__tmp);
540   }
541 
542   _LIBCPP_INLINE_VISIBILITY
543   void reset(nullptr_t = nullptr) _NOEXCEPT {
544     pointer __tmp = __ptr_.first();
545     __ptr_.first() = nullptr;
546     if (__tmp)
547       __ptr_.second()(__tmp);
548   }
549 
550   _LIBCPP_INLINE_VISIBILITY
551   void swap(unique_ptr& __u) _NOEXCEPT {
552     __ptr_.swap(__u.__ptr_);
553   }
554 
555 };
556 
557 template <class _Tp, class _Dp>
558 inline _LIBCPP_INLINE_VISIBILITY
559 typename enable_if<
560     __is_swappable<_Dp>::value,
561     void
562 >::type
563 swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
564 
565 template <class _T1, class _D1, class _T2, class _D2>
566 inline _LIBCPP_INLINE_VISIBILITY
567 bool
568 operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
569 
570 template <class _T1, class _D1, class _T2, class _D2>
571 inline _LIBCPP_INLINE_VISIBILITY
572 bool
573 operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
574 
575 template <class _T1, class _D1, class _T2, class _D2>
576 inline _LIBCPP_INLINE_VISIBILITY
577 bool
578 operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
579 {
580     typedef typename unique_ptr<_T1, _D1>::pointer _P1;
581     typedef typename unique_ptr<_T2, _D2>::pointer _P2;
582     typedef typename common_type<_P1, _P2>::type _Vp;
583     return less<_Vp>()(__x.get(), __y.get());
584 }
585 
586 template <class _T1, class _D1, class _T2, class _D2>
587 inline _LIBCPP_INLINE_VISIBILITY
588 bool
589 operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
590 
591 template <class _T1, class _D1, class _T2, class _D2>
592 inline _LIBCPP_INLINE_VISIBILITY
593 bool
594 operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
595 
596 template <class _T1, class _D1, class _T2, class _D2>
597 inline _LIBCPP_INLINE_VISIBILITY
598 bool
599 operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
600 
601 template <class _T1, class _D1>
602 inline _LIBCPP_INLINE_VISIBILITY
603 bool
604 operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
605 {
606     return !__x;
607 }
608 
609 template <class _T1, class _D1>
610 inline _LIBCPP_INLINE_VISIBILITY
611 bool
612 operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
613 {
614     return !__x;
615 }
616 
617 template <class _T1, class _D1>
618 inline _LIBCPP_INLINE_VISIBILITY
619 bool
620 operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
621 {
622     return static_cast<bool>(__x);
623 }
624 
625 template <class _T1, class _D1>
626 inline _LIBCPP_INLINE_VISIBILITY
627 bool
628 operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
629 {
630     return static_cast<bool>(__x);
631 }
632 
633 template <class _T1, class _D1>
634 inline _LIBCPP_INLINE_VISIBILITY
635 bool
636 operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
637 {
638     typedef typename unique_ptr<_T1, _D1>::pointer _P1;
639     return less<_P1>()(__x.get(), nullptr);
640 }
641 
642 template <class _T1, class _D1>
643 inline _LIBCPP_INLINE_VISIBILITY
644 bool
645 operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
646 {
647     typedef typename unique_ptr<_T1, _D1>::pointer _P1;
648     return less<_P1>()(nullptr, __x.get());
649 }
650 
651 template <class _T1, class _D1>
652 inline _LIBCPP_INLINE_VISIBILITY
653 bool
654 operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
655 {
656     return nullptr < __x;
657 }
658 
659 template <class _T1, class _D1>
660 inline _LIBCPP_INLINE_VISIBILITY
661 bool
662 operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
663 {
664     return __x < nullptr;
665 }
666 
667 template <class _T1, class _D1>
668 inline _LIBCPP_INLINE_VISIBILITY
669 bool
670 operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
671 {
672     return !(nullptr < __x);
673 }
674 
675 template <class _T1, class _D1>
676 inline _LIBCPP_INLINE_VISIBILITY
677 bool
678 operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
679 {
680     return !(__x < nullptr);
681 }
682 
683 template <class _T1, class _D1>
684 inline _LIBCPP_INLINE_VISIBILITY
685 bool
686 operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
687 {
688     return !(__x < nullptr);
689 }
690 
691 template <class _T1, class _D1>
692 inline _LIBCPP_INLINE_VISIBILITY
693 bool
694 operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
695 {
696     return !(nullptr < __x);
697 }
698 
699 #if _LIBCPP_STD_VER > 11
700 
701 template<class _Tp>
702 struct __unique_if
703 {
704     typedef unique_ptr<_Tp> __unique_single;
705 };
706 
707 template<class _Tp>
708 struct __unique_if<_Tp[]>
709 {
710     typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
711 };
712 
713 template<class _Tp, size_t _Np>
714 struct __unique_if<_Tp[_Np]>
715 {
716     typedef void __unique_array_known_bound;
717 };
718 
719 template<class _Tp, class... _Args>
720 inline _LIBCPP_INLINE_VISIBILITY
721 typename __unique_if<_Tp>::__unique_single
722 make_unique(_Args&&... __args)
723 {
724     return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
725 }
726 
727 template<class _Tp>
728 inline _LIBCPP_INLINE_VISIBILITY
729 typename __unique_if<_Tp>::__unique_array_unknown_bound
730 make_unique(size_t __n)
731 {
732     typedef typename remove_extent<_Tp>::type _Up;
733     return unique_ptr<_Tp>(new _Up[__n]());
734 }
735 
736 template<class _Tp, class... _Args>
737     typename __unique_if<_Tp>::__unique_array_known_bound
738     make_unique(_Args&&...) = delete;
739 
740 #endif // _LIBCPP_STD_VER > 11
741 
742 template <class _Tp> struct _LIBCPP_TEMPLATE_VIS hash;
743 
744 template <class _Tp, class _Dp>
745 #ifdef _LIBCPP_CXX03_LANG
746 struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
747 #else
748 struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
749     unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
750 #endif
751 {
752 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
753     _LIBCPP_DEPRECATED_IN_CXX17 typedef unique_ptr<_Tp, _Dp> argument_type;
754     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t               result_type;
755 #endif
756 
757     _LIBCPP_INLINE_VISIBILITY
758     size_t operator()(const unique_ptr<_Tp, _Dp>& __ptr) const
759     {
760         typedef typename unique_ptr<_Tp, _Dp>::pointer pointer;
761         return hash<pointer>()(__ptr.get());
762     }
763 };
764 
765 _LIBCPP_END_NAMESPACE_STD
766 
767 #endif // _LIBCPP___MEMORY_UNIQUE_PTR_H
768