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