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 // UNSUPPORTED: c++98, c++03
10 // REQUIRES: c++11 || c++14
11 
12 // <functional>
13 
14 // class function<R(ArgTypes...)>
15 
16 // template<class A> function(allocator_arg_t, const A&, function&&);
17 //
18 // This signature was removed in C++17
19 
20 #include <functional>
21 #include <memory>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 #include "min_allocator.h"
26 #include "count_new.h"
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 };
52 
53 int A::count = 0;
54 
g(int)55 int g(int) { return 0; }
56 
main(int,char **)57 int main(int, char**)
58 {
59     assert(globalMemCounter.checkOutstandingNewEq(0));
60     {
61         std::function<int(int)> f = A();
62         assert(A::count == 1);
63         assert(globalMemCounter.checkOutstandingNewEq(1));
64         assert(f.target<A>());
65         assert(f.target<int(*)(int)>() == 0);
66         std::function<int(int)> f2(std::allocator_arg, bare_allocator<A>(), std::move(f));
67         assert(A::count == 1);
68         assert(globalMemCounter.checkOutstandingNewEq(1));
69         assert(f2.target<A>());
70         assert(f2.target<int(*)(int)>() == 0);
71         assert(f.target<A>() == 0);
72         assert(f.target<int(*)(int)>() == 0);
73     }
74     assert(globalMemCounter.checkOutstandingNewEq(0));
75     {
76         // Test that moving a function constructed from a reference wrapper
77         // is done without allocating.
78         DisableAllocationGuard g;
79         using Ref = std::reference_wrapper<A>;
80         A a;
81         Ref aref(a);
82         std::function<int(int)> f(aref);
83         assert(A::count == 1);
84         assert(f.target<A>() == nullptr);
85         assert(f.target<Ref>());
86         std::function<int(int)> f2(std::allocator_arg, std::allocator<void>{},
87                                    std::move(f));
88         assert(A::count == 1);
89         assert(f2.target<A>() == nullptr);
90         assert(f2.target<Ref>());
91         assert(f.target<Ref>()); // f is unchanged because the target is small
92     }
93     {
94         // Test that moving a function constructed from a function pointer
95         // is done without allocating
96         DisableAllocationGuard guard;
97         using Ptr = int(*)(int);
98         Ptr p = g;
99         std::function<int(int)> f(p);
100         assert(f.target<A>() == nullptr);
101         assert(f.target<Ptr>());
102         std::function<int(int)> f2(std::allocator_arg, std::allocator<void>(),
103                                    std::move(f));
104         assert(f2.target<A>() == nullptr);
105         assert(f2.target<Ptr>());
106         assert(f.target<Ptr>()); // f is unchanged because the target is small
107     }
108 
109   return 0;
110 }
111