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> void reset(Y* 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 virtual ~B() {--count;} 26 }; 27 28 int B::count = 0; 29 30 struct A 31 : public B 32 { 33 static int count; 34 35 A() {++count;} 36 A(const A&) {++count;} 37 ~A() {--count;} 38 }; 39 40 int A::count = 0; 41 42 int main() 43 { 44 { 45 std::shared_ptr<B> p(new B); 46 A* ptr = new A; 47 p.reset(ptr); 48 assert(A::count == 1); 49 assert(B::count == 1); 50 assert(p.use_count() == 1); 51 assert(p.get() == ptr); 52 } 53 assert(A::count == 0); 54 { 55 std::shared_ptr<B> p; 56 A* ptr = new A; 57 p.reset(ptr); 58 assert(A::count == 1); 59 assert(B::count == 1); 60 assert(p.use_count() == 1); 61 assert(p.get() == ptr); 62 } 63 assert(A::count == 0); 64 } 65