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 // UNSUPPORTED: c++98, c++03 11 12 // <functional> 13 14 // class function<R(ArgTypes...)> 15 16 // template<class A> function(allocator_arg_t, const A&, function&&); 17 18 #include <functional> 19 #include <memory> 20 #include <cassert> 21 22 #include "test_macros.h" 23 #include "min_allocator.h" 24 #include "count_new.hpp" 25 26 class A 27 { 28 int data_[10]; 29 public: 30 static int count; 31 32 A() 33 { 34 ++count; 35 for (int i = 0; i < 10; ++i) 36 data_[i] = i; 37 } 38 39 A(const A&) {++count;} 40 41 ~A() {--count;} 42 43 int operator()(int i) const 44 { 45 for (int j = 0; j < 10; ++j) 46 i += data_[j]; 47 return i; 48 } 49 }; 50 51 int A::count = 0; 52 53 int g(int) { return 0; } 54 55 int main() 56 { 57 assert(globalMemCounter.checkOutstandingNewEq(0)); 58 { 59 std::function<int(int)> f = A(); 60 assert(A::count == 1); 61 assert(globalMemCounter.checkOutstandingNewEq(1)); 62 assert(f.target<A>()); 63 assert(f.target<int(*)(int)>() == 0); 64 std::function<int(int)> f2(std::allocator_arg, bare_allocator<A>(), std::move(f)); 65 assert(A::count == 1); 66 assert(globalMemCounter.checkOutstandingNewEq(1)); 67 assert(f2.target<A>()); 68 assert(f2.target<int(*)(int)>() == 0); 69 assert(f.target<A>() == 0); 70 assert(f.target<int(*)(int)>() == 0); 71 } 72 assert(globalMemCounter.checkOutstandingNewEq(0)); 73 { 74 // Test that moving a function constructed from a reference wrapper 75 // is done without allocating. 76 DisableAllocationGuard g; 77 using Ref = std::reference_wrapper<A>; 78 A a; 79 Ref aref(a); 80 std::function<int(int)> f(aref); 81 assert(A::count == 1); 82 assert(f.target<A>() == nullptr); 83 assert(f.target<Ref>()); 84 std::function<int(int)> f2(std::allocator_arg, std::allocator<void>{}, 85 std::move(f)); 86 assert(A::count == 1); 87 assert(f2.target<A>() == nullptr); 88 assert(f2.target<Ref>()); 89 assert(f.target<Ref>()); // f is unchanged because the target is small 90 } 91 { 92 // Test that moving a function constructed from a function pointer 93 // is done without allocating 94 DisableAllocationGuard guard; 95 using Ptr = int(*)(int); 96 Ptr p = g; 97 std::function<int(int)> f(p); 98 assert(f.target<A>() == nullptr); 99 assert(f.target<Ptr>()); 100 std::function<int(int)> f2(std::allocator_arg, std::allocator<void>(), 101 std::move(f)); 102 assert(f2.target<A>() == nullptr); 103 assert(f2.target<Ptr>()); 104 assert(f.target<Ptr>()); // f is unchanged because the target is small 105 } 106 } 107