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 // unique_ptr 13 14 // test reset 15 16 #include <memory> 17 #include <cassert> 18 19 struct A 20 { 21 static int count; AA22 A() {++count;} AA23 A(const A&) {++count;} ~AA24 virtual ~A() {--count;} 25 }; 26 27 int A::count = 0; 28 29 struct B 30 : public A 31 { 32 static int count; BB33 B() {++count;} BB34 B(const B&) {++count;} ~BB35 virtual ~B() {--count;} 36 }; 37 38 int B::count = 0; 39 main()40int main() 41 { 42 { 43 std::unique_ptr<A> p(new A); 44 assert(A::count == 1); 45 assert(B::count == 0); 46 A* i = p.get(); 47 p.reset(new B); 48 assert(A::count == 1); 49 assert(B::count == 1); 50 } 51 assert(A::count == 0); 52 assert(B::count == 0); 53 { 54 std::unique_ptr<A> p(new B); 55 assert(A::count == 1); 56 assert(B::count == 1); 57 A* i = p.get(); 58 p.reset(new B); 59 assert(A::count == 1); 60 assert(B::count == 1); 61 } 62 assert(A::count == 0); 63 assert(B::count == 0); 64 } 65