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