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, class A> function(allocator_arg_t, const A&, F);
15
16 #include <functional>
17 #include <cassert>
18
19 #include "test_allocator.h"
20
21 class A
22 {
23 int data_[10];
24 public:
25 static int count;
26
A()27 A()
28 {
29 ++count;
30 for (int i = 0; i < 10; ++i)
31 data_[i] = i;
32 }
33
A(const A &)34 A(const A&) {++count;}
35
~A()36 ~A() {--count;}
37
operator ()(int i) const38 int operator()(int i) const
39 {
40 for (int j = 0; j < 10; ++j)
41 i += data_[j];
42 return i;
43 }
44
foo(int) const45 int foo(int) const {return 1;}
46 };
47
48 int A::count = 0;
49
g(int)50 int g(int) {return 0;}
51
52 class Foo {
53 public:
bar(int k)54 void bar(int k) { }
55 };
56
main()57 int main()
58 {
59 {
60 std::function<int(int)> f(std::allocator_arg, test_allocator<A>(), A());
61 assert(A::count == 1);
62 assert(f.target<A>());
63 assert(f.target<int(*)(int)>() == 0);
64 }
65 assert(A::count == 0);
66 {
67 std::function<int(int)> f(std::allocator_arg, test_allocator<int(*)(int)>(), g);
68 assert(f.target<int(*)(int)>());
69 assert(f.target<A>() == 0);
70 }
71 {
72 std::function<int(int)> f(std::allocator_arg, test_allocator<int(*)(int)>(),
73 (int (*)(int))0);
74 assert(!f);
75 assert(f.target<int(*)(int)>() == 0);
76 assert(f.target<A>() == 0);
77 }
78 {
79 std::function<int(const A*, int)> f(std::allocator_arg,
80 test_allocator<int(A::*)(int)const>(),
81 &A::foo);
82 assert(f);
83 assert(f.target<int (A::*)(int) const>() != 0);
84 }
85 {
86 Foo f;
87 std::function<void(int)> fun = std::bind(&Foo::bar, &f, std::placeholders::_1);
88 fun(10);
89 }
90 }
91