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 // weak_ptr 13 14 // template<class Y> weak_ptr(const weak_ptr<Y>& r); 15 // template<class Y> weak_ptr(weak_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 struct C 45 { 46 static int count; 47 48 C() {++count;} 49 C(const C&) {++count;} 50 virtual ~C() {--count;} 51 }; 52 53 int C::count = 0; 54 55 template <class T> 56 std::weak_ptr<T> source (std::shared_ptr<T> p) { return std::weak_ptr<T>(p); } 57 58 int main() 59 { 60 static_assert(( std::is_convertible<std::weak_ptr<A>, std::weak_ptr<B> >::value), ""); 61 static_assert((!std::is_convertible<std::weak_ptr<B>, std::weak_ptr<A> >::value), ""); 62 static_assert((!std::is_convertible<std::weak_ptr<A>, std::weak_ptr<C> >::value), ""); 63 { 64 const std::weak_ptr<A> pA(std::shared_ptr<A>(new A)); 65 assert(pA.use_count() == 0); 66 assert(B::count == 0); 67 assert(A::count == 0); 68 { 69 std::weak_ptr<B> pB(pA); 70 assert(B::count == 0); 71 assert(A::count == 0); 72 assert(pB.use_count() == 0); 73 assert(pA.use_count() == 0); 74 } 75 assert(pA.use_count() == 0); 76 assert(B::count == 0); 77 assert(A::count == 0); 78 } 79 assert(B::count == 0); 80 assert(A::count == 0); 81 { 82 std::weak_ptr<A> pA; 83 assert(pA.use_count() == 0); 84 assert(B::count == 0); 85 assert(A::count == 0); 86 { 87 std::weak_ptr<B> pB(pA); 88 assert(B::count == 0); 89 assert(A::count == 0); 90 assert(pB.use_count() == 0); 91 assert(pA.use_count() == 0); 92 } 93 assert(pA.use_count() == 0); 94 assert(B::count == 0); 95 assert(A::count == 0); 96 } 97 assert(B::count == 0); 98 assert(A::count == 0); 99 100 { 101 std::shared_ptr<A> ps(new A); 102 std::weak_ptr<A> pA = source(ps); 103 std::weak_ptr<B> pB(std::move(pA)); 104 assert(pB.use_count() == 1); 105 } 106 assert(B::count == 0); 107 assert(A::count == 0); 108 } 109