1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <functional>
11 
12 // class function<R(ArgTypes...)>
13 
14 // template<class A> function(allocator_arg_t, const A&, function&&);
15 
16 #include <functional>
17 #include <cassert>
18 
19 #include "min_allocator.h"
20 #include "count_new.hpp"
21 
22 class A
23 {
24     int data_[10];
25 public:
26     static int count;
27 
A()28     A()
29     {
30         ++count;
31         for (int i = 0; i < 10; ++i)
32             data_[i] = i;
33     }
34 
A(const A &)35     A(const A&) {++count;}
36 
~A()37     ~A() {--count;}
38 
operator ()(int i) const39     int operator()(int i) const
40     {
41         for (int j = 0; j < 10; ++j)
42             i += data_[j];
43         return i;
44     }
45 };
46 
47 int A::count = 0;
48 
main()49 int main()
50 {
51 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
52     assert(globalMemCounter.checkOutstandingNewEq(0));
53     {
54     std::function<int(int)> f = A();
55     assert(A::count == 1);
56     assert(globalMemCounter.checkOutstandingNewEq(1));
57     assert(f.target<A>());
58     assert(f.target<int(*)(int)>() == 0);
59     std::function<int(int)> f2(std::allocator_arg, bare_allocator<A>(), std::move(f));
60     assert(A::count == 1);
61     assert(globalMemCounter.checkOutstandingNewEq(1));
62     assert(f2.target<A>());
63     assert(f2.target<int(*)(int)>() == 0);
64     assert(f.target<A>() == 0);
65     assert(f.target<int(*)(int)>() == 0);
66     }
67 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
68 }
69