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