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