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<typename T>
14 // requires Callable<T, ArgTypes...> && Convertible<Callable<T, ArgTypes...>::result_type, R>
15 // T*
16 // target();
17 // template<typename T>
18 // requires Callable<T, ArgTypes...> && Convertible<Callable<T, ArgTypes...>::result_type, R>
19 // const T*
20 // target() const;
21
22 // This test runs in C++03, but we have deprecated using std::function in C++03.
23 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS -D_LIBCPP_ENABLE_CXX03_FUNCTION
24
25 // UNSUPPORTED: no-rtti
26
27 #include <functional>
28 #include <new>
29 #include <cstdlib>
30 #include <cassert>
31
32 #include "test_macros.h"
33
34 class A
35 {
36 int data_[10];
37 public:
38 static int count;
39
A()40 A()
41 {
42 ++count;
43 for (int i = 0; i < 10; ++i)
44 data_[i] = i;
45 }
46
A(const A &)47 A(const A&) {++count;}
48
~A()49 ~A() {--count;}
50
operator ()(int i) const51 int operator()(int i) const
52 {
53 for (int j = 0; j < 10; ++j)
54 i += data_[j];
55 return i;
56 }
57
foo(int) const58 int foo(int) const {return 1;}
59 };
60
61 int A::count = 0;
62
g(int)63 int g(int) {return 0;}
64
main(int,char **)65 int main(int, char**)
66 {
67 {
68 std::function<int(int)> f = A();
69 assert(A::count == 1);
70 assert(f.target<A>());
71 assert(f.target<int(*)(int)>() == 0);
72 assert(f.target<int>() == nullptr);
73 }
74 assert(A::count == 0);
75 {
76 std::function<int(int)> f = g;
77 assert(A::count == 0);
78 assert(f.target<int(*)(int)>());
79 assert(f.target<A>() == 0);
80 assert(f.target<int>() == nullptr);
81 }
82 assert(A::count == 0);
83 {
84 const std::function<int(int)> f = A();
85 assert(A::count == 1);
86 assert(f.target<A>());
87 assert(f.target<int(*)(int)>() == 0);
88 assert(f.target<int>() == nullptr);
89 }
90 assert(A::count == 0);
91 {
92 const std::function<int(int)> f = g;
93 assert(A::count == 0);
94 assert(f.target<int(*)(int)>());
95 assert(f.target<A>() == 0);
96 assert(f.target<int>() == nullptr);
97 }
98 assert(A::count == 0);
99
100 return 0;
101 }
102