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 // UNSUPPORTED: c++98, c++03 11 12 // <memory> 13 14 // shared_ptr 15 16 // template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r); 17 18 #include <memory> 19 #include <type_traits> 20 #include <cassert> 21 22 struct B 23 { 24 static int count; 25 26 B() {++count;} 27 B(const B&) {++count;} 28 virtual ~B() {--count;} 29 }; 30 31 int B::count = 0; 32 33 struct A 34 : public B 35 { 36 static int count; 37 38 A() {++count;} 39 A(const A&) {++count;} 40 ~A() {--count;} 41 }; 42 43 int A::count = 0; 44 45 int main() 46 { 47 { 48 std::shared_ptr<A> pA(new A); 49 A* ptrA = pA.get(); 50 { 51 std::shared_ptr<B> pB(new B); 52 pB = std::move(pA); 53 assert(B::count == 1); 54 assert(A::count == 1); 55 assert(pB.use_count() == 1); 56 assert(pA.use_count() == 0); 57 assert(pA.get() == 0); 58 assert(pB.get() == ptrA); 59 } 60 assert(pA.use_count() == 0); 61 assert(B::count == 0); 62 assert(A::count == 0); 63 } 64 assert(B::count == 0); 65 assert(A::count == 0); 66 { 67 std::shared_ptr<A> pA; 68 A* ptrA = pA.get(); 69 { 70 std::shared_ptr<B> pB(new B); 71 pB = std::move(pA); 72 assert(B::count == 0); 73 assert(A::count == 0); 74 assert(pB.use_count() == 0); 75 assert(pA.use_count() == 0); 76 assert(pA.get() == 0); 77 assert(pB.get() == ptrA); 78 } 79 assert(pA.use_count() == 0); 80 assert(B::count == 0); 81 assert(A::count == 0); 82 } 83 assert(B::count == 0); 84 assert(A::count == 0); 85 { 86 std::shared_ptr<A> pA(new A); 87 A* ptrA = pA.get(); 88 { 89 std::shared_ptr<B> pB; 90 pB = std::move(pA); 91 assert(B::count == 1); 92 assert(A::count == 1); 93 assert(pB.use_count() == 1); 94 assert(pA.use_count() == 0); 95 assert(pA.get() == 0); 96 assert(pB.get() == ptrA); 97 } 98 assert(pA.use_count() == 0); 99 assert(B::count == 0); 100 assert(A::count == 0); 101 } 102 assert(B::count == 0); 103 assert(A::count == 0); 104 { 105 std::shared_ptr<A> pA; 106 A* ptrA = pA.get(); 107 { 108 std::shared_ptr<B> pB; 109 pB = std::move(pA); 110 assert(B::count == 0); 111 assert(A::count == 0); 112 assert(pB.use_count() == 0); 113 assert(pA.use_count() == 0); 114 assert(pA.get() == 0); 115 assert(pB.get() == ptrA); 116 } 117 assert(pA.use_count() == 0); 118 assert(B::count == 0); 119 assert(A::count == 0); 120 } 121 assert(B::count == 0); 122 assert(A::count == 0); 123 } 124