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 converting move ctor
15 
16 #include <memory>
17 #include <cassert>
18 
19 // test converting move ctor.  Should only require a MoveConstructible deleter, or if
20 //    deleter is a reference, not even that.
21 // Explicit version
22 
23 struct A
24 {
25     static int count;
AA26     A() {++count;}
AA27     A(const A&) {++count;}
~AA28     virtual ~A() {--count;}
29 };
30 
31 int A::count = 0;
32 
33 struct B
34     : public A
35 {
36     static int count;
BB37     B() {++count;}
BB38     B(const B&) {++count;}
~BB39     virtual ~B() {--count;}
40 };
41 
42 int B::count = 0;
43 
44 template <class T>
45 class CDeleter
46 {
47     int state_;
48 
49     CDeleter(CDeleter&);
50     CDeleter& operator=(CDeleter&);
51 public:
52 
CDeleter()53     CDeleter() : state_(5) {}
54 
state() const55     int state() const {return state_;}
set_state(int s)56     void set_state(int s) {state_ = s;}
57 
operator ()(T * p)58     void operator()(T* p) {delete p;}
59 };
60 
main()61 int main()
62 {
63     {
64     CDeleter<A> d;
65     std::unique_ptr<B, CDeleter<A>&> s(new B, d);
66     A* p = s.get();
67     std::unique_ptr<A, CDeleter<A>&> s2 = std::move(s);
68     assert(s2.get() == p);
69     assert(s.get() == 0);
70     assert(A::count == 1);
71     assert(B::count == 1);
72     d.set_state(6);
73     assert(s2.get_deleter().state() == d.state());
74     assert(s.get_deleter().state() ==  d.state());
75     }
76     assert(A::count == 0);
77     assert(B::count == 0);
78 }
79