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