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 // template<class F> 15 // requires CopyConstructible<F> && Callable<F, ArgTypes..> 16 // && Convertible<Callable<F, ArgTypes...>::result_type 17 // operator=(F f); 18 19 // UNSUPPORTED: asan, msan 20 21 #include <functional> 22 #include <new> 23 #include <cstdlib> 24 #include <cassert> 25 26 int new_called = 0; 27 28 void* operator new(std::size_t s) throw(std::bad_alloc) 29 { 30 ++new_called; 31 return std::malloc(s); 32 } 33 34 void operator delete(void* p) throw() 35 { 36 --new_called; 37 std::free(p); 38 } 39 40 class A 41 { 42 int data_[10]; 43 public: 44 static int count; 45 46 A() 47 { 48 ++count; 49 for (int i = 0; i < 10; ++i) 50 data_[i] = i; 51 } 52 53 A(const A&) {++count;} 54 55 ~A() {--count;} 56 57 int operator()(int i) const 58 { 59 for (int j = 0; j < 10; ++j) 60 i += data_[j]; 61 return i; 62 } 63 64 int foo(int) const {return 1;} 65 }; 66 67 int A::count = 0; 68 69 int g(int) {return 0;} 70 71 int main() 72 { 73 assert(new_called == 0); 74 { 75 std::function<int(int)> f; 76 f = A(); 77 assert(A::count == 1); 78 assert(new_called == 1); 79 assert(f.target<A>()); 80 assert(f.target<int(*)(int)>() == 0); 81 } 82 assert(A::count == 0); 83 assert(new_called == 0); 84 { 85 std::function<int(int)> f; 86 f = g; 87 assert(new_called == 0); 88 assert(f.target<int(*)(int)>()); 89 assert(f.target<A>() == 0); 90 } 91 assert(new_called == 0); 92 { 93 std::function<int(int)> f; 94 f = (int (*)(int))0; 95 assert(!f); 96 assert(new_called == 0); 97 assert(f.target<int(*)(int)>() == 0); 98 assert(f.target<A>() == 0); 99 } 100 { 101 std::function<int(const A*, int)> f; 102 f = &A::foo; 103 assert(f); 104 assert(new_called == 0); 105 assert(f.target<int (A::*)(int) const>() != 0); 106 } 107 } 108