Path: blob/main/contrib/llvm-project/libcxx/include/__memory/unique_ptr.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___MEMORY_UNIQUE_PTR_H10#define _LIBCPP___MEMORY_UNIQUE_PTR_H1112#include <__compare/compare_three_way.h>13#include <__compare/compare_three_way_result.h>14#include <__compare/three_way_comparable.h>15#include <__config>16#include <__functional/hash.h>17#include <__functional/operations.h>18#include <__memory/allocator_traits.h> // __pointer19#include <__memory/auto_ptr.h>20#include <__memory/compressed_pair.h>21#include <__type_traits/add_lvalue_reference.h>22#include <__type_traits/common_type.h>23#include <__type_traits/conditional.h>24#include <__type_traits/dependent_type.h>25#include <__type_traits/integral_constant.h>26#include <__type_traits/is_array.h>27#include <__type_traits/is_assignable.h>28#include <__type_traits/is_constructible.h>29#include <__type_traits/is_convertible.h>30#include <__type_traits/is_function.h>31#include <__type_traits/is_pointer.h>32#include <__type_traits/is_reference.h>33#include <__type_traits/is_same.h>34#include <__type_traits/is_swappable.h>35#include <__type_traits/is_trivially_relocatable.h>36#include <__type_traits/is_void.h>37#include <__type_traits/remove_extent.h>38#include <__type_traits/remove_pointer.h>39#include <__type_traits/type_identity.h>40#include <__utility/declval.h>41#include <__utility/forward.h>42#include <__utility/move.h>43#include <cstddef>4445#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)46# pragma GCC system_header47#endif4849_LIBCPP_PUSH_MACROS50#include <__undef_macros>5152_LIBCPP_BEGIN_NAMESPACE_STD5354#ifndef _LIBCPP_CXX03_LANG5556template <class _Ptr>57struct __is_noexcept_deref_or_void {58static constexpr bool value = noexcept(*std::declval<_Ptr>());59};6061template <>62struct __is_noexcept_deref_or_void<void*> : true_type {};63#endif6465template <class _Tp>66struct _LIBCPP_TEMPLATE_VIS default_delete {67static_assert(!is_function<_Tp>::value, "default_delete cannot be instantiated for function types");68#ifndef _LIBCPP_CXX03_LANG69_LIBCPP_HIDE_FROM_ABI constexpr default_delete() _NOEXCEPT = default;70#else71_LIBCPP_HIDE_FROM_ABI default_delete() {}72#endif73template <class _Up, __enable_if_t<is_convertible<_Up*, _Tp*>::value, int> = 0>74_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 default_delete(const default_delete<_Up>&) _NOEXCEPT {}7576_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator()(_Tp* __ptr) const _NOEXCEPT {77static_assert(sizeof(_Tp) >= 0, "cannot delete an incomplete type");78static_assert(!is_void<_Tp>::value, "cannot delete an incomplete type");79delete __ptr;80}81};8283template <class _Tp>84struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {85private:86template <class _Up>87struct _EnableIfConvertible : enable_if<is_convertible<_Up (*)[], _Tp (*)[]>::value> {};8889public:90#ifndef _LIBCPP_CXX03_LANG91_LIBCPP_HIDE_FROM_ABI constexpr default_delete() _NOEXCEPT = default;92#else93_LIBCPP_HIDE_FROM_ABI default_delete() {}94#endif9596template <class _Up>97_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX2398default_delete(const default_delete<_Up[]>&, typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}99100template <class _Up>101_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename _EnableIfConvertible<_Up>::type102operator()(_Up* __ptr) const _NOEXCEPT {103static_assert(sizeof(_Up) >= 0, "cannot delete an incomplete type");104delete[] __ptr;105}106};107108template <class _Deleter>109struct __unique_ptr_deleter_sfinae {110static_assert(!is_reference<_Deleter>::value, "incorrect specialization");111typedef const _Deleter& __lval_ref_type;112typedef _Deleter&& __good_rval_ref_type;113typedef true_type __enable_rval_overload;114};115116template <class _Deleter>117struct __unique_ptr_deleter_sfinae<_Deleter const&> {118typedef const _Deleter& __lval_ref_type;119typedef const _Deleter&& __bad_rval_ref_type;120typedef false_type __enable_rval_overload;121};122123template <class _Deleter>124struct __unique_ptr_deleter_sfinae<_Deleter&> {125typedef _Deleter& __lval_ref_type;126typedef _Deleter&& __bad_rval_ref_type;127typedef false_type __enable_rval_overload;128};129130#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)131# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((__trivial_abi__))132#else133# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI134#endif135136template <class _Tp, class _Dp = default_delete<_Tp> >137class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {138public:139typedef _Tp element_type;140typedef _Dp deleter_type;141typedef _LIBCPP_NODEBUG typename __pointer<_Tp, deleter_type>::type pointer;142143static_assert(!is_rvalue_reference<deleter_type>::value, "the specified deleter type cannot be an rvalue reference");144145// A unique_ptr contains the following members which may be trivially relocatable:146// - pointer : this may be trivially relocatable, so it's checked147// - deleter_type: this may be trivially relocatable, so it's checked148//149// This unique_ptr implementation only contains a pointer to the unique object and a deleter, so there are no150// references to itself. This means that the entire structure is trivially relocatable if its members are.151using __trivially_relocatable = __conditional_t<152__libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<deleter_type>::value,153unique_ptr,154void>;155156private:157__compressed_pair<pointer, deleter_type> __ptr_;158159typedef _LIBCPP_NODEBUG __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;160161template <bool _Dummy>162using _LValRefType _LIBCPP_NODEBUG = typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;163164template <bool _Dummy>165using _GoodRValRefType _LIBCPP_NODEBUG = typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;166167template <bool _Dummy>168using _BadRValRefType _LIBCPP_NODEBUG = typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;169170template <bool _Dummy, class _Deleter = typename __dependent_type< __type_identity<deleter_type>, _Dummy>::type>171using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG =172__enable_if_t<is_default_constructible<_Deleter>::value && !is_pointer<_Deleter>::value>;173174template <class _ArgType>175using _EnableIfDeleterConstructible _LIBCPP_NODEBUG = __enable_if_t<is_constructible<deleter_type, _ArgType>::value>;176177template <class _UPtr, class _Up>178using _EnableIfMoveConvertible _LIBCPP_NODEBUG =179__enable_if_t< is_convertible<typename _UPtr::pointer, pointer>::value && !is_array<_Up>::value >;180181template <class _UDel>182using _EnableIfDeleterConvertible _LIBCPP_NODEBUG =183__enable_if_t< (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||184(!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value) >;185186template <class _UDel>187using _EnableIfDeleterAssignable = __enable_if_t< is_assignable<_Dp&, _UDel&&>::value >;188189public:190template <bool _Dummy = true, class = _EnableIfDeleterDefaultConstructible<_Dummy> >191_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(__value_init_tag(), __value_init_tag()) {}192193template <bool _Dummy = true, class = _EnableIfDeleterDefaultConstructible<_Dummy> >194_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT195: __ptr_(__value_init_tag(), __value_init_tag()) {}196197template <bool _Dummy = true, class = _EnableIfDeleterDefaultConstructible<_Dummy> >198_LIBCPP_HIDE_FROM_ABI199_LIBCPP_CONSTEXPR_SINCE_CXX23 explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p, __value_init_tag()) {}200201template <bool _Dummy = true, class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >202_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT203: __ptr_(__p, __d) {}204205template <bool _Dummy = true, class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >206_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT207: __ptr_(__p, std::move(__d)) {208static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");209}210211template <bool _Dummy = true, class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >212_LIBCPP_HIDE_FROM_ABI unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;213214_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr&& __u) _NOEXCEPT215: __ptr_(__u.release(), std::forward<deleter_type>(__u.get_deleter())) {}216217template <class _Up,218class _Ep,219class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,220class = _EnableIfDeleterConvertible<_Ep> >221_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT222: __ptr_(__u.release(), std::forward<_Ep>(__u.get_deleter())) {}223224#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)225template <class _Up,226__enable_if_t<is_convertible<_Up*, _Tp*>::value && is_same<_Dp, default_delete<_Tp> >::value, int> = 0>227_LIBCPP_HIDE_FROM_ABI unique_ptr(auto_ptr<_Up>&& __p) _NOEXCEPT : __ptr_(__p.release(), __value_init_tag()) {}228#endif229230_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {231reset(__u.release());232__ptr_.second() = std::forward<deleter_type>(__u.get_deleter());233return *this;234}235236template <class _Up,237class _Ep,238class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,239class = _EnableIfDeleterAssignable<_Ep> >240_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {241reset(__u.release());242__ptr_.second() = std::forward<_Ep>(__u.get_deleter());243return *this;244}245246#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)247template <class _Up,248__enable_if_t<is_convertible<_Up*, _Tp*>::value && is_same<_Dp, default_delete<_Tp> >::value, int> = 0>249_LIBCPP_HIDE_FROM_ABI unique_ptr& operator=(auto_ptr<_Up> __p) {250reset(__p.release());251return *this;252}253#endif254255#ifdef _LIBCPP_CXX03_LANG256unique_ptr(unique_ptr const&) = delete;257unique_ptr& operator=(unique_ptr const&) = delete;258#endif259260_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 ~unique_ptr() { reset(); }261262_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(nullptr_t) _NOEXCEPT {263reset();264return *this;265}266267_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __add_lvalue_reference_t<_Tp> operator*() const268_NOEXCEPT_(__is_noexcept_deref_or_void<pointer>::value) {269return *__ptr_.first();270}271_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer operator->() const _NOEXCEPT { return __ptr_.first(); }272_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer get() const _NOEXCEPT { return __ptr_.first(); }273_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 deleter_type& get_deleter() _NOEXCEPT { return __ptr_.second(); }274_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 const deleter_type& get_deleter() const _NOEXCEPT {275return __ptr_.second();276}277_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit operator bool() const _NOEXCEPT {278return __ptr_.first() != nullptr;279}280281_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer release() _NOEXCEPT {282pointer __t = __ptr_.first();283__ptr_.first() = pointer();284return __t;285}286287_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void reset(pointer __p = pointer()) _NOEXCEPT {288pointer __tmp = __ptr_.first();289__ptr_.first() = __p;290if (__tmp)291__ptr_.second()(__tmp);292}293294_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void swap(unique_ptr& __u) _NOEXCEPT { __ptr_.swap(__u.__ptr_); }295};296297template <class _Tp, class _Dp>298class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {299public:300typedef _Tp element_type;301typedef _Dp deleter_type;302typedef typename __pointer<_Tp, deleter_type>::type pointer;303304// A unique_ptr contains the following members which may be trivially relocatable:305// - pointer : this may be trivially relocatable, so it's checked306// - deleter_type: this may be trivially relocatable, so it's checked307//308// This unique_ptr implementation only contains a pointer to the unique object and a deleter, so there are no309// references to itself. This means that the entire structure is trivially relocatable if its members are.310using __trivially_relocatable = __conditional_t<311__libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<deleter_type>::value,312unique_ptr,313void>;314315private:316__compressed_pair<pointer, deleter_type> __ptr_;317318template <class _From>319struct _CheckArrayPointerConversion : is_same<_From, pointer> {};320321template <class _FromElem>322struct _CheckArrayPointerConversion<_FromElem*>323: integral_constant<bool,324is_same<_FromElem*, pointer>::value ||325(is_same<pointer, element_type*>::value &&326is_convertible<_FromElem (*)[], element_type (*)[]>::value) > {};327328typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;329330template <bool _Dummy>331using _LValRefType _LIBCPP_NODEBUG = typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;332333template <bool _Dummy>334using _GoodRValRefType _LIBCPP_NODEBUG = typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;335336template <bool _Dummy>337using _BadRValRefType _LIBCPP_NODEBUG = typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;338339template <bool _Dummy, class _Deleter = typename __dependent_type< __type_identity<deleter_type>, _Dummy>::type>340using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG =341__enable_if_t<is_default_constructible<_Deleter>::value && !is_pointer<_Deleter>::value>;342343template <class _ArgType>344using _EnableIfDeleterConstructible _LIBCPP_NODEBUG = __enable_if_t<is_constructible<deleter_type, _ArgType>::value>;345346template <class _Pp>347using _EnableIfPointerConvertible _LIBCPP_NODEBUG = __enable_if_t< _CheckArrayPointerConversion<_Pp>::value >;348349template <class _UPtr, class _Up, class _ElemT = typename _UPtr::element_type>350using _EnableIfMoveConvertible _LIBCPP_NODEBUG =351__enable_if_t< is_array<_Up>::value && is_same<pointer, element_type*>::value &&352is_same<typename _UPtr::pointer, _ElemT*>::value &&353is_convertible<_ElemT (*)[], element_type (*)[]>::value >;354355template <class _UDel>356using _EnableIfDeleterConvertible _LIBCPP_NODEBUG =357__enable_if_t< (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||358(!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value) >;359360template <class _UDel>361using _EnableIfDeleterAssignable _LIBCPP_NODEBUG = __enable_if_t< is_assignable<_Dp&, _UDel&&>::value >;362363public:364template <bool _Dummy = true, class = _EnableIfDeleterDefaultConstructible<_Dummy> >365_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(__value_init_tag(), __value_init_tag()) {}366367template <bool _Dummy = true, class = _EnableIfDeleterDefaultConstructible<_Dummy> >368_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT369: __ptr_(__value_init_tag(), __value_init_tag()) {}370371template <class _Pp,372bool _Dummy = true,373class = _EnableIfDeleterDefaultConstructible<_Dummy>,374class = _EnableIfPointerConvertible<_Pp> >375_LIBCPP_HIDE_FROM_ABI376_LIBCPP_CONSTEXPR_SINCE_CXX23 explicit unique_ptr(_Pp __p) _NOEXCEPT : __ptr_(__p, __value_init_tag()) {}377378template <class _Pp,379bool _Dummy = true,380class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,381class = _EnableIfPointerConvertible<_Pp> >382_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT383: __ptr_(__p, __d) {}384385template <bool _Dummy = true, class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >386_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT387: __ptr_(nullptr, __d) {}388389template <class _Pp,390bool _Dummy = true,391class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,392class = _EnableIfPointerConvertible<_Pp> >393_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT394: __ptr_(__p, std::move(__d)) {395static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");396}397398template <bool _Dummy = true, class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >399_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT400: __ptr_(nullptr, std::move(__d)) {401static_assert(!is_reference<deleter_type>::value, "rvalue deleter bound to reference");402}403404template <class _Pp,405bool _Dummy = true,406class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,407class = _EnableIfPointerConvertible<_Pp> >408_LIBCPP_HIDE_FROM_ABI unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;409410_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr&& __u) _NOEXCEPT411: __ptr_(__u.release(), std::forward<deleter_type>(__u.get_deleter())) {}412413_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {414reset(__u.release());415__ptr_.second() = std::forward<deleter_type>(__u.get_deleter());416return *this;417}418419template <class _Up,420class _Ep,421class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,422class = _EnableIfDeleterConvertible<_Ep> >423_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT424: __ptr_(__u.release(), std::forward<_Ep>(__u.get_deleter())) {}425426template <class _Up,427class _Ep,428class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,429class = _EnableIfDeleterAssignable<_Ep> >430_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {431reset(__u.release());432__ptr_.second() = std::forward<_Ep>(__u.get_deleter());433return *this;434}435436#ifdef _LIBCPP_CXX03_LANG437unique_ptr(unique_ptr const&) = delete;438unique_ptr& operator=(unique_ptr const&) = delete;439#endif440441public:442_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 ~unique_ptr() { reset(); }443444_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr& operator=(nullptr_t) _NOEXCEPT {445reset();446return *this;447}448449_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __add_lvalue_reference_t<_Tp> operator[](size_t __i) const {450return __ptr_.first()[__i];451}452_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer get() const _NOEXCEPT { return __ptr_.first(); }453454_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 deleter_type& get_deleter() _NOEXCEPT { return __ptr_.second(); }455456_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 const deleter_type& get_deleter() const _NOEXCEPT {457return __ptr_.second();458}459_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit operator bool() const _NOEXCEPT {460return __ptr_.first() != nullptr;461}462463_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer release() _NOEXCEPT {464pointer __t = __ptr_.first();465__ptr_.first() = pointer();466return __t;467}468469template <class _Pp, __enable_if_t<_CheckArrayPointerConversion<_Pp>::value, int> = 0>470_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void reset(_Pp __p) _NOEXCEPT {471pointer __tmp = __ptr_.first();472__ptr_.first() = __p;473if (__tmp)474__ptr_.second()(__tmp);475}476477_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void reset(nullptr_t = nullptr) _NOEXCEPT {478pointer __tmp = __ptr_.first();479__ptr_.first() = nullptr;480if (__tmp)481__ptr_.second()(__tmp);482}483484_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void swap(unique_ptr& __u) _NOEXCEPT { __ptr_.swap(__u.__ptr_); }485};486487template <class _Tp, class _Dp, __enable_if_t<__is_swappable_v<_Dp>, int> = 0>488inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void489swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {490__x.swap(__y);491}492493template <class _T1, class _D1, class _T2, class _D2>494inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool495operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {496return __x.get() == __y.get();497}498499#if _LIBCPP_STD_VER <= 17500template <class _T1, class _D1, class _T2, class _D2>501inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {502return !(__x == __y);503}504#endif505506template <class _T1, class _D1, class _T2, class _D2>507inline _LIBCPP_HIDE_FROM_ABI bool operator<(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {508typedef typename unique_ptr<_T1, _D1>::pointer _P1;509typedef typename unique_ptr<_T2, _D2>::pointer _P2;510typedef typename common_type<_P1, _P2>::type _Vp;511return less<_Vp>()(__x.get(), __y.get());512}513514template <class _T1, class _D1, class _T2, class _D2>515inline _LIBCPP_HIDE_FROM_ABI bool operator>(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {516return __y < __x;517}518519template <class _T1, class _D1, class _T2, class _D2>520inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {521return !(__y < __x);522}523524template <class _T1, class _D1, class _T2, class _D2>525inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {526return !(__x < __y);527}528529#if _LIBCPP_STD_VER >= 20530template <class _T1, class _D1, class _T2, class _D2>531requires three_way_comparable_with<typename unique_ptr<_T1, _D1>::pointer, typename unique_ptr<_T2, _D2>::pointer>532_LIBCPP_HIDE_FROM_ABI533compare_three_way_result_t<typename unique_ptr<_T1, _D1>::pointer, typename unique_ptr<_T2, _D2>::pointer>534operator<=>(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {535return compare_three_way()(__x.get(), __y.get());536}537#endif538539template <class _T1, class _D1>540inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool541operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT {542return !__x;543}544545#if _LIBCPP_STD_VER <= 17546template <class _T1, class _D1>547inline _LIBCPP_HIDE_FROM_ABI bool operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT {548return !__x;549}550551template <class _T1, class _D1>552inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT {553return static_cast<bool>(__x);554}555556template <class _T1, class _D1>557inline _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT {558return static_cast<bool>(__x);559}560#endif // _LIBCPP_STD_VER <= 17561562template <class _T1, class _D1>563inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t) {564typedef typename unique_ptr<_T1, _D1>::pointer _P1;565return less<_P1>()(__x.get(), nullptr);566}567568template <class _T1, class _D1>569inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x) {570typedef typename unique_ptr<_T1, _D1>::pointer _P1;571return less<_P1>()(nullptr, __x.get());572}573574template <class _T1, class _D1>575inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t) {576return nullptr < __x;577}578579template <class _T1, class _D1>580inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x) {581return __x < nullptr;582}583584template <class _T1, class _D1>585inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t) {586return !(nullptr < __x);587}588589template <class _T1, class _D1>590inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x) {591return !(__x < nullptr);592}593594template <class _T1, class _D1>595inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t) {596return !(__x < nullptr);597}598599template <class _T1, class _D1>600inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x) {601return !(nullptr < __x);602}603604#if _LIBCPP_STD_VER >= 20605template <class _T1, class _D1>606requires three_way_comparable< typename unique_ptr<_T1, _D1>::pointer>607_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 compare_three_way_result_t<typename unique_ptr<_T1, _D1>::pointer>608operator<=>(const unique_ptr<_T1, _D1>& __x, nullptr_t) {609return compare_three_way()(__x.get(), static_cast<typename unique_ptr<_T1, _D1>::pointer>(nullptr));610}611#endif612613#if _LIBCPP_STD_VER >= 14614615template <class _Tp>616struct __unique_if {617typedef unique_ptr<_Tp> __unique_single;618};619620template <class _Tp>621struct __unique_if<_Tp[]> {622typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;623};624625template <class _Tp, size_t _Np>626struct __unique_if<_Tp[_Np]> {627typedef void __unique_array_known_bound;628};629630template <class _Tp, class... _Args>631inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_single632make_unique(_Args&&... __args) {633return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...));634}635636template <class _Tp>637inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_array_unknown_bound638make_unique(size_t __n) {639typedef __remove_extent_t<_Tp> _Up;640return unique_ptr<_Tp>(new _Up[__n]());641}642643template <class _Tp, class... _Args>644typename __unique_if<_Tp>::__unique_array_known_bound make_unique(_Args&&...) = delete;645646#endif // _LIBCPP_STD_VER >= 14647648#if _LIBCPP_STD_VER >= 20649650template <class _Tp>651_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_single652make_unique_for_overwrite() {653return unique_ptr<_Tp>(new _Tp);654}655656template <class _Tp>657_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_array_unknown_bound658make_unique_for_overwrite(size_t __n) {659return unique_ptr<_Tp>(new __remove_extent_t<_Tp>[__n]);660}661662template <class _Tp, class... _Args>663typename __unique_if<_Tp>::__unique_array_known_bound make_unique_for_overwrite(_Args&&...) = delete;664665#endif // _LIBCPP_STD_VER >= 20666667template <class _Tp>668struct _LIBCPP_TEMPLATE_VIS hash;669670template <class _Tp, class _Dp>671#ifdef _LIBCPP_CXX03_LANG672struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >673#else674struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper< unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >675#endif676{677#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)678_LIBCPP_DEPRECATED_IN_CXX17 typedef unique_ptr<_Tp, _Dp> argument_type;679_LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;680#endif681682_LIBCPP_HIDE_FROM_ABI size_t operator()(const unique_ptr<_Tp, _Dp>& __ptr) const {683typedef typename unique_ptr<_Tp, _Dp>::pointer pointer;684return hash<pointer>()(__ptr.get());685}686};687688_LIBCPP_END_NAMESPACE_STD689690_LIBCPP_POP_MACROS691692#endif // _LIBCPP___MEMORY_UNIQUE_PTR_H693694695