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 only require default Deleter ctor 20 21 struct A 22 { 23 static int count; 24 A() {++count;} 25 A(const A&) {++count;} 26 ~A() {--count;} 27 }; 28 29 int A::count = 0; 30 31 class Deleter 32 { 33 int state_; 34 35 Deleter(Deleter&); 36 Deleter& operator=(Deleter&); 37 38 public: 39 Deleter() : state_(5) {} 40 41 int state() const {return state_;} 42 43 void operator()(A* p) {delete p;} 44 }; 45 46 int main() 47 { 48 { 49 A* p = new A; 50 assert(A::count == 1); 51 std::unique_ptr<A> s(p); 52 assert(s.get() == p); 53 } 54 assert(A::count == 0); 55 { 56 A* p = new A; 57 assert(A::count == 1); 58 std::unique_ptr<A, Deleter> s(p); 59 assert(s.get() == p); 60 assert(s.get_deleter().state() == 5); 61 } 62 assert(A::count == 0); 63 } 64