libstdc++
|
00001 // unique_ptr implementation -*- C++ -*- 00002 00003 // Copyright (C) 2008-2016 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file bits/unique_ptr.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{memory} 00028 */ 00029 00030 #ifndef _UNIQUE_PTR_H 00031 #define _UNIQUE_PTR_H 1 00032 00033 #include <bits/c++config.h> 00034 #include <debug/assertions.h> 00035 #include <type_traits> 00036 #include <utility> 00037 #include <tuple> 00038 00039 namespace std _GLIBCXX_VISIBILITY(default) 00040 { 00041 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00042 00043 /** 00044 * @addtogroup pointer_abstractions 00045 * @{ 00046 */ 00047 00048 #if _GLIBCXX_USE_DEPRECATED 00049 template<typename> class auto_ptr; 00050 #endif 00051 00052 /// Primary template of default_delete, used by unique_ptr 00053 template<typename _Tp> 00054 struct default_delete 00055 { 00056 /// Default constructor 00057 constexpr default_delete() noexcept = default; 00058 00059 /** @brief Converting constructor. 00060 * 00061 * Allows conversion from a deleter for arrays of another type, @p _Up, 00062 * only if @p _Up* is convertible to @p _Tp*. 00063 */ 00064 template<typename _Up, typename = typename 00065 enable_if<is_convertible<_Up*, _Tp*>::value>::type> 00066 default_delete(const default_delete<_Up>&) noexcept { } 00067 00068 /// Calls @c delete @p __ptr 00069 void 00070 operator()(_Tp* __ptr) const 00071 { 00072 static_assert(!is_void<_Tp>::value, 00073 "can't delete pointer to incomplete type"); 00074 static_assert(sizeof(_Tp)>0, 00075 "can't delete pointer to incomplete type"); 00076 delete __ptr; 00077 } 00078 }; 00079 00080 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00081 // DR 740 - omit specialization for array objects with a compile time length 00082 /// Specialization for arrays, default_delete. 00083 template<typename _Tp> 00084 struct default_delete<_Tp[]> 00085 { 00086 public: 00087 /// Default constructor 00088 constexpr default_delete() noexcept = default; 00089 00090 /** @brief Converting constructor. 00091 * 00092 * Allows conversion from a deleter for arrays of another type, such as 00093 * a const-qualified version of @p _Tp. 00094 * 00095 * Conversions from types derived from @c _Tp are not allowed because 00096 * it is unsafe to @c delete[] an array of derived types through a 00097 * pointer to the base type. 00098 */ 00099 template<typename _Up, typename = typename 00100 enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type> 00101 default_delete(const default_delete<_Up[]>&) noexcept { } 00102 00103 /// Calls @c delete[] @p __ptr 00104 template<typename _Up> 00105 typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type 00106 operator()(_Up* __ptr) const 00107 { 00108 static_assert(sizeof(_Tp)>0, 00109 "can't delete pointer to incomplete type"); 00110 delete [] __ptr; 00111 } 00112 }; 00113 00114 /// 20.7.1.2 unique_ptr for single objects. 00115 template <typename _Tp, typename _Dp = default_delete<_Tp> > 00116 class unique_ptr 00117 { 00118 // use SFINAE to determine whether _Del::pointer exists 00119 class _Pointer 00120 { 00121 template<typename _Up> 00122 static typename _Up::pointer __test(typename _Up::pointer*); 00123 00124 template<typename _Up> 00125 static _Tp* __test(...); 00126 00127 typedef typename remove_reference<_Dp>::type _Del; 00128 00129 public: 00130 typedef decltype(__test<_Del>(0)) type; 00131 }; 00132 00133 typedef std::tuple<typename _Pointer::type, _Dp> __tuple_type; 00134 __tuple_type _M_t; 00135 00136 public: 00137 typedef typename _Pointer::type pointer; 00138 typedef _Tp element_type; 00139 typedef _Dp deleter_type; 00140 00141 00142 // helper template for detecting a safe conversion from another 00143 // unique_ptr 00144 template<typename _Up, typename _Ep> 00145 using __safe_conversion_up = __and_< 00146 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>, 00147 __not_<is_array<_Up>>, 00148 __or_<__and_<is_reference<deleter_type>, 00149 is_same<deleter_type, _Ep>>, 00150 __and_<__not_<is_reference<deleter_type>>, 00151 is_convertible<_Ep, deleter_type>> 00152 > 00153 >; 00154 00155 // Constructors. 00156 00157 /// Default constructor, creates a unique_ptr that owns nothing. 00158 constexpr unique_ptr() noexcept 00159 : _M_t() 00160 { static_assert(!is_pointer<deleter_type>::value, 00161 "constructed with null function pointer deleter"); } 00162 00163 /** Takes ownership of a pointer. 00164 * 00165 * @param __p A pointer to an object of @c element_type 00166 * 00167 * The deleter will be value-initialized. 00168 */ 00169 explicit 00170 unique_ptr(pointer __p) noexcept 00171 : _M_t() 00172 { 00173 std::get<0>(_M_t) = __p; 00174 static_assert(!is_pointer<deleter_type>::value, 00175 "constructed with null function pointer deleter"); 00176 } 00177 00178 /** Takes ownership of a pointer. 00179 * 00180 * @param __p A pointer to an object of @c element_type 00181 * @param __d A reference to a deleter. 00182 * 00183 * The deleter will be initialized with @p __d 00184 */ 00185 unique_ptr(pointer __p, 00186 typename conditional<is_reference<deleter_type>::value, 00187 deleter_type, const deleter_type&>::type __d) noexcept 00188 : _M_t(__p, __d) { } 00189 00190 /** Takes ownership of a pointer. 00191 * 00192 * @param __p A pointer to an object of @c element_type 00193 * @param __d An rvalue reference to a deleter. 00194 * 00195 * The deleter will be initialized with @p std::move(__d) 00196 */ 00197 unique_ptr(pointer __p, 00198 typename remove_reference<deleter_type>::type&& __d) noexcept 00199 : _M_t(std::move(__p), std::move(__d)) 00200 { static_assert(!std::is_reference<deleter_type>::value, 00201 "rvalue deleter bound to reference"); } 00202 00203 /// Creates a unique_ptr that owns nothing. 00204 constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { } 00205 00206 // Move constructors. 00207 00208 /// Move constructor. 00209 unique_ptr(unique_ptr&& __u) noexcept 00210 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { } 00211 00212 /** @brief Converting constructor from another type 00213 * 00214 * Requires that the pointer owned by @p __u is convertible to the 00215 * type of pointer owned by this object, @p __u does not own an array, 00216 * and @p __u has a compatible deleter type. 00217 */ 00218 template<typename _Up, typename _Ep, typename = _Require< 00219 __safe_conversion_up<_Up, _Ep>, 00220 typename conditional<is_reference<_Dp>::value, 00221 is_same<_Ep, _Dp>, 00222 is_convertible<_Ep, _Dp>>::type>> 00223 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept 00224 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter())) 00225 { } 00226 00227 #if _GLIBCXX_USE_DEPRECATED 00228 /// Converting constructor from @c auto_ptr 00229 template<typename _Up, typename = _Require< 00230 is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>> 00231 unique_ptr(auto_ptr<_Up>&& __u) noexcept; 00232 #endif 00233 00234 /// Destructor, invokes the deleter if the stored pointer is not null. 00235 ~unique_ptr() noexcept 00236 { 00237 auto& __ptr = std::get<0>(_M_t); 00238 if (__ptr != nullptr) 00239 get_deleter()(__ptr); 00240 __ptr = pointer(); 00241 } 00242 00243 // Assignment. 00244 00245 /** @brief Move assignment operator. 00246 * 00247 * @param __u The object to transfer ownership from. 00248 * 00249 * Invokes the deleter first if this object owns a pointer. 00250 */ 00251 unique_ptr& 00252 operator=(unique_ptr&& __u) noexcept 00253 { 00254 reset(__u.release()); 00255 get_deleter() = std::forward<deleter_type>(__u.get_deleter()); 00256 return *this; 00257 } 00258 00259 /** @brief Assignment from another type. 00260 * 00261 * @param __u The object to transfer ownership from, which owns a 00262 * convertible pointer to a non-array object. 00263 * 00264 * Invokes the deleter first if this object owns a pointer. 00265 */ 00266 template<typename _Up, typename _Ep> 00267 typename enable_if< __and_< 00268 __safe_conversion_up<_Up, _Ep>, 00269 is_assignable<deleter_type&, _Ep&&> 00270 >::value, 00271 unique_ptr&>::type 00272 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept 00273 { 00274 reset(__u.release()); 00275 get_deleter() = std::forward<_Ep>(__u.get_deleter()); 00276 return *this; 00277 } 00278 00279 /// Reset the %unique_ptr to empty, invoking the deleter if necessary. 00280 unique_ptr& 00281 operator=(nullptr_t) noexcept 00282 { 00283 reset(); 00284 return *this; 00285 } 00286 00287 // Observers. 00288 00289 /// Dereference the stored pointer. 00290 typename add_lvalue_reference<element_type>::type 00291 operator*() const 00292 { 00293 __glibcxx_assert(get() != pointer()); 00294 return *get(); 00295 } 00296 00297 /// Return the stored pointer. 00298 pointer 00299 operator->() const noexcept 00300 { 00301 _GLIBCXX_DEBUG_PEDASSERT(get() != pointer()); 00302 return get(); 00303 } 00304 00305 /// Return the stored pointer. 00306 pointer 00307 get() const noexcept 00308 { return std::get<0>(_M_t); } 00309 00310 /// Return a reference to the stored deleter. 00311 deleter_type& 00312 get_deleter() noexcept 00313 { return std::get<1>(_M_t); } 00314 00315 /// Return a reference to the stored deleter. 00316 const deleter_type& 00317 get_deleter() const noexcept 00318 { return std::get<1>(_M_t); } 00319 00320 /// Return @c true if the stored pointer is not null. 00321 explicit operator bool() const noexcept 00322 { return get() == pointer() ? false : true; } 00323 00324 // Modifiers. 00325 00326 /// Release ownership of any stored pointer. 00327 pointer 00328 release() noexcept 00329 { 00330 pointer __p = get(); 00331 std::get<0>(_M_t) = pointer(); 00332 return __p; 00333 } 00334 00335 /** @brief Replace the stored pointer. 00336 * 00337 * @param __p The new pointer to store. 00338 * 00339 * The deleter will be invoked if a pointer is already owned. 00340 */ 00341 void 00342 reset(pointer __p = pointer()) noexcept 00343 { 00344 using std::swap; 00345 swap(std::get<0>(_M_t), __p); 00346 if (__p != pointer()) 00347 get_deleter()(__p); 00348 } 00349 00350 /// Exchange the pointer and deleter with another object. 00351 void 00352 swap(unique_ptr& __u) noexcept 00353 { 00354 using std::swap; 00355 swap(_M_t, __u._M_t); 00356 } 00357 00358 // Disable copy from lvalue. 00359 unique_ptr(const unique_ptr&) = delete; 00360 unique_ptr& operator=(const unique_ptr&) = delete; 00361 }; 00362 00363 /// 20.7.1.3 unique_ptr for array objects with a runtime length 00364 // [unique.ptr.runtime] 00365 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00366 // DR 740 - omit specialization for array objects with a compile time length 00367 template<typename _Tp, typename _Dp> 00368 class unique_ptr<_Tp[], _Dp> 00369 { 00370 // use SFINAE to determine whether _Del::pointer exists 00371 class _Pointer 00372 { 00373 template<typename _Up> 00374 static typename _Up::pointer __test(typename _Up::pointer*); 00375 00376 template<typename _Up> 00377 static _Tp* __test(...); 00378 00379 typedef typename remove_reference<_Dp>::type _Del; 00380 00381 public: 00382 typedef decltype(__test<_Del>(0)) type; 00383 }; 00384 00385 typedef std::tuple<typename _Pointer::type, _Dp> __tuple_type; 00386 __tuple_type _M_t; 00387 00388 template<typename _Up> 00389 using __remove_cv = typename remove_cv<_Up>::type; 00390 00391 // like is_base_of<_Tp, _Up> but false if unqualified types are the same 00392 template<typename _Up> 00393 using __is_derived_Tp 00394 = __and_< is_base_of<_Tp, _Up>, 00395 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >; 00396 00397 00398 public: 00399 typedef typename _Pointer::type pointer; 00400 typedef _Tp element_type; 00401 typedef _Dp deleter_type; 00402 00403 // helper template for detecting a safe conversion from another 00404 // unique_ptr 00405 template<typename _Up, typename _Ep, 00406 typename _Up_up = unique_ptr<_Up, _Ep>, 00407 typename _Up_element_type = typename _Up_up::element_type> 00408 using __safe_conversion_up = __and_< 00409 is_array<_Up>, 00410 is_same<pointer, element_type*>, 00411 is_same<typename _Up_up::pointer, _Up_element_type*>, 00412 is_convertible<_Up_element_type(*)[], element_type(*)[]>, 00413 __or_<__and_<is_reference<deleter_type>, is_same<deleter_type, _Ep>>, 00414 __and_<__not_<is_reference<deleter_type>>, 00415 is_convertible<_Ep, deleter_type>>> 00416 >; 00417 00418 // helper template for detecting a safe conversion from a raw pointer 00419 template<typename _Up> 00420 using __safe_conversion_raw = __and_< 00421 __or_<__or_<is_same<_Up, pointer>, 00422 is_same<_Up, nullptr_t>>, 00423 __and_<is_pointer<_Up>, 00424 is_same<pointer, element_type*>, 00425 is_convertible< 00426 typename remove_pointer<_Up>::type(*)[], 00427 element_type(*)[]> 00428 > 00429 > 00430 >; 00431 00432 // Constructors. 00433 00434 /// Default constructor, creates a unique_ptr that owns nothing. 00435 constexpr unique_ptr() noexcept 00436 : _M_t() 00437 { static_assert(!std::is_pointer<deleter_type>::value, 00438 "constructed with null function pointer deleter"); } 00439 00440 /** Takes ownership of a pointer. 00441 * 00442 * @param __p A pointer to an array of a type safely convertible 00443 * to an array of @c element_type 00444 * 00445 * The deleter will be value-initialized. 00446 */ 00447 template<typename _Up, 00448 typename = typename enable_if< 00449 __safe_conversion_raw<_Up>::value, bool>::type> 00450 explicit 00451 unique_ptr(_Up __p) noexcept 00452 : _M_t(__p, deleter_type()) 00453 { static_assert(!is_pointer<deleter_type>::value, 00454 "constructed with null function pointer deleter"); } 00455 00456 /** Takes ownership of a pointer. 00457 * 00458 * @param __p A pointer to an array of a type safely convertible 00459 * to an array of @c element_type 00460 * @param __d A reference to a deleter. 00461 * 00462 * The deleter will be initialized with @p __d 00463 */ 00464 template<typename _Up, 00465 typename = typename enable_if< 00466 __safe_conversion_raw<_Up>::value, bool>::type> 00467 unique_ptr(_Up __p, 00468 typename conditional<is_reference<deleter_type>::value, 00469 deleter_type, const deleter_type&>::type __d) noexcept 00470 : _M_t(__p, __d) { } 00471 00472 /** Takes ownership of a pointer. 00473 * 00474 * @param __p A pointer to an array of a type safely convertible 00475 * to an array of @c element_type 00476 * @param __d A reference to a deleter. 00477 * 00478 * The deleter will be initialized with @p std::move(__d) 00479 */ 00480 template<typename _Up, 00481 typename = typename enable_if< 00482 __safe_conversion_raw<_Up>::value, bool>::type> 00483 unique_ptr(_Up __p, typename 00484 remove_reference<deleter_type>::type&& __d) noexcept 00485 : _M_t(std::move(__p), std::move(__d)) 00486 { static_assert(!is_reference<deleter_type>::value, 00487 "rvalue deleter bound to reference"); } 00488 00489 /// Move constructor. 00490 unique_ptr(unique_ptr&& __u) noexcept 00491 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { } 00492 00493 /// Creates a unique_ptr that owns nothing. 00494 constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { } 00495 00496 template<typename _Up, typename _Ep, 00497 typename = _Require<__safe_conversion_up<_Up, _Ep>>> 00498 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept 00499 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter())) 00500 { } 00501 00502 /// Destructor, invokes the deleter if the stored pointer is not null. 00503 ~unique_ptr() 00504 { 00505 auto& __ptr = std::get<0>(_M_t); 00506 if (__ptr != nullptr) 00507 get_deleter()(__ptr); 00508 __ptr = pointer(); 00509 } 00510 00511 // Assignment. 00512 00513 /** @brief Move assignment operator. 00514 * 00515 * @param __u The object to transfer ownership from. 00516 * 00517 * Invokes the deleter first if this object owns a pointer. 00518 */ 00519 unique_ptr& 00520 operator=(unique_ptr&& __u) noexcept 00521 { 00522 reset(__u.release()); 00523 get_deleter() = std::forward<deleter_type>(__u.get_deleter()); 00524 return *this; 00525 } 00526 00527 /** @brief Assignment from another type. 00528 * 00529 * @param __u The object to transfer ownership from, which owns a 00530 * convertible pointer to an array object. 00531 * 00532 * Invokes the deleter first if this object owns a pointer. 00533 */ 00534 template<typename _Up, typename _Ep> 00535 typename 00536 enable_if<__and_<__safe_conversion_up<_Up, _Ep>, 00537 is_assignable<deleter_type&, _Ep&&> 00538 >::value, 00539 unique_ptr&>::type 00540 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept 00541 { 00542 reset(__u.release()); 00543 get_deleter() = std::forward<_Ep>(__u.get_deleter()); 00544 return *this; 00545 } 00546 00547 /// Reset the %unique_ptr to empty, invoking the deleter if necessary. 00548 unique_ptr& 00549 operator=(nullptr_t) noexcept 00550 { 00551 reset(); 00552 return *this; 00553 } 00554 00555 // Observers. 00556 00557 /// Access an element of owned array. 00558 typename std::add_lvalue_reference<element_type>::type 00559 operator[](size_t __i) const 00560 { 00561 __glibcxx_assert(get() != pointer()); 00562 return get()[__i]; 00563 } 00564 00565 /// Return the stored pointer. 00566 pointer 00567 get() const noexcept 00568 { return std::get<0>(_M_t); } 00569 00570 /// Return a reference to the stored deleter. 00571 deleter_type& 00572 get_deleter() noexcept 00573 { return std::get<1>(_M_t); } 00574 00575 /// Return a reference to the stored deleter. 00576 const deleter_type& 00577 get_deleter() const noexcept 00578 { return std::get<1>(_M_t); } 00579 00580 /// Return @c true if the stored pointer is not null. 00581 explicit operator bool() const noexcept 00582 { return get() == pointer() ? false : true; } 00583 00584 // Modifiers. 00585 00586 /// Release ownership of any stored pointer. 00587 pointer 00588 release() noexcept 00589 { 00590 pointer __p = get(); 00591 std::get<0>(_M_t) = pointer(); 00592 return __p; 00593 } 00594 00595 /** @brief Replace the stored pointer. 00596 * 00597 * @param __p The new pointer to store. 00598 * 00599 * The deleter will be invoked if a pointer is already owned. 00600 */ 00601 template <typename _Up, 00602 typename = _Require< 00603 __or_<is_same<_Up, pointer>, 00604 __and_<is_same<pointer, element_type*>, 00605 is_pointer<_Up>, 00606 is_convertible< 00607 typename remove_pointer<_Up>::type(*)[], 00608 element_type(*)[] 00609 > 00610 > 00611 > 00612 >> 00613 void 00614 reset(_Up __p) noexcept 00615 { 00616 pointer __ptr = __p; 00617 using std::swap; 00618 swap(std::get<0>(_M_t), __ptr); 00619 if (__ptr != nullptr) 00620 get_deleter()(__ptr); 00621 } 00622 00623 void reset(nullptr_t = nullptr) noexcept 00624 { 00625 reset(pointer()); 00626 } 00627 00628 /// Exchange the pointer and deleter with another object. 00629 void 00630 swap(unique_ptr& __u) noexcept 00631 { 00632 using std::swap; 00633 swap(_M_t, __u._M_t); 00634 } 00635 00636 // Disable copy from lvalue. 00637 unique_ptr(const unique_ptr&) = delete; 00638 unique_ptr& operator=(const unique_ptr&) = delete; 00639 }; 00640 00641 template<typename _Tp, typename _Dp> 00642 inline void 00643 swap(unique_ptr<_Tp, _Dp>& __x, 00644 unique_ptr<_Tp, _Dp>& __y) noexcept 00645 { __x.swap(__y); } 00646 00647 template<typename _Tp, typename _Dp, 00648 typename _Up, typename _Ep> 00649 inline bool 00650 operator==(const unique_ptr<_Tp, _Dp>& __x, 00651 const unique_ptr<_Up, _Ep>& __y) 00652 { return __x.get() == __y.get(); } 00653 00654 template<typename _Tp, typename _Dp> 00655 inline bool 00656 operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept 00657 { return !__x; } 00658 00659 template<typename _Tp, typename _Dp> 00660 inline bool 00661 operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept 00662 { return !__x; } 00663 00664 template<typename _Tp, typename _Dp, 00665 typename _Up, typename _Ep> 00666 inline bool 00667 operator!=(const unique_ptr<_Tp, _Dp>& __x, 00668 const unique_ptr<_Up, _Ep>& __y) 00669 { return __x.get() != __y.get(); } 00670 00671 template<typename _Tp, typename _Dp> 00672 inline bool 00673 operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept 00674 { return (bool)__x; } 00675 00676 template<typename _Tp, typename _Dp> 00677 inline bool 00678 operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept 00679 { return (bool)__x; } 00680 00681 template<typename _Tp, typename _Dp, 00682 typename _Up, typename _Ep> 00683 inline bool 00684 operator<(const unique_ptr<_Tp, _Dp>& __x, 00685 const unique_ptr<_Up, _Ep>& __y) 00686 { 00687 typedef typename 00688 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer, 00689 typename unique_ptr<_Up, _Ep>::pointer>::type _CT; 00690 return std::less<_CT>()(__x.get(), __y.get()); 00691 } 00692 00693 template<typename _Tp, typename _Dp> 00694 inline bool 00695 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) 00696 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(), 00697 nullptr); } 00698 00699 template<typename _Tp, typename _Dp> 00700 inline bool 00701 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) 00702 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr, 00703 __x.get()); } 00704 00705 template<typename _Tp, typename _Dp, 00706 typename _Up, typename _Ep> 00707 inline bool 00708 operator<=(const unique_ptr<_Tp, _Dp>& __x, 00709 const unique_ptr<_Up, _Ep>& __y) 00710 { return !(__y < __x); } 00711 00712 template<typename _Tp, typename _Dp> 00713 inline bool 00714 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) 00715 { return !(nullptr < __x); } 00716 00717 template<typename _Tp, typename _Dp> 00718 inline bool 00719 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) 00720 { return !(__x < nullptr); } 00721 00722 template<typename _Tp, typename _Dp, 00723 typename _Up, typename _Ep> 00724 inline bool 00725 operator>(const unique_ptr<_Tp, _Dp>& __x, 00726 const unique_ptr<_Up, _Ep>& __y) 00727 { return (__y < __x); } 00728 00729 template<typename _Tp, typename _Dp> 00730 inline bool 00731 operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) 00732 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr, 00733 __x.get()); } 00734 00735 template<typename _Tp, typename _Dp> 00736 inline bool 00737 operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) 00738 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(), 00739 nullptr); } 00740 00741 template<typename _Tp, typename _Dp, 00742 typename _Up, typename _Ep> 00743 inline bool 00744 operator>=(const unique_ptr<_Tp, _Dp>& __x, 00745 const unique_ptr<_Up, _Ep>& __y) 00746 { return !(__x < __y); } 00747 00748 template<typename _Tp, typename _Dp> 00749 inline bool 00750 operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) 00751 { return !(__x < nullptr); } 00752 00753 template<typename _Tp, typename _Dp> 00754 inline bool 00755 operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) 00756 { return !(nullptr < __x); } 00757 00758 /// std::hash specialization for unique_ptr. 00759 template<typename _Tp, typename _Dp> 00760 struct hash<unique_ptr<_Tp, _Dp>> 00761 : public __hash_base<size_t, unique_ptr<_Tp, _Dp>> 00762 { 00763 size_t 00764 operator()(const unique_ptr<_Tp, _Dp>& __u) const noexcept 00765 { 00766 typedef unique_ptr<_Tp, _Dp> _UP; 00767 return std::hash<typename _UP::pointer>()(__u.get()); 00768 } 00769 }; 00770 00771 #if __cplusplus > 201103L 00772 00773 #define __cpp_lib_make_unique 201304 00774 00775 template<typename _Tp> 00776 struct _MakeUniq 00777 { typedef unique_ptr<_Tp> __single_object; }; 00778 00779 template<typename _Tp> 00780 struct _MakeUniq<_Tp[]> 00781 { typedef unique_ptr<_Tp[]> __array; }; 00782 00783 template<typename _Tp, size_t _Bound> 00784 struct _MakeUniq<_Tp[_Bound]> 00785 { struct __invalid_type { }; }; 00786 00787 /// std::make_unique for single objects 00788 template<typename _Tp, typename... _Args> 00789 inline typename _MakeUniq<_Tp>::__single_object 00790 make_unique(_Args&&... __args) 00791 { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); } 00792 00793 /// std::make_unique for arrays of unknown bound 00794 template<typename _Tp> 00795 inline typename _MakeUniq<_Tp>::__array 00796 make_unique(size_t __num) 00797 { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); } 00798 00799 /// Disable std::make_unique for arrays of known bound 00800 template<typename _Tp, typename... _Args> 00801 inline typename _MakeUniq<_Tp>::__invalid_type 00802 make_unique(_Args&&...) = delete; 00803 #endif 00804 00805 // @} group pointer_abstractions 00806 00807 _GLIBCXX_END_NAMESPACE_VERSION 00808 } // namespace 00809 00810 #endif /* _UNIQUE_PTR_H */