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 D, class T> D* get_deleter(const shared_ptr<T>& p); 15 16 #include <memory> 17 #include <cassert> 18 #include "../test_deleter.h" 19 20 struct A 21 { 22 static int count; 23 24 A() {++count;} 25 A(const A&) {++count;} 26 ~A() {--count;} 27 }; 28 29 int A::count = 0; 30 31 int main() 32 { 33 { 34 { 35 A* ptr = new A; 36 std::shared_ptr<A> p(ptr, test_deleter<A>(3)); 37 test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); 38 assert(test_deleter<A>::count == 1); 39 assert(test_deleter<A>::dealloc_count == 0); 40 assert(d); 41 assert(d->state() == 3); 42 } 43 assert(A::count == 0); 44 assert(test_deleter<A>::count == 0); 45 assert(test_deleter<A>::dealloc_count == 1); 46 } 47 test_deleter<A>::dealloc_count = 0; 48 { 49 { 50 std::shared_ptr<A> p(nullptr, test_deleter<A>(3)); 51 test_deleter<A>* d = std::get_deleter<test_deleter<A> >(p); 52 assert(test_deleter<A>::count == 1); 53 assert(test_deleter<A>::dealloc_count == 0); 54 assert(d); 55 assert(d->state() == 3); 56 } 57 assert(A::count == 0); 58 assert(test_deleter<A>::count == 0); 59 assert(test_deleter<A>::dealloc_count == 1); 60 } 61 test_deleter<A>::dealloc_count = 0; 62 { 63 std::shared_ptr<A> p(nullptr, test_deleter<A>(3)); 64 std::default_delete<A>* d = std::get_deleter<std::default_delete<A> >(p); 65 assert(d == 0); 66 } 67 } 68