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=(const function& f);
15 
16 #include <functional>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "count_new.hpp"
21 
22 class A
23 {
24     int data_[10];
25 public:
26     static int count;
27 
28     A()
29     {
30         ++count;
31         for (int i = 0; i < 10; ++i)
32             data_[i] = i;
33     }
34 
35     A(const A&) {++count;}
36 
37     ~A() {--count;}
38 
39     int operator()(int i) const
40     {
41         for (int j = 0; j < 10; ++j)
42             i += data_[j];
43         return i;
44     }
45 };
46 
47 int A::count = 0;
48 
49 int g(int) {return 0;}
50 
51 int main()
52 {
53     assert(globalMemCounter.checkOutstandingNewEq(0));
54     {
55     std::function<int(int)> f = A();
56     assert(A::count == 1);
57     assert(globalMemCounter.checkOutstandingNewEq(1));
58     assert(f.target<A>());
59     assert(f.target<int(*)(int)>() == 0);
60     std::function<int(int)> f2;
61     f2 = f;
62     assert(A::count == 2);
63     assert(globalMemCounter.checkOutstandingNewEq(2));
64     assert(f2.target<A>());
65     assert(f2.target<int(*)(int)>() == 0);
66     }
67     assert(A::count == 0);
68     assert(globalMemCounter.checkOutstandingNewEq(0));
69     {
70     std::function<int(int)> f = g;
71     assert(globalMemCounter.checkOutstandingNewEq(0));
72     assert(f.target<int(*)(int)>());
73     assert(f.target<A>() == 0);
74     std::function<int(int)> f2;
75     f2 = f;
76     assert(globalMemCounter.checkOutstandingNewEq(0));
77     assert(f2.target<int(*)(int)>());
78     assert(f2.target<A>() == 0);
79     }
80     assert(globalMemCounter.checkOutstandingNewEq(0));
81     {
82     std::function<int(int)> f;
83     assert(globalMemCounter.checkOutstandingNewEq(0));
84     assert(f.target<int(*)(int)>() == 0);
85     assert(f.target<A>() == 0);
86     std::function<int(int)> f2;
87     f2 = f;
88     assert(globalMemCounter.checkOutstandingNewEq(0));
89     assert(f2.target<int(*)(int)>() == 0);
90     assert(f2.target<A>() == 0);
91     }
92 #if TEST_STD_VER >= 11
93     assert(globalMemCounter.checkOutstandingNewEq(0));
94     {
95     std::function<int(int)> f = A();
96     assert(A::count == 1);
97     assert(globalMemCounter.checkOutstandingNewEq(1));
98     assert(f.target<A>());
99     assert(f.target<int(*)(int)>() == 0);
100     std::function<int(int)> f2;
101     f2 = std::move(f);
102     assert(A::count == 1);
103     assert(globalMemCounter.checkOutstandingNewEq(1));
104     assert(f2.target<A>());
105     assert(f2.target<int(*)(int)>() == 0);
106     assert(f.target<A>() == 0);
107     assert(f.target<int(*)(int)>() == 0);
108     }
109 #endif
110 }
111