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<class F>
14 //   requires CopyConstructible<F> && Callable<F, ArgTypes..>
15 //         && Convertible<Callable<F, ArgTypes...>::result_type
16 //   operator=(F f);
17 
18 #include <functional>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "count_new.h"
23 
24 class A
25 {
26     int data_[10];
27 public:
28     static int count;
29 
30     A()
31     {
32         ++count;
33         for (int i = 0; i < 10; ++i)
34             data_[i] = i;
35     }
36 
37     A(const A&) {++count;}
38 
39     ~A() {--count;}
40 
41     int operator()(int i) const
42     {
43         for (int j = 0; j < 10; ++j)
44             i += data_[j];
45         return i;
46     }
47 
48     int foo(int) const {return 1;}
49 };
50 
51 int A::count = 0;
52 
53 int g(int) {return 0;}
54 
55 #if TEST_STD_VER >= 11
56 struct RValueCallable {
57     template <class ...Args>
58     void operator()(Args&&...) && {}
59 };
60 struct LValueCallable {
61     template <class ...Args>
62     void operator()(Args&&...) & {}
63 };
64 #endif
65 
66 int main(int, char**)
67 {
68     globalMemCounter.reset();
69     assert(globalMemCounter.checkOutstandingNewEq(0));
70     {
71     std::function<int(int)> f;
72     f = A();
73     assert(A::count == 1);
74     assert(globalMemCounter.checkOutstandingNewEq(1));
75     assert(f.target<A>());
76     assert(f.target<int(*)(int)>() == 0);
77     }
78     assert(A::count == 0);
79     assert(globalMemCounter.checkOutstandingNewEq(0));
80     {
81     std::function<int(int)> f;
82     f = g;
83     assert(globalMemCounter.checkOutstandingNewEq(0));
84     assert(f.target<int(*)(int)>());
85     assert(f.target<A>() == 0);
86     }
87     assert(globalMemCounter.checkOutstandingNewEq(0));
88     {
89     std::function<int(int)> f;
90     f = (int (*)(int))0;
91     assert(!f);
92     assert(globalMemCounter.checkOutstandingNewEq(0));
93     assert(f.target<int(*)(int)>() == 0);
94     assert(f.target<A>() == 0);
95     }
96     {
97     std::function<int(const A*, int)> f;
98     f = &A::foo;
99     assert(f);
100     assert(globalMemCounter.checkOutstandingNewEq(0));
101     assert(f.target<int (A::*)(int) const>() != 0);
102     }
103     {
104     std::function<void(int)> f;
105     f = &g;
106     assert(f);
107     assert(f.target<int(*)(int)>() != 0);
108     f(1);
109     }
110 #if TEST_STD_VER >= 11
111     {
112         using Fn = std::function<void(int, int, int)>;
113         static_assert(std::is_assignable<Fn&, LValueCallable&>::value, "");
114         static_assert(std::is_assignable<Fn&, LValueCallable>::value, "");
115         static_assert(!std::is_assignable<Fn&, RValueCallable&>::value, "");
116         static_assert(!std::is_assignable<Fn&, RValueCallable>::value, "");
117     }
118 #endif
119 
120   return 0;
121 }
122