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 "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)48 int g(int) {return 0;}
49 
main()50 int 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     assert(f.target<int(*)(int)>() == 0);
59     std::function<int(int)> f2;
60     f2 = f;
61     assert(A::count == 2);
62     assert(globalMemCounter.checkOutstandingNewEq(2));
63     assert(f2.target<A>());
64     assert(f2.target<int(*)(int)>() == 0);
65     }
66     assert(A::count == 0);
67     assert(globalMemCounter.checkOutstandingNewEq(0));
68     {
69     std::function<int(int)> f = g;
70     assert(globalMemCounter.checkOutstandingNewEq(0));
71     assert(f.target<int(*)(int)>());
72     assert(f.target<A>() == 0);
73     std::function<int(int)> f2;
74     f2 = f;
75     assert(globalMemCounter.checkOutstandingNewEq(0));
76     assert(f2.target<int(*)(int)>());
77     assert(f2.target<A>() == 0);
78     }
79     assert(globalMemCounter.checkOutstandingNewEq(0));
80     {
81     std::function<int(int)> f;
82     assert(globalMemCounter.checkOutstandingNewEq(0));
83     assert(f.target<int(*)(int)>() == 0);
84     assert(f.target<A>() == 0);
85     std::function<int(int)> f2;
86     f2 = f;
87     assert(globalMemCounter.checkOutstandingNewEq(0));
88     assert(f2.target<int(*)(int)>() == 0);
89     assert(f2.target<A>() == 0);
90     }
91 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
92     assert(globalMemCounter.checkOutstandingNewEq(0));
93     {
94     std::function<int(int)> f = A();
95     assert(A::count == 1);
96     assert(globalMemCounter.checkOutstandingNewEq(1));
97     assert(f.target<A>());
98     assert(f.target<int(*)(int)>() == 0);
99     std::function<int(int)> f2;
100     f2 = std::move(f);
101     assert(A::count == 1);
102     assert(globalMemCounter.checkOutstandingNewEq(1));
103     assert(f2.target<A>());
104     assert(f2.target<int(*)(int)>() == 0);
105     assert(f.target<A>() == 0);
106     assert(f.target<int(*)(int)>() == 0);
107     }
108 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
109 }
110