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