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 #include <functional>
23 #include <new>
24 #include <cstdlib>
25 #include <cassert>
26 
27 #include "test_macros.h"
28 
29 class A
30 {
31     int data_[10];
32 public:
33     static int count;
34 
35     A()
36     {
37         ++count;
38         for (int i = 0; i < 10; ++i)
39             data_[i] = i;
40     }
41 
42     A(const A&) {++count;}
43 
44     ~A() {--count;}
45 
46     int operator()(int i) const
47     {
48         for (int j = 0; j < 10; ++j)
49             i += data_[j];
50         return i;
51     }
52 
53     int foo(int) const {return 1;}
54 };
55 
56 int A::count = 0;
57 
58 int g(int) {return 0;}
59 
60 int main(int, char**)
61 {
62     {
63     std::function<int(int)> f = A();
64     assert(A::count == 1);
65     assert(f.target<A>());
66     assert(f.target<int(*)(int)>() == 0);
67     assert(f.target<int>() == nullptr);
68     }
69     assert(A::count == 0);
70     {
71     std::function<int(int)> f = g;
72     assert(A::count == 0);
73     assert(f.target<int(*)(int)>());
74     assert(f.target<A>() == 0);
75     assert(f.target<int>() == nullptr);
76     }
77     assert(A::count == 0);
78     {
79     const std::function<int(int)> f = A();
80     assert(A::count == 1);
81     assert(f.target<A>());
82     assert(f.target<int(*)(int)>() == 0);
83     assert(f.target<int>() == nullptr);
84     }
85     assert(A::count == 0);
86     {
87     const std::function<int(int)> f = g;
88     assert(A::count == 0);
89     assert(f.target<int(*)(int)>());
90     assert(f.target<A>() == 0);
91     assert(f.target<int>() == nullptr);
92     }
93     assert(A::count == 0);
94 
95   return 0;
96 }
97