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& operator=(nullptr_t); 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 }; 45 46 int A::count = 0; 47 g(int)48int g(int) {return 0;} 49 main()50int main() 51 { 52 assert(globalMemCounter.checkOutstandingNewEq(0)); 53 { 54 std::function<int(int)> f = A(); 55 assert(A::count == 1); 56 assert(globalMemCounter.checkOutstandingNewEq(1)); 57 assert(f.target<A>()); 58 f = nullptr; 59 assert(A::count == 0); 60 assert(globalMemCounter.checkOutstandingNewEq(0)); 61 assert(f.target<A>() == 0); 62 } 63 { 64 std::function<int(int)> f = g; 65 assert(globalMemCounter.checkOutstandingNewEq(0)); 66 assert(f.target<int(*)(int)>()); 67 assert(f.target<A>() == 0); 68 f = nullptr; 69 assert(globalMemCounter.checkOutstandingNewEq(0)); 70 assert(f.target<int(*)(int)>() == 0); 71 } 72 } 73