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 F>
15 //   requires CopyConstructible<F> && Callable<F, ArgTypes..>
16 //         && Convertible<Callable<F, ArgTypes...>::result_type
17 //   operator=(F f);
18 
19 #include <functional>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 #include "count_new.hpp"
24 
25 class A
26 {
27     int data_[10];
28 public:
29     static int count;
30 
A()31     A()
32     {
33         ++count;
34         for (int i = 0; i < 10; ++i)
35             data_[i] = i;
36     }
37 
A(const A &)38     A(const A&) {++count;}
39 
~A()40     ~A() {--count;}
41 
operator ()(int i) const42     int operator()(int i) const
43     {
44         for (int j = 0; j < 10; ++j)
45             i += data_[j];
46         return i;
47     }
48 
foo(int) const49     int foo(int) const {return 1;}
50 };
51 
52 int A::count = 0;
53 
g(int)54 int g(int) {return 0;}
55 
56 #if TEST_STD_VER >= 11
57 struct RValueCallable {
58     template <class ...Args>
operator ()RValueCallable59     void operator()(Args&&...) && {}
60 };
61 struct LValueCallable {
62     template <class ...Args>
operator ()LValueCallable63     void operator()(Args&&...) & {}
64 };
65 #endif
66 
main()67 int main()
68 {
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