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 unique_ptr(pointer) ctor 15 16 #include <memory> 17 #include <cassert> 18 19 // unique_ptr(pointer) ctor should work with derived pointers 20 21 struct A 22 { 23 static int count; 24 A() {++count;} 25 A(const A&) {++count;} 26 virtual ~A() {--count;} 27 }; 28 29 int A::count = 0; 30 31 struct B 32 : public A 33 { 34 static int count; 35 B() {++count;} 36 B(const B&) {++count;} 37 virtual ~B() {--count;} 38 }; 39 40 int B::count = 0; 41 42 class Deleter 43 { 44 int state_; 45 46 Deleter(Deleter&); 47 Deleter& operator=(Deleter&); 48 49 public: 50 Deleter() : state_(5) {} 51 52 int state() const {return state_;} 53 54 void operator()(A* p) {delete p;} 55 }; 56 57 int main() 58 { 59 { 60 B* p = new B; 61 assert(A::count == 1); 62 assert(B::count == 1); 63 std::unique_ptr<A> s(p); 64 assert(s.get() == p); 65 } 66 assert(A::count == 0); 67 assert(B::count == 0); 68 { 69 B* p = new B; 70 assert(A::count == 1); 71 assert(B::count == 1); 72 std::unique_ptr<A, Deleter> s(p); 73 assert(s.get() == p); 74 assert(s.get_deleter().state() == 5); 75 } 76 assert(A::count == 0); 77 assert(B::count == 0); 78 } 79