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.hpp"
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     assert(globalMemCounter.checkOutstandingNewEq(0));
69     {
70     std::function<int(int)> f;
71     f = A();
72     assert(A::count == 1);
73     assert(globalMemCounter.checkOutstandingNewEq(1));
74     assert(f.target<A>());
75     assert(f.target<int(*)(int)>() == 0);
76     }
77     assert(A::count == 0);
78     assert(globalMemCounter.checkOutstandingNewEq(0));
79     {
80     std::function<int(int)> f;
81     f = g;
82     assert(globalMemCounter.checkOutstandingNewEq(0));
83     assert(f.target<int(*)(int)>());
84     assert(f.target<A>() == 0);
85     }
86     assert(globalMemCounter.checkOutstandingNewEq(0));
87     {
88     std::function<int(int)> f;
89     f = (int (*)(int))0;
90     assert(!f);
91     assert(globalMemCounter.checkOutstandingNewEq(0));
92     assert(f.target<int(*)(int)>() == 0);
93     assert(f.target<A>() == 0);
94     }
95     {
96     std::function<int(const A*, int)> f;
97     f = &A::foo;
98     assert(f);
99     assert(globalMemCounter.checkOutstandingNewEq(0));
100     assert(f.target<int (A::*)(int) const>() != 0);
101     }
102     {
103     std::function<void(int)> f;
104     f = &g;
105     assert(f);
106     assert(f.target<int(*)(int)>() != 0);
107     f(1);
108     }
109 #if TEST_STD_VER >= 11
110     {
111         using Fn = std::function<void(int, int, int)>;
112         static_assert(std::is_assignable<Fn&, LValueCallable&>::value, "");
113         static_assert(std::is_assignable<Fn&, LValueCallable>::value, "");
114         static_assert(!std::is_assignable<Fn&, RValueCallable&>::value, "");
115         static_assert(!std::is_assignable<Fn&, RValueCallable>::value, "");
116     }
117 #endif
118 
119   return 0;
120 }
121