1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Smart pointer managing sole ownership of a resource. */
9 #ifndef mozilla_UniquePtr_h
10 #define mozilla_UniquePtr_h
12 #include "mozilla/Assertions.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/Compiler.h"
15 #include "mozilla/Move.h"
16 #include "mozilla/NullPtr.h"
17 #include "mozilla/Pair.h"
18 #include "mozilla/TypeTraits.h"
22 template<typename T
> class DefaultDelete
;
23 template<typename T
, class D
= DefaultDelete
<T
>> class UniquePtr
;
25 } // namespace mozilla
30 * UniquePtr is a smart pointer that wholly owns a resource. Ownership may be
31 * transferred out of a UniquePtr through explicit action, but otherwise the
32 * resource is destroyed when the UniquePtr is destroyed.
34 * UniquePtr is similar to C++98's std::auto_ptr, but it improves upon auto_ptr
35 * in one crucial way: it's impossible to copy a UniquePtr. Copying an auto_ptr
36 * obviously *can't* copy ownership of its singly-owned resource. So what
37 * happens if you try to copy one? Bizarrely, ownership is implicitly
38 * *transferred*, preserving single ownership but breaking code that assumes a
39 * copy of an object is identical to the original. (This is why auto_ptr is
40 * prohibited in STL containers.)
42 * UniquePtr solves this problem by being *movable* rather than copyable.
43 * Instead of passing a |UniquePtr u| directly to the constructor or assignment
44 * operator, you pass |Move(u)|. In doing so you indicate that you're *moving*
45 * ownership out of |u|, into the target of the construction/assignment. After
46 * the transfer completes, |u| contains |nullptr| and may be safely destroyed.
47 * This preserves single ownership but also allows UniquePtr to be moved by
48 * algorithms that have been made move-safe. (Note: if |u| is instead a
49 * temporary expression, don't use |Move()|: just pass the expression, because
50 * it's already move-ready. For more information see Move.h.)
52 * UniquePtr is also better than std::auto_ptr in that the deletion operation is
53 * customizable. An optional second template parameter specifies a class that
54 * (through its operator()(T*)) implements the desired deletion policy. If no
55 * policy is specified, mozilla::DefaultDelete<T> is used -- which will either
56 * |delete| or |delete[]| the resource, depending whether the resource is an
57 * array. Custom deletion policies ideally should be empty classes (no member
58 * fields, no member fields in base classes, no virtual methods/inheritance),
59 * because then UniquePtr can be just as efficient as a raw pointer.
61 * Use of UniquePtr proceeds like so:
63 * UniquePtr<int> g1; // initializes to nullptr
64 * g1.reset(new int); // switch resources using reset()
65 * g1 = nullptr; // clears g1, deletes the int
67 * UniquePtr<int> g2(new int); // owns that int
68 * int* p = g2.release(); // g2 leaks its int -- still requires deletion
69 * delete p; // now freed
71 * struct S { int x; S(int x) : x(x) {} };
72 * UniquePtr<S> g3, g4(new S(5));
73 * g3 = Move(g4); // g3 owns the S, g4 cleared
74 * S* p = g3.get(); // g3 still owns |p|
75 * assert(g3->x == 5); // operator-> works (if .get() != nullptr)
76 * assert((*g3).x == 5); // also operator* (again, if not cleared)
77 * Swap(g3, g4); // g4 now owns the S, g3 cleared
78 * g3.swap(g4); // g3 now owns the S, g4 cleared
79 * UniquePtr<S> g5(Move(g3)); // g5 owns the S, g3 cleared
80 * g5.reset(); // deletes the S, g5 cleared
82 * struct FreePolicy { void operator()(void* p) { free(p); } };
83 * UniquePtr<int, FreePolicy> g6(static_cast<int*>(malloc(sizeof(int))));
84 * int* ptr = g6.get();
85 * g6 = nullptr; // calls free(ptr)
87 * Now, carefully note a few things you *can't* do:
90 * b1 = new int; // BAD: can only assign another UniquePtr
91 * int* ptr = b1; // BAD: no auto-conversion to pointer, use get()
93 * UniquePtr<int> b2(b1); // BAD: can't copy a UniquePtr
94 * UniquePtr<int> b3 = b1; // BAD: can't copy-assign a UniquePtr
96 * (Note that changing a UniquePtr to store a direct |new| expression is
97 * permitted, but usually you should use MakeUnique, defined at the end of this
100 * A few miscellaneous notes:
102 * UniquePtr, when not instantiated for an array type, can be move-constructed
103 * and move-assigned, not only from itself but from "derived" UniquePtr<U, E>
104 * instantiations where U converts to T and E converts to D. If you want to use
105 * this, you're going to have to specify a deletion policy for both UniquePtr
106 * instantations, and T pretty much has to have a virtual destructor. In other
107 * words, this doesn't work:
109 * struct Base { virtual ~Base() {} };
110 * struct Derived : Base {};
112 * UniquePtr<Base> b1;
113 * // BAD: DefaultDelete<Base> and DefaultDelete<Derived> don't interconvert
114 * UniquePtr<Derived> d1(Move(b));
116 * UniquePtr<Base> b2;
117 * UniquePtr<Derived, DefaultDelete<Base>> d2(Move(b2)); // okay
119 * UniquePtr is specialized for array types. Specializing with an array type
120 * creates a smart-pointer version of that array -- not a pointer to such an
123 * UniquePtr<int[]> arr(new int[5]);
126 * What else is different? Deletion of course uses |delete[]|. An operator[]
127 * is provided. Functionality that doesn't make sense for arrays is removed.
128 * The constructors and mutating methods only accept array pointers (not T*, U*
129 * that converts to T*, or UniquePtr<U[]> or UniquePtr<U>) or |nullptr|.
131 * It's perfectly okay to return a UniquePtr from a method to assure the related
132 * resource is properly deleted. You'll need to use |Move()| when returning a
133 * local UniquePtr. Otherwise you can return |nullptr|, or you can return
136 * UniquePtr will commonly be a member of a class, with lifetime equivalent to
137 * that of that class. If you want to expose the related resource, you could
138 * expose a raw pointer via |get()|, but ownership of a raw pointer is
139 * inherently unclear. So it's better to expose a |const UniquePtr&| instead.
140 * This prohibits mutation but still allows use of |get()| when needed (but
141 * operator-> is preferred). Of course, you can only use this smart pointer as
142 * long as the enclosing class instance remains live -- no different than if you
143 * exposed the |get()| raw pointer.
145 * To pass a UniquePtr-managed resource as a pointer, use a |const UniquePtr&|
146 * argument. To specify an inout parameter (where the method may or may not
147 * take ownership of the resource, or reset it), or to specify an out parameter
148 * (where simply returning a |UniquePtr| isn't possible), use a |UniquePtr&|
149 * argument. To unconditionally transfer ownership of a UniquePtr
150 * into a method, use a |UniquePtr| argument. To conditionally transfer
151 * ownership of a resource into a method, should the method want it, use a
152 * |UniquePtr&&| argument.
154 template<typename T
, class D
>
159 typedef T ElementType
;
160 typedef D DeleterType
;
163 Pair
<Pointer
, DeleterType
> mTuple
;
165 Pointer
& ptr() { return mTuple
.first(); }
166 const Pointer
& ptr() const { return mTuple
.first(); }
168 DeleterType
& del() { return mTuple
.second(); }
169 const DeleterType
& del() const { return mTuple
.second(); }
173 * Construct a UniquePtr containing |nullptr|.
175 MOZ_CONSTEXPR
UniquePtr()
176 : mTuple(static_cast<Pointer
>(nullptr), DeleterType())
178 static_assert(!IsPointer
<D
>::value
, "must provide a deleter instance");
179 static_assert(!IsReference
<D
>::value
, "must provide a deleter instance");
183 * Construct a UniquePtr containing |aPtr|.
185 explicit UniquePtr(Pointer aPtr
)
186 : mTuple(aPtr
, DeleterType())
188 static_assert(!IsPointer
<D
>::value
, "must provide a deleter instance");
189 static_assert(!IsReference
<D
>::value
, "must provide a deleter instance");
192 UniquePtr(Pointer aPtr
,
193 typename Conditional
<IsReference
<D
>::value
,
199 // If you encounter an error with MSVC10 about RemoveReference below, along
200 // the lines that "more than one partial specialization matches the template
201 // argument list": don't use UniquePtr<T, reference to function>! Ideally
202 // you should make deletion use the same function every time, using a
205 // // BAD, won't compile with MSVC10, deleter doesn't need to be a
206 // // variable at all
207 // typedef void (&FreeSignature)(void*);
208 // UniquePtr<int, FreeSignature> ptr((int*) malloc(sizeof(int)), free);
210 // // GOOD, compiles with MSVC10, deletion behavior statically known and
212 // struct DeleteByFreeing
214 // void operator()(void* aPtr) { free(aPtr); }
217 // If deletion really, truly, must be a variable: you might be able to work
218 // around this with a deleter class that contains the function reference.
219 // But this workaround is untried and untested, because variable deletion
220 // behavior really isn't something you should use.
221 UniquePtr(Pointer aPtr
,
222 typename RemoveReference
<D
>::Type
&& aD2
)
223 : mTuple(aPtr
, Move(aD2
))
225 static_assert(!IsReference
<D
>::value
,
226 "rvalue deleter can't be stored by reference");
229 UniquePtr(UniquePtr
&& aOther
)
230 : mTuple(aOther
.release(), Forward
<DeleterType
>(aOther
.getDeleter()))
235 typename EnableIf
<IsNullPointer
<N
>::value
, int>::Type aDummy
= 0)
236 : mTuple(static_cast<Pointer
>(nullptr), DeleterType())
238 static_assert(!IsPointer
<D
>::value
, "must provide a deleter instance");
239 static_assert(!IsReference
<D
>::value
, "must provide a deleter instance");
242 template<typename U
, class E
>
243 UniquePtr(UniquePtr
<U
, E
>&& aOther
,
244 typename EnableIf
<IsConvertible
<typename UniquePtr
<U
, E
>::Pointer
,
246 !IsArray
<U
>::value
&&
247 (IsReference
<D
>::value
248 ? IsSame
<D
, E
>::value
249 : IsConvertible
<E
, D
>::value
),
250 int>::Type aDummy
= 0)
251 : mTuple(aOther
.release(), Forward
<E
>(aOther
.getDeleter()))
255 ~UniquePtr() { reset(nullptr); }
257 UniquePtr
& operator=(UniquePtr
&& aOther
)
259 reset(aOther
.release());
260 getDeleter() = Forward
<DeleterType
>(aOther
.getDeleter());
264 template<typename U
, typename E
>
265 UniquePtr
& operator=(UniquePtr
<U
, E
>&& aOther
)
267 static_assert(IsConvertible
<typename UniquePtr
<U
, E
>::Pointer
,
269 "incompatible UniquePtr pointees");
270 static_assert(!IsArray
<U
>::value
,
271 "can't assign from UniquePtr holding an array");
273 reset(aOther
.release());
274 getDeleter() = Forward
<E
>(aOther
.getDeleter());
278 UniquePtr
& operator=(NullptrT aNull
)
280 MOZ_ASSERT(aNull
== nullptr);
285 T
& operator*() const { return *get(); }
286 Pointer
operator->() const
288 MOZ_ASSERT(get(), "dereferencing a UniquePtr containing nullptr");
292 Pointer
get() const { return ptr(); }
294 DeleterType
& getDeleter() { return del(); }
295 const DeleterType
& getDeleter() const { return del(); }
298 typedef void (UniquePtr::* ConvertibleToBool
)(double, char);
299 void nonNull(double, char) {}
302 operator ConvertibleToBool() const
304 return get() != nullptr ? &UniquePtr::nonNull
: nullptr;
314 void reset(Pointer aPtr
= Pointer())
318 if (old
!= nullptr) {
323 void swap(UniquePtr
& aOther
)
325 mTuple
.swap(aOther
.mTuple
);
329 UniquePtr(const UniquePtr
& aOther
) MOZ_DELETE
; // construct using Move()!
330 void operator=(const UniquePtr
& aOther
) MOZ_DELETE
; // assign using Move()!
333 // In case you didn't read the comment by the main definition (you should!): the
334 // UniquePtr<T[]> specialization exists to manage array pointers. It deletes
335 // such pointers using delete[], it will reject construction and modification
336 // attempts using U* or U[]. Otherwise it works like the normal UniquePtr.
337 template<typename T
, class D
>
338 class UniquePtr
<T
[], D
>
342 typedef T ElementType
;
343 typedef D DeleterType
;
346 Pair
<Pointer
, DeleterType
> mTuple
;
350 * Construct a UniquePtr containing nullptr.
352 MOZ_CONSTEXPR
UniquePtr()
353 : mTuple(static_cast<Pointer
>(nullptr), DeleterType())
355 static_assert(!IsPointer
<D
>::value
, "must provide a deleter instance");
356 static_assert(!IsReference
<D
>::value
, "must provide a deleter instance");
360 * Construct a UniquePtr containing |aPtr|.
362 explicit UniquePtr(Pointer aPtr
)
363 : mTuple(aPtr
, DeleterType())
365 static_assert(!IsPointer
<D
>::value
, "must provide a deleter instance");
366 static_assert(!IsReference
<D
>::value
, "must provide a deleter instance");
370 // delete[] knows how to handle *only* an array of a single class type. For
371 // delete[] to work correctly, it must know the size of each element, the
372 // fields and base classes of each element requiring destruction, and so on.
373 // So forbid all overloads which would end up invoking delete[] on a pointer
374 // of the wrong type.
377 typename EnableIf
<IsPointer
<U
>::value
&&
378 IsConvertible
<U
, Pointer
>::value
,
379 int>::Type aDummy
= 0)
383 UniquePtr(Pointer aPtr
,
384 typename Conditional
<IsReference
<D
>::value
,
390 // If you encounter an error with MSVC10 about RemoveReference below, along
391 // the lines that "more than one partial specialization matches the template
392 // argument list": don't use UniquePtr<T[], reference to function>! See the
393 // comment by this constructor in the non-T[] specialization above.
394 UniquePtr(Pointer aPtr
,
395 typename RemoveReference
<D
>::Type
&& aD2
)
396 : mTuple(aPtr
, Move(aD2
))
398 static_assert(!IsReference
<D
>::value
,
399 "rvalue deleter can't be stored by reference");
403 // Forbidden for the same reasons as stated above.
404 template<typename U
, typename V
>
405 UniquePtr(U
&& aU
, V
&& aV
,
406 typename EnableIf
<IsPointer
<U
>::value
&&
407 IsConvertible
<U
, Pointer
>::value
,
408 int>::Type aDummy
= 0)
412 UniquePtr(UniquePtr
&& aOther
)
413 : mTuple(aOther
.release(), Forward
<DeleterType
>(aOther
.getDeleter()))
418 typename EnableIf
<IsNullPointer
<N
>::value
, int>::Type aDummy
= 0)
419 : mTuple(static_cast<Pointer
>(nullptr), DeleterType())
421 static_assert(!IsPointer
<D
>::value
, "must provide a deleter instance");
422 static_assert(!IsReference
<D
>::value
, "must provide a deleter instance");
425 ~UniquePtr() { reset(nullptr); }
427 UniquePtr
& operator=(UniquePtr
&& aOther
)
429 reset(aOther
.release());
430 getDeleter() = Forward
<DeleterType
>(aOther
.getDeleter());
434 UniquePtr
& operator=(NullptrT
)
440 T
& operator[](decltype(sizeof(int)) aIndex
) const { return get()[aIndex
]; }
441 Pointer
get() const { return mTuple
.first(); }
443 DeleterType
& getDeleter() { return mTuple
.second(); }
444 const DeleterType
& getDeleter() const { return mTuple
.second(); }
447 typedef void (UniquePtr::* ConvertibleToBool
)(double, char);
448 void nonNull(double, char) {}
451 operator ConvertibleToBool() const
453 return get() != nullptr ? &UniquePtr::nonNull
: nullptr;
458 Pointer p
= mTuple
.first();
459 mTuple
.first() = nullptr;
463 void reset(Pointer aPtr
= Pointer())
465 Pointer old
= mTuple
.first();
466 mTuple
.first() = aPtr
;
467 if (old
!= nullptr) {
468 mTuple
.second()(old
);
473 // Kill off all remaining overloads that aren't true nullptr (the overload
474 // above should handle that) or emulated nullptr (which acts like int/long
478 typename EnableIf
<!IsNullPointer
<U
>::value
&&
480 Conditional
<(sizeof(int) == sizeof(void*)),
483 int>::Type aDummy
= 0)
487 void swap(UniquePtr
& aOther
) { mTuple
.swap(aOther
.mTuple
); }
490 UniquePtr(const UniquePtr
& aOther
) MOZ_DELETE
; // construct using Move()!
491 void operator=(const UniquePtr
& aOther
) MOZ_DELETE
; // assign using Move()!
494 /** A default deletion policy using plain old operator delete. */
499 MOZ_CONSTEXPR
DefaultDelete() {}
502 DefaultDelete(const DefaultDelete
<U
>& aOther
,
503 typename EnableIf
<mozilla::IsConvertible
<U
*, T
*>::value
,
504 int>::Type aDummy
= 0)
507 void operator()(T
* aPtr
) const
509 static_assert(sizeof(T
) > 0, "T must be complete");
514 /** A default deletion policy using operator delete[]. */
516 class DefaultDelete
<T
[]>
519 MOZ_CONSTEXPR
DefaultDelete() {}
521 void operator()(T
* aPtr
) const
523 static_assert(sizeof(T
) > 0, "T must be complete");
529 void operator()(U
* aPtr
) const MOZ_DELETE
;
532 template<typename T
, class D
>
534 Swap(UniquePtr
<T
, D
>& aX
, UniquePtr
<T
, D
>& aY
)
539 template<typename T
, class D
, typename U
, class E
>
541 operator==(const UniquePtr
<T
, D
>& aX
, const UniquePtr
<U
, E
>& aY
)
543 return aX
.get() == aY
.get();
546 template<typename T
, class D
, typename U
, class E
>
548 operator!=(const UniquePtr
<T
, D
>& aX
, const UniquePtr
<U
, E
>& aY
)
550 return aX
.get() != aY
.get();
553 template<typename T
, class D
>
555 operator==(const UniquePtr
<T
, D
>& aX
, NullptrT aNull
)
557 MOZ_ASSERT(aNull
== nullptr);
561 template<typename T
, class D
>
563 operator==(NullptrT aNull
, const UniquePtr
<T
, D
>& aX
)
565 MOZ_ASSERT(aNull
== nullptr);
569 template<typename T
, class D
>
571 operator!=(const UniquePtr
<T
, D
>& aX
, NullptrT aNull
)
573 MOZ_ASSERT(aNull
== nullptr);
577 template<typename T
, class D
>
579 operator!=(NullptrT aNull
, const UniquePtr
<T
, D
>& aX
)
581 MOZ_ASSERT(aNull
== nullptr);
585 // No operator<, operator>, operator<=, operator>= for now because simplicity.
590 struct UniqueSelector
592 typedef UniquePtr
<T
> SingleObject
;
596 struct UniqueSelector
<T
[]>
598 typedef UniquePtr
<T
[]> UnknownBound
;
601 template<typename T
, decltype(sizeof(int)) N
>
602 struct UniqueSelector
<T
[N
]>
604 typedef UniquePtr
<T
[N
]> KnownBound
;
607 } // namespace detail
610 * MakeUnique is a helper function for allocating new'd objects and arrays,
611 * returning a UniquePtr containing the resulting pointer. The semantics of
612 * MakeUnique<Type>(...) are as follows.
614 * If Type is an array T[n]:
615 * Disallowed, deleted, no overload for you!
616 * If Type is an array T[]:
617 * MakeUnique<T[]>(size_t) is the only valid overload. The pointer returned
618 * is as if by |new T[n]()|, which value-initializes each element. (If T
619 * isn't a class type, this will zero each element. If T is a class type,
620 * then roughly speaking, each element will be constructed using its default
621 * constructor. See C++11 [dcl.init]p7 for the full gory details.)
622 * If Type is non-array T:
623 * The arguments passed to MakeUnique<T>(...) are forwarded into a
624 * |new T(...)| call, initializing the T as would happen if executing
625 * |T(...)|. (Note: literal nullptr must not be provided as an argument to
626 * MakeUnique, because nullptr may be emulated. See Move.h for details.)
628 * There are various benefits to using MakeUnique instead of |new| expressions.
630 * First, MakeUnique eliminates use of |new| from code entirely. If objects are
631 * only created through UniquePtr, then (assuming all explicit release() calls
632 * are safe, including transitively, and no type-safety casting funniness)
633 * correctly maintained ownership of the UniquePtr guarantees no leaks are
634 * possible. (This pays off best if a class is only ever created through a
635 * factory method on the class, using a private constructor.)
637 * Second, initializing a UniquePtr using a |new| expression requires renaming
638 * the new'd type, whereas MakeUnique in concert with the |auto| keyword names
641 * UniquePtr<char> ptr1(new char()); // repetitive
642 * auto ptr2 = MakeUnique<char>(); // shorter
644 * Of course this assumes the reader understands the operation MakeUnique
645 * performs. In the long run this is probably a reasonable assumption. In the
646 * short run you'll have to use your judgment about what readers can be expected
647 * to know, or to quickly look up.
649 * Third, a call to MakeUnique can be assigned directly to a UniquePtr. In
650 * contrast you can't assign a pointer into a UniquePtr without using the
651 * cumbersome reset().
654 * p = new char; // ERROR
655 * p.reset(new char); // works, but fugly
656 * p = MakeUnique<char>(); // preferred
658 * (And third, although not relevant to Mozilla: MakeUnique is exception-safe.
659 * An exception thrown after |new T| succeeds will leak that memory, unless the
660 * pointer is assigned to an object that will manage its ownership. UniquePtr
661 * ably serves this function.)
664 // We don't have variadic template support everywhere, so just hard-code arities
665 // 0-8 for now. If you need more arguments, feel free to add the extra
666 // overloads (and deletions for the T = E[N] case).
668 // Beware! Due to lack of true nullptr support in gcc 4.4 and 4.5, passing
669 // literal nullptr to MakeUnique will not work on some platforms. See Move.h
673 typename
detail::UniqueSelector
<T
>::SingleObject
676 return UniquePtr
<T
>(new T());
679 template<typename T
, typename A1
>
680 typename
detail::UniqueSelector
<T
>::SingleObject
683 return UniquePtr
<T
>(new T(Forward
<A1
>(aA1
)));
686 template<typename T
, typename A1
, typename A2
>
687 typename
detail::UniqueSelector
<T
>::SingleObject
688 MakeUnique(A1
&& aA1
, A2
&& aA2
)
690 return UniquePtr
<T
>(new T(Forward
<A1
>(aA1
), Forward
<A2
>(aA2
)));
693 template<typename T
, typename A1
, typename A2
, typename A3
>
694 typename
detail::UniqueSelector
<T
>::SingleObject
695 MakeUnique(A1
&& aA1
, A2
&& aA2
, A3
&& aA3
)
697 return UniquePtr
<T
>(new T(Forward
<A1
>(aA1
), Forward
<A2
>(aA2
),
701 template<typename T
, typename A1
, typename A2
, typename A3
, typename A4
>
702 typename
detail::UniqueSelector
<T
>::SingleObject
703 MakeUnique(A1
&& aA1
, A2
&& aA2
, A3
&& aA3
, A4
&& aA4
)
705 return UniquePtr
<T
>(new T(Forward
<A1
>(aA1
), Forward
<A2
>(aA2
),
706 Forward
<A3
>(aA3
), Forward
<A4
>(aA4
)));
709 template<typename T
, typename A1
, typename A2
, typename A3
, typename A4
,
711 typename
detail::UniqueSelector
<T
>::SingleObject
712 MakeUnique(A1
&& aA1
, A2
&& aA2
, A3
&& aA3
, A4
&& aA4
, A5
&& aA5
)
714 return UniquePtr
<T
>(new T(Forward
<A1
>(aA1
), Forward
<A2
>(aA2
),
715 Forward
<A3
>(aA3
), Forward
<A4
>(aA4
),
719 template<typename T
, typename A1
, typename A2
, typename A3
, typename A4
,
720 typename A5
, typename A6
>
721 typename
detail::UniqueSelector
<T
>::SingleObject
722 MakeUnique(A1
&& a1
, A2
&& a2
, A3
&& a3
, A4
&& a4
, A5
&& a5
, A6
&& a6
)
724 return UniquePtr
<T
>(new T(Forward
<A1
>(a1
), Forward
<A2
>(a2
),
725 Forward
<A3
>(a3
), Forward
<A4
>(a4
),
726 Forward
<A5
>(a5
), Forward
<A6
>(a6
)));
729 template<typename T
, typename A1
, typename A2
, typename A3
, typename A4
,
730 typename A5
, typename A6
, typename A7
>
731 typename
detail::UniqueSelector
<T
>::SingleObject
732 MakeUnique(A1
&& a1
, A2
&& a2
, A3
&& a3
, A4
&& a4
, A5
&& a5
, A6
&& a6
, A7
&& a7
)
734 return UniquePtr
<T
>(new T(Forward
<A1
>(a1
), Forward
<A2
>(a2
),
735 Forward
<A3
>(a3
), Forward
<A4
>(a4
),
736 Forward
<A5
>(a5
), Forward
<A6
>(a6
),
740 template<typename T
, typename A1
, typename A2
, typename A3
, typename A4
,
741 typename A5
, typename A6
, typename A7
, typename A8
>
742 typename
detail::UniqueSelector
<T
>::SingleObject
743 MakeUnique(A1
&& a1
, A2
&& a2
, A3
&& a3
, A4
&& a4
, A5
&& a5
, A6
&& a6
, A7
&& a7
,
746 return UniquePtr
<T
>(new T(Forward
<A1
>(a1
), Forward
<A2
>(a2
),
747 Forward
<A3
>(a3
), Forward
<A4
>(a4
),
748 Forward
<A5
>(a5
), Forward
<A6
>(a6
),
749 Forward
<A7
>(a7
), Forward
<A8
>(a8
)));
753 typename
detail::UniqueSelector
<T
>::UnknownBound
754 MakeUnique(decltype(sizeof(int)) aN
)
756 typedef typename RemoveExtent
<T
>::Type ArrayType
;
757 return UniquePtr
<T
>(new ArrayType
[aN
]());
761 typename
detail::UniqueSelector
<T
>::KnownBound
762 MakeUnique() MOZ_DELETE
;
764 template<typename T
, typename A1
>
765 typename
detail::UniqueSelector
<T
>::KnownBound
766 MakeUnique(A1
&& aA1
) MOZ_DELETE
;
768 template<typename T
, typename A1
, typename A2
>
769 typename
detail::UniqueSelector
<T
>::KnownBound
770 MakeUnique(A1
&& aA1
, A2
&& aA2
) MOZ_DELETE
;
772 template<typename T
, typename A1
, typename A2
, typename A3
>
773 typename
detail::UniqueSelector
<T
>::KnownBound
774 MakeUnique(A1
&& aA1
, A2
&& aA2
, A3
&& aA3
) MOZ_DELETE
;
776 template<typename T
, typename A1
, typename A2
, typename A3
, typename A4
>
777 typename
detail::UniqueSelector
<T
>::KnownBound
778 MakeUnique(A1
&& aA1
, A2
&& aA2
, A3
&& aA3
, A4
&& aA4
) MOZ_DELETE
;
780 template<typename T
, typename A1
, typename A2
, typename A3
, typename A4
,
782 typename
detail::UniqueSelector
<T
>::KnownBound
783 MakeUnique(A1
&& aA1
, A2
&& aA2
, A3
&& aA3
, A4
&& aA4
, A5
&& aA5
) MOZ_DELETE
;
785 template<typename T
, typename A1
, typename A2
, typename A3
, typename A4
,
786 typename A5
, typename A6
>
787 typename
detail::UniqueSelector
<T
>::KnownBound
788 MakeUnique(A1
&& a1
, A2
&& a2
, A3
&& a3
, A4
&& a4
, A5
&& a5
,
791 template<typename T
, typename A1
, typename A2
, typename A3
, typename A4
,
792 typename A5
, typename A6
, typename A7
>
793 typename
detail::UniqueSelector
<T
>::KnownBound
794 MakeUnique(A1
&& a1
, A2
&& a2
, A3
&& a3
, A4
&& a4
, A5
&& a5
, A6
&& a6
,
797 template<typename T
, typename A1
, typename A2
, typename A3
, typename A4
,
798 typename A5
, typename A6
, typename A7
, typename A8
>
799 typename
detail::UniqueSelector
<T
>::KnownBound
800 MakeUnique(A1
&& a1
, A2
&& a2
, A3
&& a3
, A4
&& a4
, A5
&& a5
, A6
&& a6
,
801 A7
&& a7
, A8
&& a8
) MOZ_DELETE
;
803 } // namespace mozilla
805 #endif /* mozilla_UniquePtr_h */