1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <memory>
11 
12 // unique_ptr
13 
14 // Test unique_ptr<T[]>(pointer) ctor
15 
16 // unique_ptr<T[]>(pointer) ctor shouldn't require complete type
17 
18 #include <memory>
19 #include <cassert>
20 
21 struct A;
22 
23 class Deleter
24 {
25     int state_;
26 
27     Deleter(Deleter&);
28     Deleter& operator=(Deleter&);
29 
30 public:
Deleter()31     Deleter() : state_(5) {}
32 
state() const33     int state() const {return state_;}
34 
35     void operator()(A* p);
36 };
37 
38 void check(int i);
39 
40 template <class D = std::default_delete<A[]> >
41 struct B
42 {
43     std::unique_ptr<A[], D> a_;
44     explicit B(A*);
45     ~B();
46 
getB47     A* get() const {return a_.get();}
get_deleterB48     D& get_deleter() {return a_.get_deleter();}
49 };
50 
51 A* get();
52 
main()53 int main()
54 {
55     {
56     A* p = get();
57     check(3);
58     B<> s(p);
59     assert(s.get() == p);
60     }
61     check(0);
62     {
63     A* p = get();
64     check(3);
65     B<Deleter> s(p);
66     assert(s.get() == p);
67     assert(s.get_deleter().state() == 5);
68     }
69     check(0);
70 }
71 
72 struct A
73 {
74     static int count;
AA75     A() {++count;}
AA76     A(const A&) {++count;}
~AA77     ~A() {--count;}
78 };
79 
80 int A::count = 0;
81 
get()82 A* get() {return new A[3];}
83 
operator ()(A * p)84 void Deleter::operator()(A* p) {delete [] p;}
85 
check(int i)86 void check(int i)
87 {
88     assert(A::count == i);
89 }
90 
91 template <class D>
B(A * a)92 B<D>::B(A* a) : a_(a) {}
93 
94 template <class D>
~B()95 B<D>::~B() {}
96