21 template<
typename U >
22 default_delete(default_delete<U>
const &) {}
24 void operator () (T *p)
const
29struct default_delete<T[]>
33 void operator () (T *p)
37template<
typename T,
typename C >
38struct unique_ptr_index_op {};
40template<
typename T,
typename C >
41struct unique_ptr_index_op<T[], C>
44 reference operator [] (
int idx)
const
45 {
return static_cast<C
const *
>(
this)->get()[idx]; }
48template<
typename T,
typename T_Del = default_delete<T> >
49class unique_ptr :
public unique_ptr_index_op<T, unique_ptr<T, T_Del> >
53 typedef _unspec* _unspec_ptr_type;
56 typedef cxx::remove_extent_t<T> element_type;
57 typedef element_type *pointer;
58 typedef element_type &reference;
59 typedef T_Del deleter_type;
61 unique_ptr() : _ptr(pointer()) {}
63 explicit unique_ptr(pointer p) : _ptr(p) {}
65 unique_ptr(unique_ptr &&o) : _ptr(o.release()) {}
67 ~unique_ptr() { reset(); }
69 unique_ptr &operator = (unique_ptr &&o)
75 unique_ptr &operator = (_unspec_ptr_type)
81 element_type &operator * ()
const {
return *get(); }
82 pointer operator -> ()
const {
return get(); }
84 pointer get()
const {
return _ptr; }
86 operator _unspec_ptr_type ()
const
87 {
return reinterpret_cast<_unspec_ptr_type
>(get()); }
96 void reset(pointer p = pointer())
100 deleter_type()(get());
105 unique_ptr(unique_ptr
const &) =
delete;
106 unique_ptr &operator = (unique_ptr
const &) =
delete;
112template<
typename T >
115{
return unique_ptr<T>(p); }
117template<
typename T >
118cxx::enable_if_t<cxx::is_array<T>::value, unique_ptr<T>>
119make_unique(
unsigned long size)
120{
return cxx::unique_ptr<T>(
new cxx::remove_extent_t<T>[size]()); }
122template<
typename T,
typename... Args >
123cxx::enable_if_t<!cxx::is_array<T>::value, unique_ptr<T>>
124make_unique(Args &&... args)
125{
return cxx::unique_ptr<T>(
new T(cxx::forward<Args>(args)...)); }