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 14 // template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p); 15 16 #include <memory> 17 #include <cassert> 18 19 struct B 20 { 21 static int count; 22 23 B() {++count;} 24 B(const B&) {++count;} 25 ~B() {--count;} 26 }; 27 28 int B::count = 0; 29 30 struct A 31 { 32 static int count; 33 34 A() {++count;} 35 A(const A&) {++count;} 36 ~A() {--count;} 37 }; 38 39 int A::count = 0; 40 41 int main() 42 { 43 { 44 std::shared_ptr<A> pA(new A); 45 assert(pA.use_count() == 1); 46 { 47 B b; 48 std::shared_ptr<B> pB(pA, &b); 49 assert(A::count == 1); 50 assert(B::count == 1); 51 assert(pA.use_count() == 2); 52 assert(pB.use_count() == 2); 53 assert(pB.get() == &b); 54 } 55 assert(pA.use_count() == 1); 56 assert(A::count == 1); 57 assert(B::count == 0); 58 } 59 assert(A::count == 0); 60 assert(B::count == 0); 61 } 62