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 // <functional> 11 12 // class function<R(ArgTypes...)> 13 14 // function(F); 15 16 #include <functional> 17 #include <cassert> 18 19 #include "count_new.hpp" 20 21 class A 22 { 23 int data_[10]; 24 public: 25 static int count; 26 A()27 A() 28 { 29 ++count; 30 for (int i = 0; i < 10; ++i) 31 data_[i] = i; 32 } 33 A(const A &)34 A(const A&) {++count;} 35 ~A()36 ~A() {--count;} 37 operator ()(int i) const38 int operator()(int i) const 39 { 40 for (int j = 0; j < 10; ++j) 41 i += data_[j]; 42 return i; 43 } 44 foo(int) const45 int foo(int) const {return 1;} 46 }; 47 48 int A::count = 0; 49 g(int)50int g(int) {return 0;} 51 main()52int main() 53 { 54 assert(globalMemCounter.checkOutstandingNewEq(0)); 55 { 56 std::function<int(int)> f = A(); 57 assert(A::count == 1); 58 assert(globalMemCounter.checkOutstandingNewEq(1)); 59 assert(f.target<A>()); 60 assert(f.target<int(*)(int)>() == 0); 61 } 62 assert(A::count == 0); 63 assert(globalMemCounter.checkOutstandingNewEq(0)); 64 { 65 std::function<int(int)> f = g; 66 assert(globalMemCounter.checkOutstandingNewEq(0)); 67 assert(f.target<int(*)(int)>()); 68 assert(f.target<A>() == 0); 69 } 70 assert(globalMemCounter.checkOutstandingNewEq(0)); 71 { 72 std::function<int(int)> f = (int (*)(int))0; 73 assert(!f); 74 assert(globalMemCounter.checkOutstandingNewEq(0)); 75 assert(f.target<int(*)(int)>() == 0); 76 assert(f.target<A>() == 0); 77 } 78 { 79 std::function<int(const A*, int)> f = &A::foo; 80 assert(f); 81 assert(globalMemCounter.checkOutstandingNewEq(0)); 82 assert(f.target<int (A::*)(int) const>() != 0); 83 } 84 { 85 std::function<void(int)> f(&g); 86 assert(f); 87 assert(f.target<int(*)(int)>() != 0); 88 f(1); 89 } 90 { 91 std::function <void()> f(static_cast<void (*)()>(0)); 92 assert(!f); 93 } 94 } 95