1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <functional>
10 
11 // class function<R(ArgTypes...)>
12 
13 // template<typename T>
14 //   requires Callable<T, ArgTypes...> && Convertible<Callable<T, ArgTypes...>::result_type, R>
15 //   T*
16 //   target();
17 // template<typename T>
18 //   requires Callable<T, ArgTypes...> && Convertible<Callable<T, ArgTypes...>::result_type, R>
19 //   const T*
20 //   target() const;
21 
22 // This test runs in C++03, but we have deprecated using std::function in C++03.
23 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
24 
25 #include <functional>
26 #include <new>
27 #include <cstdlib>
28 #include <cassert>
29 
30 #include "test_macros.h"
31 
32 class A
33 {
34     int data_[10];
35 public:
36     static int count;
37 
38     A()
39     {
40         ++count;
41         for (int i = 0; i < 10; ++i)
42             data_[i] = i;
43     }
44 
45     A(const A&) {++count;}
46 
47     ~A() {--count;}
48 
49     int operator()(int i) const
50     {
51         for (int j = 0; j < 10; ++j)
52             i += data_[j];
53         return i;
54     }
55 
56     int foo(int) const {return 1;}
57 };
58 
59 int A::count = 0;
60 
61 int g(int) {return 0;}
62 
63 int main(int, char**)
64 {
65     {
66     std::function<int(int)> f = A();
67     assert(A::count == 1);
68     assert(f.target<A>());
69     assert(f.target<int(*)(int)>() == 0);
70     assert(f.target<int>() == nullptr);
71     }
72     assert(A::count == 0);
73     {
74     std::function<int(int)> f = g;
75     assert(A::count == 0);
76     assert(f.target<int(*)(int)>());
77     assert(f.target<A>() == 0);
78     assert(f.target<int>() == nullptr);
79     }
80     assert(A::count == 0);
81     {
82     const std::function<int(int)> f = A();
83     assert(A::count == 1);
84     assert(f.target<A>());
85     assert(f.target<int(*)(int)>() == 0);
86     assert(f.target<int>() == nullptr);
87     }
88     assert(A::count == 0);
89     {
90     const std::function<int(int)> f = g;
91     assert(A::count == 0);
92     assert(f.target<int(*)(int)>());
93     assert(f.target<A>() == 0);
94     assert(f.target<int>() == nullptr);
95     }
96     assert(A::count == 0);
97 
98   return 0;
99 }
100