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