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 #if TEST_STD_VER > 14
46 template <typename T, typename U>
47 struct test_invoke_result;
48
49 template <typename Fn, typename ...Args, typename Ret>
50 struct test_invoke_result<Fn(Args...), Ret>
51 {
calltest_invoke_result52 static void call()
53 {
54 static_assert(std::is_invocable<Fn, Args...>::value, "");
55 static_assert(std::is_invocable_r<Ret, Fn, Args...>::value, "");
56 static_assert((std::is_same<typename std::invoke_result<Fn, Args...>::type, Ret>::value), "");
57 }
58 };
59 #endif
60
61 template <class T, class U>
test_result_of()62 void test_result_of()
63 {
64 static_assert((std::is_same<typename std::result_of<T>::type, U>::value), "");
65 #if TEST_STD_VER > 14
66 test_invoke_result<T, U>::call();
67 #endif
68 }
69
70 #if TEST_STD_VER > 14
71 template <typename T>
72 struct test_invoke_no_result;
73
74 template <typename Fn, typename ...Args>
75 struct test_invoke_no_result<Fn(Args...)>
76 {
calltest_invoke_no_result77 static void call()
78 {
79 static_assert(std::is_invocable<Fn, Args...>::value == false, "");
80 static_assert((!HasType<std::invoke_result<Fn, Args...> >::value), "");
81 }
82 };
83 #endif
84
85 template <class T>
test_no_result()86 void test_no_result()
87 {
88 #if TEST_STD_VER >= 11
89 static_assert((!HasType<std::result_of<T> >::value), "");
90 #endif
91 #if TEST_STD_VER > 14
92 test_invoke_no_result<T>::call();
93 #endif
94 }
95
main()96 int main()
97 {
98 typedef NotDerived ND;
99 { // functor object
100 test_result_of<S(int), short> ();
101 test_result_of<S&(unsigned char, int&), double> ();
102 test_result_of<S const&(unsigned char, int&), double const &> ();
103 test_result_of<S volatile&(unsigned char, int&), double volatile&> ();
104 test_result_of<S const volatile&(unsigned char, int&), double const volatile&> ();
105 }
106 { // pointer to function
107 typedef bool (&RF0)();
108 typedef bool* (&RF1)(int);
109 typedef bool& (&RF2)(int, int);
110 typedef bool const& (&RF3)(int, int, int);
111 typedef bool (&RF4)(int, ...);
112 typedef bool (*PF0)();
113 typedef bool* (*PF1)(int);
114 typedef bool& (*PF2)(int, int);
115 typedef bool const& (*PF3)(int, int, int);
116 typedef bool (*PF4)(int, ...);
117 typedef bool (*&PRF0)();
118 typedef bool* (*&PRF1)(int);
119 typedef bool& (*&PRF2)(int, int);
120 typedef bool const& (*&PRF3)(int, int, int);
121 typedef bool (*&PRF4)(int, ...);
122 test_result_of<RF0(), bool>();
123 test_result_of<RF1(int), bool*>();
124 test_result_of<RF2(int, long), bool&>();
125 test_result_of<RF3(int, long, int), bool const&>();
126 test_result_of<RF4(int, float, void*), bool>();
127 test_result_of<PF0(), bool>();
128 test_result_of<PF1(int), bool*>();
129 test_result_of<PF2(int, long), bool&>();
130 test_result_of<PF3(int, long, int), bool const&>();
131 test_result_of<PF4(int, float, void*), bool>();
132 test_result_of<PRF0(), bool>();
133 test_result_of<PRF1(int), bool*>();
134 test_result_of<PRF2(int, long), bool&>();
135 test_result_of<PRF3(int, long, int), bool const&>();
136 test_result_of<PRF4(int, float, void*), bool>();
137 }
138 { // pointer to member function
139
140 typedef int (S::*PMS0)();
141 typedef int* (S::*PMS1)(long);
142 typedef int& (S::*PMS2)(long, int);
143 typedef const int& (S::*PMS3)(int, ...);
144 test_result_of<PMS0( S), int> ();
145 test_result_of<PMS0( S&), int> ();
146 test_result_of<PMS0( S*), int> ();
147 test_result_of<PMS0( S*&), int> ();
148 test_result_of<PMS0( std::reference_wrapper<S>), int> ();
149 test_result_of<PMS0(const std::reference_wrapper<S>&), int> ();
150 test_result_of<PMS0( std::reference_wrapper<SD>), int> ();
151 test_result_of<PMS0(const std::reference_wrapper<SD>&), int> ();
152 test_result_of<PMS0(std::unique_ptr<S>), int> ();
153 test_result_of<PMS0(std::unique_ptr<SD>), int> ();
154 test_no_result<PMS0(const S&)>();
155 test_no_result<PMS0(volatile S&)>();
156 test_no_result<PMS0(const volatile S&)>();
157 test_no_result<PMS0(ND & )>();
158 test_no_result<PMS0(const ND& )>();
159 test_no_result<PMS0(std::unique_ptr<S const> )>();
160 test_no_result<PMS0(std::reference_wrapper<S const>)>();
161 test_no_result<PMS0(std::reference_wrapper<ND> )>();
162 test_no_result<PMS0(std::unique_ptr<ND> )>();
163
164 test_result_of<PMS1( S, int), int*> ();
165 test_result_of<PMS1( S&, int), int*> ();
166 test_result_of<PMS1( S*, int), int*> ();
167 test_result_of<PMS1( S*&, int), int*> ();
168 test_result_of<PMS1(std::unique_ptr<S>, int), int*> ();
169 test_result_of<PMS1(std::unique_ptr<SD>, int), int*> ();
170 test_result_of<PMS1(std::reference_wrapper<S>, int), int*> ();
171 test_result_of<PMS1(const std::reference_wrapper<S>&, int), int*> ();
172 test_result_of<PMS1(std::reference_wrapper<SD>, int), int*> ();
173 test_result_of<PMS1(const std::reference_wrapper<SD>&, int), int*> ();
174 test_no_result<PMS1(const S&, int)>();
175 test_no_result<PMS1(volatile S&, int)>();
176 test_no_result<PMS1(const volatile S&, int)>();
177 test_no_result<PMS1(ND &, int)>();
178 test_no_result<PMS1(const ND&, int)>();
179 test_no_result<PMS1(std::unique_ptr<S const>, int)>();
180 test_no_result<PMS1(std::reference_wrapper<S const>, int)>();
181 test_no_result<PMS1(std::reference_wrapper<ND>, int)>();
182 test_no_result<PMS1(std::unique_ptr<ND>, int)>();
183
184 test_result_of<PMS2( S, int, int), int&> ();
185 test_result_of<PMS2( S&, int, int), int&> ();
186 test_result_of<PMS2( S*, int, int), int&> ();
187 test_result_of<PMS2( S*&, int, int), int&> ();
188 test_result_of<PMS2(std::unique_ptr<S>, int, int), int&> ();
189 test_result_of<PMS2(std::unique_ptr<SD>, int, int), int&> ();
190 test_result_of<PMS2(std::reference_wrapper<S>, int, int), int&> ();
191 test_result_of<PMS2(const std::reference_wrapper<S>&, int, int), int&> ();
192 test_result_of<PMS2(std::reference_wrapper<SD>, int, int), int&> ();
193 test_result_of<PMS2(const std::reference_wrapper<SD>&, int, int), int&> ();
194 test_no_result<PMS2(const S&, int, int)>();
195 test_no_result<PMS2(volatile S&, int, int)>();
196 test_no_result<PMS2(const volatile S&, int, int)>();
197 test_no_result<PMS2(std::unique_ptr<S const>, int, int)>();
198 test_no_result<PMS2(std::reference_wrapper<S const>, int, int)>();
199 test_no_result<PMS2(const ND&, int, int)>();
200 test_no_result<PMS2(std::reference_wrapper<ND>, int, int)>();
201 test_no_result<PMS2(std::unique_ptr<ND>, int, int)>();
202
203 test_result_of<PMS3(S&, int), const int &>();
204 test_result_of<PMS3(S&, int, long), const int &>();
205
206 typedef int (S::*PMS0C)() const;
207 typedef int* (S::*PMS1C)(long) const;
208 typedef int& (S::*PMS2C)(long, int) const;
209 typedef const int& (S::*PMS3C)(int, ...) const;
210 test_result_of<PMS0C( S), int> ();
211 test_result_of<PMS0C( S&), int> ();
212 test_result_of<PMS0C(const S&), int> ();
213 test_result_of<PMS0C( S*), int> ();
214 test_result_of<PMS0C(const S*), int> ();
215 test_result_of<PMS0C( S*&), int> ();
216 test_result_of<PMS0C(const S*&), int> ();
217 test_result_of<PMS0C(std::unique_ptr<S>), int> ();
218 test_result_of<PMS0C(std::unique_ptr<SD>), int> ();
219 test_result_of<PMS0C(std::reference_wrapper<S> ), int> ();
220 test_result_of<PMS0C(std::reference_wrapper<const S> ), int> ();
221 test_result_of<PMS0C(const std::reference_wrapper<S> & ), int> ();
222 test_result_of<PMS0C(const std::reference_wrapper<const S> &), int> ();
223 test_result_of<PMS0C(std::reference_wrapper<SD> ), int> ();
224 test_result_of<PMS0C(std::reference_wrapper<const SD> ), int> ();
225 test_result_of<PMS0C(const std::reference_wrapper<SD> & ), int> ();
226 test_result_of<PMS0C(const std::reference_wrapper<const SD> &), int> ();
227 test_no_result<PMS0C(volatile S&)>();
228 test_no_result<PMS0C(const volatile S&)>();
229
230 test_result_of<PMS1C( S, int), int*> ();
231 test_result_of<PMS1C( S&, int), int*> ();
232 test_result_of<PMS1C(const S&, int), int*> ();
233 test_result_of<PMS1C( S*, int), int*> ();
234 test_result_of<PMS1C(const S*, int), int*> ();
235 test_result_of<PMS1C( S*&, int), int*> ();
236 test_result_of<PMS1C(const S*&, int), int*> ();
237 test_result_of<PMS1C(std::unique_ptr<S>, int), int*> ();
238 test_no_result<PMS1C(volatile S&, int)>();
239 test_no_result<PMS1C(const volatile S&, int)>();
240
241 test_result_of<PMS2C( S, int, int), int&> ();
242 test_result_of<PMS2C( S&, int, int), int&> ();
243 test_result_of<PMS2C(const S&, int, int), int&> ();
244 test_result_of<PMS2C( S*, int, int), int&> ();
245 test_result_of<PMS2C(const S*, int, int), int&> ();
246 test_result_of<PMS2C( S*&, int, int), int&> ();
247 test_result_of<PMS2C(const S*&, int, int), int&> ();
248 test_result_of<PMS2C(std::unique_ptr<S>, int, int), int&> ();
249 test_no_result<PMS2C(volatile S&, int, int)>();
250 test_no_result<PMS2C(const volatile S&, int, int)>();
251
252 test_result_of<PMS3C(S&, int), const int &>();
253 test_result_of<PMS3C(S&, int, long), const int &>();
254
255 typedef int (S::*PMS0V)() volatile;
256 typedef int* (S::*PMS1V)(long) volatile;
257 typedef int& (S::*PMS2V)(long, int) volatile;
258 typedef const int& (S::*PMS3V)(int, ...) volatile;
259 test_result_of<PMS0V( S), int> ();
260 test_result_of<PMS0V( S&), int> ();
261 test_result_of<PMS0V(volatile S&), int> ();
262 test_result_of<PMS0V( S*), int> ();
263 test_result_of<PMS0V(volatile S*), int> ();
264 test_result_of<PMS0V( S*&), int> ();
265 test_result_of<PMS0V(volatile S*&), int> ();
266 test_result_of<PMS0V(std::unique_ptr<S>), int> ();
267 test_no_result<PMS0V(const S&)>();
268 test_no_result<PMS0V(const volatile S&)>();
269
270 test_result_of<PMS1V( S, int), int*> ();
271 test_result_of<PMS1V( S&, int), int*> ();
272 test_result_of<PMS1V(volatile S&, int), int*> ();
273 test_result_of<PMS1V( S*, int), int*> ();
274 test_result_of<PMS1V(volatile S*, int), int*> ();
275 test_result_of<PMS1V( S*&, int), int*> ();
276 test_result_of<PMS1V(volatile S*&, int), int*> ();
277 test_result_of<PMS1V(std::unique_ptr<S>, int), int*> ();
278 test_no_result<PMS1V(const S&, int)>();
279 test_no_result<PMS1V(const volatile S&, int)>();
280
281 test_result_of<PMS2V( S, int, int), int&> ();
282 test_result_of<PMS2V( S&, int, int), int&> ();
283 test_result_of<PMS2V(volatile S&, int, int), int&> ();
284 test_result_of<PMS2V( S*, int, int), int&> ();
285 test_result_of<PMS2V(volatile S*, int, int), int&> ();
286 test_result_of<PMS2V( S*&, int, int), int&> ();
287 test_result_of<PMS2V(volatile S*&, int, int), int&> ();
288 test_result_of<PMS2V(std::unique_ptr<S>, int, int), int&> ();
289 test_no_result<PMS2V(const S&, int, int)>();
290 test_no_result<PMS2V(const volatile S&, int, int)>();
291
292 test_result_of<PMS3V(S&, int), const int &>();
293 test_result_of<PMS3V(S&, int, long), const int &>();
294
295 typedef int (S::*PMS0CV)() const volatile;
296 typedef int* (S::*PMS1CV)(long) const volatile;
297 typedef int& (S::*PMS2CV)(long, int) const volatile;
298 typedef const int& (S::*PMS3CV)(int, ...) const volatile;
299 test_result_of<PMS0CV( S), int> ();
300 test_result_of<PMS0CV( S&), int> ();
301 test_result_of<PMS0CV(const S&), int> ();
302 test_result_of<PMS0CV(volatile S&), int> ();
303 test_result_of<PMS0CV(const volatile S&), int> ();
304 test_result_of<PMS0CV( S*), int> ();
305 test_result_of<PMS0CV(const S*), int> ();
306 test_result_of<PMS0CV(volatile S*), int> ();
307 test_result_of<PMS0CV(const volatile S*), int> ();
308 test_result_of<PMS0CV( S*&), int> ();
309 test_result_of<PMS0CV(const S*&), int> ();
310 test_result_of<PMS0CV(volatile S*&), int> ();
311 test_result_of<PMS0CV(const volatile S*&), int> ();
312 test_result_of<PMS0CV(std::unique_ptr<S>), int> ();
313
314 test_result_of<PMS1CV( S, int), int*> ();
315 test_result_of<PMS1CV( S&, int), int*> ();
316 test_result_of<PMS1CV(const S&, int), int*> ();
317 test_result_of<PMS1CV(volatile S&, int), int*> ();
318 test_result_of<PMS1CV(const volatile S&, int), int*> ();
319 test_result_of<PMS1CV( S*, int), int*> ();
320 test_result_of<PMS1CV(const S*, int), int*> ();
321 test_result_of<PMS1CV(volatile S*, int), int*> ();
322 test_result_of<PMS1CV(const volatile S*, int), int*> ();
323 test_result_of<PMS1CV( S*&, int), int*> ();
324 test_result_of<PMS1CV(const S*&, int), int*> ();
325 test_result_of<PMS1CV(volatile S*&, int), int*> ();
326 test_result_of<PMS1CV(const volatile S*&, int), int*> ();
327 test_result_of<PMS1CV(std::unique_ptr<S>, int), int*> ();
328
329 test_result_of<PMS2CV( S, int, int), int&> ();
330 test_result_of<PMS2CV( S&, int, int), int&> ();
331 test_result_of<PMS2CV(const S&, int, int), int&> ();
332 test_result_of<PMS2CV(volatile S&, int, int), int&> ();
333 test_result_of<PMS2CV(const volatile S&, int, int), int&> ();
334 test_result_of<PMS2CV( S*, int, int), int&> ();
335 test_result_of<PMS2CV(const S*, int, int), int&> ();
336 test_result_of<PMS2CV(volatile S*, int, int), int&> ();
337 test_result_of<PMS2CV(const volatile S*, int, int), int&> ();
338 test_result_of<PMS2CV( S*&, int, int), int&> ();
339 test_result_of<PMS2CV(const S*&, int, int), int&> ();
340 test_result_of<PMS2CV(volatile S*&, int, int), int&> ();
341 test_result_of<PMS2CV(const volatile S*&, int, int), int&> ();
342 test_result_of<PMS2CV(std::unique_ptr<S>, int, int), int&> ();
343
344 test_result_of<PMS3CV(S&, int), const int &>();
345 test_result_of<PMS3CV(S&, int, long), const int &>();
346 }
347 { // pointer to member data
348 typedef char S::*PMD;
349 test_result_of<PMD(S&), char &>();
350 test_result_of<PMD(S*), char &>();
351 test_result_of<PMD(S* const), char &>();
352 test_result_of<PMD(const S&), const char&> ();
353 test_result_of<PMD(const S*), const char&> ();
354 test_result_of<PMD(volatile S&), volatile char&> ();
355 test_result_of<PMD(volatile S*), volatile char&> ();
356 test_result_of<PMD(const volatile S&), const volatile char&> ();
357 test_result_of<PMD(const volatile S*), const volatile char&> ();
358 test_result_of<PMD(SD&), char &>();
359 test_result_of<PMD(SD const&), const char&>();
360 test_result_of<PMD(SD*), char&>();
361 test_result_of<PMD(const SD*), const char&>();
362 test_result_of<PMD(std::unique_ptr<S>), char &>();
363 test_result_of<PMD(std::unique_ptr<S const>), const char&>();
364 #if TEST_STD_VER >= 11
365 test_result_of<PMD(std::reference_wrapper<S>), char&>();
366 test_result_of<PMD(std::reference_wrapper<S const>), const char&>();
367 #endif
368 test_no_result<PMD(ND&)>();
369 }
370 }
371