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 // result_of<Fn(ArgTypes...)>
13
14 #include <type_traits>
15 #include <memory>
16 #include <cassert>
17 #include "test_macros.h"
18
19 struct S
20 {
21 typedef short (*FreeFunc)(long);
22 operator FreeFunc() const;
23 double operator()(char, int&);
24 double const& operator()(char, int&) const;
25 double volatile& operator()(char, int&) volatile;
26 double const volatile& operator()(char, int&) const volatile;
27 };
28
29
30 struct SD : public S { };
31
32 struct NotDerived {};
33
34 template <class Tp>
35 struct Voider {
36 typedef void type;
37 };
38
39 template <class T, class = void>
40 struct HasType : std::false_type {};
41
42 template <class T>
43 struct HasType<T, typename Voider<typename T::type>::type> : std::true_type {};
44
45 template <class T, class U>
test_result_of()46 void test_result_of()
47 {
48 #if TEST_STD_VER > 14
49 static_assert(std::is_callable<T>::value, "");
50 static_assert(std::is_callable<T, U>::value, "");
51 #endif
52 static_assert((std::is_same<typename std::result_of<T>::type, U>::value), "");
53 }
54
55 template <class T>
test_no_result()56 void test_no_result()
57 {
58 #if TEST_STD_VER >= 11
59 static_assert((!HasType<std::result_of<T> >::value), "");
60 #endif
61 #if TEST_STD_VER > 14
62 static_assert(std::is_callable<T>::value == false, "");
63 #endif
64 }
65
main()66 int main()
67 {
68 typedef NotDerived ND;
69 { // functor object
70 test_result_of<S(int), short> ();
71 test_result_of<S&(unsigned char, int&), double> ();
72 test_result_of<S const&(unsigned char, int&), double const &> ();
73 test_result_of<S volatile&(unsigned char, int&), double volatile&> ();
74 test_result_of<S const volatile&(unsigned char, int&), double const volatile&> ();
75 }
76 { // pointer to function
77 typedef bool (&RF0)();
78 typedef bool* (&RF1)(int);
79 typedef bool& (&RF2)(int, int);
80 typedef bool const& (&RF3)(int, int, int);
81 typedef bool (*PF0)();
82 typedef bool* (*PF1)(int);
83 typedef bool& (*PF2)(int, int);
84 typedef bool const& (*PF3)(int, int, int);
85 typedef bool (*&PRF0)();
86 typedef bool* (*&PRF1)(int);
87 typedef bool& (*&PRF2)(int, int);
88 typedef bool const& (*&PRF3)(int, int, int);
89 test_result_of<RF0(), bool>();
90 test_result_of<RF1(int), bool*>();
91 test_result_of<RF2(int, long), bool&>();
92 test_result_of<RF3(int, long, int), bool const&>();
93 test_result_of<PF0(), bool>();
94 test_result_of<PF1(int), bool*>();
95 test_result_of<PF2(int, long), bool&>();
96 test_result_of<PF3(int, long, int), bool const&>();
97 test_result_of<PRF0(), bool>();
98 test_result_of<PRF1(int), bool*>();
99 test_result_of<PRF2(int, long), bool&>();
100 test_result_of<PRF3(int, long, int), bool const&>();
101 }
102 { // pointer to member function
103
104 typedef int (S::*PMS0)();
105 typedef int* (S::*PMS1)(long);
106 typedef int& (S::*PMS2)(long, int);
107 test_result_of<PMS0( S), int> ();
108 test_result_of<PMS0( S&), int> ();
109 test_result_of<PMS0( S*), int> ();
110 test_result_of<PMS0( S*&), int> ();
111 test_result_of<PMS0( std::reference_wrapper<S>), int> ();
112 test_result_of<PMS0(const std::reference_wrapper<S>&), int> ();
113 test_result_of<PMS0( std::reference_wrapper<SD>), int> ();
114 test_result_of<PMS0(const std::reference_wrapper<SD>&), int> ();
115 test_result_of<PMS0(std::unique_ptr<S>), int> ();
116 test_result_of<PMS0(std::unique_ptr<SD>), int> ();
117 test_no_result<PMS0(const S&)>();
118 test_no_result<PMS0(volatile S&)>();
119 test_no_result<PMS0(const volatile S&)>();
120 test_no_result<PMS0(ND & )>();
121 test_no_result<PMS0(const ND& )>();
122 test_no_result<PMS0(std::unique_ptr<S const> )>();
123 test_no_result<PMS0(std::reference_wrapper<S const>)>();
124 test_no_result<PMS0(std::reference_wrapper<ND> )>();
125 test_no_result<PMS0(std::unique_ptr<ND> )>();
126
127 test_result_of<PMS1( S, int), int*> ();
128 test_result_of<PMS1( S&, int), int*> ();
129 test_result_of<PMS1( S*, int), int*> ();
130 test_result_of<PMS1( S*&, int), int*> ();
131 test_result_of<PMS1(std::unique_ptr<S>, int), int*> ();
132 test_result_of<PMS1(std::unique_ptr<SD>, int), int*> ();
133 test_result_of<PMS1(std::reference_wrapper<S>, int), int*> ();
134 test_result_of<PMS1(const std::reference_wrapper<S>&, int), int*> ();
135 test_result_of<PMS1(std::reference_wrapper<SD>, int), int*> ();
136 test_result_of<PMS1(const std::reference_wrapper<SD>&, int), int*> ();
137 test_no_result<PMS1(const S&, int)>();
138 test_no_result<PMS1(volatile S&, int)>();
139 test_no_result<PMS1(const volatile S&, int)>();
140 test_no_result<PMS1(ND &, int)>();
141 test_no_result<PMS1(const ND&, int)>();
142 test_no_result<PMS1(std::unique_ptr<S const>, int)>();
143 test_no_result<PMS1(std::reference_wrapper<S const>, int)>();
144 test_no_result<PMS1(std::reference_wrapper<ND>, int)>();
145 test_no_result<PMS1(std::unique_ptr<ND>, int)>();
146
147 test_result_of<PMS2( S, int, int), int&> ();
148 test_result_of<PMS2( S&, int, int), int&> ();
149 test_result_of<PMS2( S*, int, int), int&> ();
150 test_result_of<PMS2( S*&, int, int), int&> ();
151 test_result_of<PMS2(std::unique_ptr<S>, int, int), int&> ();
152 test_result_of<PMS2(std::unique_ptr<SD>, int, int), int&> ();
153 test_result_of<PMS2(std::reference_wrapper<S>, int, int), int&> ();
154 test_result_of<PMS2(const std::reference_wrapper<S>&, int, int), int&> ();
155 test_result_of<PMS2(std::reference_wrapper<SD>, int, int), int&> ();
156 test_result_of<PMS2(const std::reference_wrapper<SD>&, int, int), int&> ();
157 test_no_result<PMS2(const S&, int, int)>();
158 test_no_result<PMS2(volatile S&, int, int)>();
159 test_no_result<PMS2(const volatile S&, int, int)>();
160 test_no_result<PMS2(std::unique_ptr<S const>, int, int)>();
161 test_no_result<PMS2(std::reference_wrapper<S const>, int, int)>();
162 test_no_result<PMS2(const ND&, int, int)>();
163 test_no_result<PMS2(std::reference_wrapper<ND>, int, int)>();
164 test_no_result<PMS2(std::unique_ptr<ND>, int, int)>();
165
166 typedef int (S::*PMS0C)() const;
167 typedef int* (S::*PMS1C)(long) const;
168 typedef int& (S::*PMS2C)(long, int) const;
169 test_result_of<PMS0C( S), int> ();
170 test_result_of<PMS0C( S&), int> ();
171 test_result_of<PMS0C(const S&), int> ();
172 test_result_of<PMS0C( S*), int> ();
173 test_result_of<PMS0C(const S*), int> ();
174 test_result_of<PMS0C( S*&), int> ();
175 test_result_of<PMS0C(const S*&), int> ();
176 test_result_of<PMS0C(std::unique_ptr<S>), int> ();
177 test_result_of<PMS0C(std::unique_ptr<SD>), int> ();
178 test_result_of<PMS0C(std::reference_wrapper<S> ), int> ();
179 test_result_of<PMS0C(std::reference_wrapper<const S> ), int> ();
180 test_result_of<PMS0C(const std::reference_wrapper<S> & ), int> ();
181 test_result_of<PMS0C(const std::reference_wrapper<const S> &), int> ();
182 test_result_of<PMS0C(std::reference_wrapper<SD> ), int> ();
183 test_result_of<PMS0C(std::reference_wrapper<const SD> ), int> ();
184 test_result_of<PMS0C(const std::reference_wrapper<SD> & ), int> ();
185 test_result_of<PMS0C(const std::reference_wrapper<const SD> &), int> ();
186 test_no_result<PMS0C(volatile S&)>();
187 test_no_result<PMS0C(const volatile S&)>();
188
189 test_result_of<PMS1C( S, int), int*> ();
190 test_result_of<PMS1C( S&, int), int*> ();
191 test_result_of<PMS1C(const S&, int), int*> ();
192 test_result_of<PMS1C( S*, int), int*> ();
193 test_result_of<PMS1C(const S*, int), int*> ();
194 test_result_of<PMS1C( S*&, int), int*> ();
195 test_result_of<PMS1C(const S*&, int), int*> ();
196 test_result_of<PMS1C(std::unique_ptr<S>, int), int*> ();
197 test_no_result<PMS1C(volatile S&, int)>();
198 test_no_result<PMS1C(const volatile S&, int)>();
199
200 test_result_of<PMS2C( S, int, int), int&> ();
201 test_result_of<PMS2C( S&, int, int), int&> ();
202 test_result_of<PMS2C(const S&, int, int), int&> ();
203 test_result_of<PMS2C( S*, int, int), int&> ();
204 test_result_of<PMS2C(const S*, int, int), int&> ();
205 test_result_of<PMS2C( S*&, int, int), int&> ();
206 test_result_of<PMS2C(const S*&, int, int), int&> ();
207 test_result_of<PMS2C(std::unique_ptr<S>, int, int), int&> ();
208 test_no_result<PMS2C(volatile S&, int, int)>();
209 test_no_result<PMS2C(const volatile S&, int, int)>();
210
211 typedef int (S::*PMS0V)() volatile;
212 typedef int* (S::*PMS1V)(long) volatile;
213 typedef int& (S::*PMS2V)(long, int) volatile;
214 test_result_of<PMS0V( S), int> ();
215 test_result_of<PMS0V( S&), int> ();
216 test_result_of<PMS0V(volatile S&), int> ();
217 test_result_of<PMS0V( S*), int> ();
218 test_result_of<PMS0V(volatile S*), int> ();
219 test_result_of<PMS0V( S*&), int> ();
220 test_result_of<PMS0V(volatile S*&), int> ();
221 test_result_of<PMS0V(std::unique_ptr<S>), int> ();
222 test_no_result<PMS0V(const S&)>();
223 test_no_result<PMS0V(const volatile S&)>();
224
225 test_result_of<PMS1V( S, int), int*> ();
226 test_result_of<PMS1V( S&, int), int*> ();
227 test_result_of<PMS1V(volatile S&, int), int*> ();
228 test_result_of<PMS1V( S*, int), int*> ();
229 test_result_of<PMS1V(volatile S*, int), int*> ();
230 test_result_of<PMS1V( S*&, int), int*> ();
231 test_result_of<PMS1V(volatile S*&, int), int*> ();
232 test_result_of<PMS1V(std::unique_ptr<S>, int), int*> ();
233 test_no_result<PMS1V(const S&, int)>();
234 test_no_result<PMS1V(const volatile S&, int)>();
235
236 test_result_of<PMS2V( S, int, int), int&> ();
237 test_result_of<PMS2V( S&, int, int), int&> ();
238 test_result_of<PMS2V(volatile S&, int, int), int&> ();
239 test_result_of<PMS2V( S*, int, int), int&> ();
240 test_result_of<PMS2V(volatile S*, int, int), int&> ();
241 test_result_of<PMS2V( S*&, int, int), int&> ();
242 test_result_of<PMS2V(volatile S*&, int, int), int&> ();
243 test_result_of<PMS2V(std::unique_ptr<S>, int, int), int&> ();
244 test_no_result<PMS2V(const S&, int, int)>();
245 test_no_result<PMS2V(const volatile S&, int, int)>();
246
247 typedef int (S::*PMS0CV)() const volatile;
248 typedef int* (S::*PMS1CV)(long) const volatile;
249 typedef int& (S::*PMS2CV)(long, int) const volatile;
250 test_result_of<PMS0CV( S), int> ();
251 test_result_of<PMS0CV( S&), int> ();
252 test_result_of<PMS0CV(const S&), int> ();
253 test_result_of<PMS0CV(volatile S&), int> ();
254 test_result_of<PMS0CV(const volatile S&), int> ();
255 test_result_of<PMS0CV( S*), int> ();
256 test_result_of<PMS0CV(const S*), int> ();
257 test_result_of<PMS0CV(volatile S*), int> ();
258 test_result_of<PMS0CV(const volatile S*), int> ();
259 test_result_of<PMS0CV( S*&), int> ();
260 test_result_of<PMS0CV(const S*&), int> ();
261 test_result_of<PMS0CV(volatile S*&), int> ();
262 test_result_of<PMS0CV(const volatile S*&), int> ();
263 test_result_of<PMS0CV(std::unique_ptr<S>), int> ();
264
265 test_result_of<PMS1CV( S, int), int*> ();
266 test_result_of<PMS1CV( S&, int), int*> ();
267 test_result_of<PMS1CV(const S&, int), int*> ();
268 test_result_of<PMS1CV(volatile S&, int), int*> ();
269 test_result_of<PMS1CV(const volatile S&, int), int*> ();
270 test_result_of<PMS1CV( S*, int), int*> ();
271 test_result_of<PMS1CV(const S*, int), int*> ();
272 test_result_of<PMS1CV(volatile S*, int), int*> ();
273 test_result_of<PMS1CV(const volatile S*, int), int*> ();
274 test_result_of<PMS1CV( S*&, int), int*> ();
275 test_result_of<PMS1CV(const S*&, int), int*> ();
276 test_result_of<PMS1CV(volatile S*&, int), int*> ();
277 test_result_of<PMS1CV(const volatile S*&, int), int*> ();
278 test_result_of<PMS1CV(std::unique_ptr<S>, int), int*> ();
279
280 test_result_of<PMS2CV( S, int, int), int&> ();
281 test_result_of<PMS2CV( S&, int, int), int&> ();
282 test_result_of<PMS2CV(const S&, int, int), int&> ();
283 test_result_of<PMS2CV(volatile S&, int, int), int&> ();
284 test_result_of<PMS2CV(const volatile S&, int, int), int&> ();
285 test_result_of<PMS2CV( S*, int, int), int&> ();
286 test_result_of<PMS2CV(const S*, int, int), int&> ();
287 test_result_of<PMS2CV(volatile S*, int, int), int&> ();
288 test_result_of<PMS2CV(const volatile S*, int, int), int&> ();
289 test_result_of<PMS2CV( S*&, int, int), int&> ();
290 test_result_of<PMS2CV(const S*&, int, int), int&> ();
291 test_result_of<PMS2CV(volatile S*&, int, int), int&> ();
292 test_result_of<PMS2CV(const volatile S*&, int, int), int&> ();
293 test_result_of<PMS2CV(std::unique_ptr<S>, int, int), int&> ();
294 }
295 { // pointer to member data
296 typedef char S::*PMD;
297 test_result_of<PMD(S&), char &>();
298 test_result_of<PMD(S*), char &>();
299 test_result_of<PMD(S* const), char &>();
300 test_result_of<PMD(const S&), const char&> ();
301 test_result_of<PMD(const S*), const char&> ();
302 test_result_of<PMD(volatile S&), volatile char&> ();
303 test_result_of<PMD(volatile S*), volatile char&> ();
304 test_result_of<PMD(const volatile S&), const volatile char&> ();
305 test_result_of<PMD(const volatile S*), const volatile char&> ();
306 test_result_of<PMD(SD&), char &>();
307 test_result_of<PMD(SD const&), const char&>();
308 test_result_of<PMD(SD*), char&>();
309 test_result_of<PMD(const SD*), const char&>();
310 test_result_of<PMD(std::unique_ptr<S>), char &>();
311 test_result_of<PMD(std::unique_ptr<S const>), const char&>();
312 #if TEST_STD_VER >= 11
313 test_result_of<PMD(std::reference_wrapper<S>), char&>();
314 test_result_of<PMD(std::reference_wrapper<S const>), const char&>();
315 #endif
316 test_no_result<PMD(ND&)>();
317 }
318 }
319