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 // template<class F>
15 //   requires CopyConstructible<F> && Callable<F, ArgTypes..>
16 //         && Convertible<Callable<F, ArgTypes...>::result_type
17 //   operator=(F f);
18 
19 #include <functional>
20 #include <cassert>
21 
22 #include "count_new.hpp"
23 
24 class A
25 {
26     int data_[10];
27 public:
28     static int count;
29 
A()30     A()
31     {
32         ++count;
33         for (int i = 0; i < 10; ++i)
34             data_[i] = i;
35     }
36 
A(const A &)37     A(const A&) {++count;}
38 
~A()39     ~A() {--count;}
40 
operator ()(int i) const41     int operator()(int i) const
42     {
43         for (int j = 0; j < 10; ++j)
44             i += data_[j];
45         return i;
46     }
47 
foo(int) const48     int foo(int) const {return 1;}
49 };
50 
51 int A::count = 0;
52 
g(int)53 int g(int) {return 0;}
54 
main()55 int main()
56 {
57     assert(globalMemCounter.checkOutstandingNewEq(0));
58     {
59     std::function<int(int)> f;
60     f = A();
61     assert(A::count == 1);
62     assert(globalMemCounter.checkOutstandingNewEq(1));
63     assert(f.target<A>());
64     assert(f.target<int(*)(int)>() == 0);
65     }
66     assert(A::count == 0);
67     assert(globalMemCounter.checkOutstandingNewEq(0));
68     {
69     std::function<int(int)> f;
70     f = g;
71     assert(globalMemCounter.checkOutstandingNewEq(0));
72     assert(f.target<int(*)(int)>());
73     assert(f.target<A>() == 0);
74     }
75     assert(globalMemCounter.checkOutstandingNewEq(0));
76     {
77     std::function<int(int)> f;
78     f = (int (*)(int))0;
79     assert(!f);
80     assert(globalMemCounter.checkOutstandingNewEq(0));
81     assert(f.target<int(*)(int)>() == 0);
82     assert(f.target<A>() == 0);
83     }
84     {
85     std::function<int(const A*, int)> f;
86     f = &A::foo;
87     assert(f);
88     assert(globalMemCounter.checkOutstandingNewEq(0));
89     assert(f.target<int (A::*)(int) const>() != 0);
90     }
91     {
92     std::function<void(int)> f;
93     f = &g;
94     assert(f);
95     assert(f.target<int(*)(int)>() != 0);
96     f(1);
97     }
98 }
99