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