• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUNCTIONAL_BASE_03
12#define _LIBCPP_FUNCTIONAL_BASE_03
13
14// manual variadic expansion for <functional>
15
16// __weak_result_type
17
18template <class _Tp>
19struct __derives_from_unary_function
20{
21private:
22    struct __two {char __lx; char __lxx;};
23    static __two __test(...);
24    template <class _Ap, class _Rp>
25        static unary_function<_Ap, _Rp>
26        __test(const volatile unary_function<_Ap, _Rp>*);
27public:
28    static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
29    typedef decltype(__test((_Tp*)0)) type;
30};
31
32template <class _Tp>
33struct __derives_from_binary_function
34{
35private:
36    struct __two {char __lx; char __lxx;};
37    static __two __test(...);
38    template <class _A1, class _A2, class _Rp>
39        static binary_function<_A1, _A2, _Rp>
40        __test(const volatile binary_function<_A1, _A2, _Rp>*);
41public:
42    static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
43    typedef decltype(__test((_Tp*)0)) type;
44};
45
46template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
47struct __maybe_derive_from_unary_function  // bool is true
48    : public __derives_from_unary_function<_Tp>::type
49{
50};
51
52template <class _Tp>
53struct __maybe_derive_from_unary_function<_Tp, false>
54{
55};
56
57template <class _Tp, bool = __derives_from_binary_function<_Tp>::value>
58struct __maybe_derive_from_binary_function  // bool is true
59    : public __derives_from_binary_function<_Tp>::type
60{
61};
62
63template <class _Tp>
64struct __maybe_derive_from_binary_function<_Tp, false>
65{
66};
67
68template <class _Tp, bool = __has_result_type<_Tp>::value>
69struct __weak_result_type_imp // bool is true
70    : public __maybe_derive_from_unary_function<_Tp>,
71      public __maybe_derive_from_binary_function<_Tp>
72{
73    typedef typename _Tp::result_type result_type;
74};
75
76template <class _Tp>
77struct __weak_result_type_imp<_Tp, false>
78    : public __maybe_derive_from_unary_function<_Tp>,
79      public __maybe_derive_from_binary_function<_Tp>
80{
81};
82
83template <class _Tp>
84struct __weak_result_type
85    : public __weak_result_type_imp<typename remove_reference<_Tp>::type>
86{
87};
88
89// 0 argument case
90
91template <class _Rp>
92struct __weak_result_type<_Rp ()>
93{
94    typedef _Rp result_type;
95};
96
97template <class _Rp>
98struct __weak_result_type<_Rp (&)()>
99{
100    typedef _Rp result_type;
101};
102
103template <class _Rp>
104struct __weak_result_type<_Rp (*)()>
105{
106    typedef _Rp result_type;
107};
108
109// 1 argument case
110
111template <class _Rp, class _A1>
112struct __weak_result_type<_Rp (_A1)>
113    : public unary_function<_A1, _Rp>
114{
115};
116
117template <class _Rp, class _A1>
118struct __weak_result_type<_Rp (&)(_A1)>
119    : public unary_function<_A1, _Rp>
120{
121};
122
123template <class _Rp, class _A1>
124struct __weak_result_type<_Rp (*)(_A1)>
125    : public unary_function<_A1, _Rp>
126{
127};
128
129template <class _Rp, class _Cp>
130struct __weak_result_type<_Rp (_Cp::*)()>
131    : public unary_function<_Cp*, _Rp>
132{
133};
134
135template <class _Rp, class _Cp>
136struct __weak_result_type<_Rp (_Cp::*)() const>
137    : public unary_function<const _Cp*, _Rp>
138{
139};
140
141template <class _Rp, class _Cp>
142struct __weak_result_type<_Rp (_Cp::*)() volatile>
143    : public unary_function<volatile _Cp*, _Rp>
144{
145};
146
147template <class _Rp, class _Cp>
148struct __weak_result_type<_Rp (_Cp::*)() const volatile>
149    : public unary_function<const volatile _Cp*, _Rp>
150{
151};
152
153// 2 argument case
154
155template <class _Rp, class _A1, class _A2>
156struct __weak_result_type<_Rp (_A1, _A2)>
157    : public binary_function<_A1, _A2, _Rp>
158{
159};
160
161template <class _Rp, class _A1, class _A2>
162struct __weak_result_type<_Rp (*)(_A1, _A2)>
163    : public binary_function<_A1, _A2, _Rp>
164{
165};
166
167template <class _Rp, class _A1, class _A2>
168struct __weak_result_type<_Rp (&)(_A1, _A2)>
169    : public binary_function<_A1, _A2, _Rp>
170{
171};
172
173template <class _Rp, class _Cp, class _A1>
174struct __weak_result_type<_Rp (_Cp::*)(_A1)>
175    : public binary_function<_Cp*, _A1, _Rp>
176{
177};
178
179template <class _Rp, class _Cp, class _A1>
180struct __weak_result_type<_Rp (_Cp::*)(_A1) const>
181    : public binary_function<const _Cp*, _A1, _Rp>
182{
183};
184
185template <class _Rp, class _Cp, class _A1>
186struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile>
187    : public binary_function<volatile _Cp*, _A1, _Rp>
188{
189};
190
191template <class _Rp, class _Cp, class _A1>
192struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile>
193    : public binary_function<const volatile _Cp*, _A1, _Rp>
194{
195};
196
197// 3 or more arguments
198
199template <class _Rp, class _A1, class _A2, class _A3>
200struct __weak_result_type<_Rp (_A1, _A2, _A3)>
201{
202    typedef _Rp result_type;
203};
204
205template <class _Rp, class _A1, class _A2, class _A3>
206struct __weak_result_type<_Rp (&)(_A1, _A2, _A3)>
207{
208    typedef _Rp result_type;
209};
210
211template <class _Rp, class _A1, class _A2, class _A3>
212struct __weak_result_type<_Rp (*)(_A1, _A2, _A3)>
213{
214    typedef _Rp result_type;
215};
216
217template <class _Rp, class _Cp, class _A1, class _A2>
218struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2)>
219{
220    typedef _Rp result_type;
221};
222
223template <class _Rp, class _Cp, class _A1, class _A2>
224struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2) const>
225{
226    typedef _Rp result_type;
227};
228
229template <class _Rp, class _Cp, class _A1, class _A2>
230struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2) volatile>
231{
232    typedef _Rp result_type;
233};
234
235// __invoke
236
237// __ref_return0
238//
239// template <class _Tp, bool _HasResultType>
240// struct ________ref_return0  // _HasResultType is true
241// {
242//     typedef typename _Tp::result_type type;
243// };
244//
245// template <class _Tp>
246// struct ________ref_return0<_Tp, false>
247// {
248//     typedef void type;
249// };
250//
251// template <class _Tp, bool _IsClass>
252// struct ____ref_return0  // _IsClass is true
253//     : public ________ref_return0<_Tp, __has_result_type<typename remove_cv<_Tp>::type>::value>
254// {
255// };
256//
257// template <class _Tp, bool _HasResultType>
258// struct ______ref_return0  // _HasResultType is true
259// {
260//     typedef typename __callable_type<_Tp>::result_type type;
261// };
262//
263// template <class _Tp>
264// struct ______ref_return0<_Tp, false>  // pointer to member data
265// {
266//     typedef void type;
267// };
268//
269// template <class _Tp>
270// struct ____ref_return0<_Tp, false>
271//     : public ______ref_return0<typename remove_cv<_Tp>::type,
272//                  __has_result_type<__callable_type<typename remove_cv<_Tp>::type> >::value>
273// {
274// };
275//
276// template <class _Tp>
277// struct __ref_return0
278//     : public ____ref_return0<typename remove_reference<_Tp>::type,
279//                    is_class<typename remove_reference<_Tp>::type>::value>
280// {
281// };
282//
283// __ref_return1
284//
285// template <class _Tp, bool _IsClass, class _A0>
286// struct ____ref_return1  // _IsClass is true
287// {
288//     typedef typename result_of<_Tp(_A0)>::type type;
289// };
290//
291// template <class _Tp, bool _HasResultType, class _A0>
292// struct ______ref_return1  // _HasResultType is true
293// {
294//     typedef typename __callable_type<_Tp>::result_type type;
295// };
296//
297// template <class _Tp, class _A0, bool>
298// struct __ref_return1_member_data1;
299//
300// template <class _Rp, class _Cp, class _A0>
301// struct __ref_return1_member_data1<_Rp _Cp::*, _A0, true>
302// {
303//     typedef typename __apply_cv<_A0, _Rp>::type& type;
304// };
305//
306// template <class _Rp, class _Cp, class _A0>
307// struct __ref_return1_member_data1<_Rp _Cp::*, _A0, false>
308// {
309//     static _A0 __a;
310//     typedef typename __apply_cv<decltype(*__a), _Rp>::type& type;
311// };
312//
313// template <class _Tp, class _A0>
314// struct __ref_return1_member_data;
315//
316// template <class _Rp, class _Cp, class _A0>
317// struct __ref_return1_member_data<_Rp _Cp::*, _A0>
318//     : public __ref_return1_member_data1<_Rp _Cp::*, _A0,
319//                 is_same<typename remove_cv<_Cp>::type,
320//                         typename remove_cv<typename remove_reference<_A0>::type>::type>::value>
321// {
322// };
323//
324// template <class _Tp, class _A0>
325// struct ______ref_return1<_Tp, false, _A0>  // pointer to member data
326//     : public __ref_return1_member_data<typename remove_cv<_Tp>::type, _A0>
327// {
328// };
329//
330// template <class _Tp, class _A0>
331// struct ____ref_return1<_Tp, false, _A0>
332//     : public ______ref_return1<typename remove_cv<_Tp>::type,
333//                  __has_result_type<__callable_type<typename remove_cv<_Tp>::type> >::value, _A0>
334// {
335// };
336//
337// template <class _Tp, class _A0>
338// struct __ref_return1
339//     : public ____ref_return1<typename remove_reference<_Tp>::type,
340//                    is_class<typename remove_reference<_Tp>::type>::value, _A0>
341// {
342// };
343//
344// __ref_return2
345//
346// template <class _Tp, bool _IsClass, class _A0, class _A1>
347// struct ____ref_return2  // _IsClass is true
348// {
349//     typedef typename result_of<_Tp(_A0, _A1)>::type type;
350// };
351//
352// template <class _Tp, bool _HasResultType, class _A0, class _A1>
353// struct ______ref_return2  // _HasResultType is true
354// {
355//     typedef typename __callable_type<_Tp>::result_type type;
356// };
357//
358// template <class _Tp>
359// struct ______ref_return2<_Tp, false, class _A0, class _A1>  // pointer to member data
360// {
361//     static_assert(sizeof(_Tp) == 0, "An attempt has been made to `call` a pointer"
362//                          " to member data with too many arguments.");
363// };
364//
365// template <class _Tp, class _A0, class _A1>
366// struct ____ref_return2<_Tp, false, _A0, _A1>
367//     : public ______ref_return2<typename remove_cv<_Tp>::type,
368//                  __has_result_type<__callable_type<typename remove_cv<_Tp>::type> >::value, _A0, _A1>
369// {
370// };
371//
372// template <class _Tp, class _A0, class _A1>
373// struct __ref_return2
374//     : public ____ref_return2<typename remove_reference<_Tp>::type,
375//                    is_class<typename remove_reference<_Tp>::type>::value, _A0, _A1>
376// {
377// };
378//
379// __ref_return3
380//
381// template <class _Tp, bool _IsClass, class _A0, class _A1, class _A2>
382// struct ____ref_return3  // _IsClass is true
383// {
384//     typedef typename result_of<_Tp(_A0, _A1, _A2)>::type type;
385// };
386//
387// template <class _Tp, bool _HasResultType, class _A0, class _A1, class _A2>
388// struct ______ref_return3  // _HasResultType is true
389// {
390//     typedef typename __callable_type<_Tp>::result_type type;
391// };
392//
393// template <class _Tp>
394// struct ______ref_return3<_Tp, false, class _A0, class _A1, class _A2>  // pointer to member data
395// {
396//     static_assert(sizeof(_Tp) == 0, "An attempt has been made to `call` a pointer"
397//                          " to member data with too many arguments.");
398// };
399//
400// template <class _Tp, class _A0, class _A1, class _A2>
401// struct ____ref_return3<_Tp, false, _A0, _A1, _A2>
402//     : public ______ref_return3<typename remove_cv<_Tp>::type,
403//                  __has_result_type<__callable_type<typename remove_cv<_Tp>::type> >::value, _A0, _A1, _A2>
404// {
405// };
406//
407// template <class _Tp, class _A0, class _A1, class _A2>
408// struct __ref_return3
409//     : public ____ref_return3<typename remove_reference<_Tp>::type,
410//                    is_class<typename remove_reference<_Tp>::type>::value, _A0, _A1, _A2>
411// {
412// };
413
414// first bullet
415
416template <class _Rp, class _Tp, class _T1>
417inline _LIBCPP_INLINE_VISIBILITY
418typename enable_if
419<
420    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
421    _Rp
422>::type
423__invoke(_Rp (_Tp::*__f)(), _T1& __t1)
424{
425    return (__t1.*__f)();
426}
427
428template <class _Rp, class _Tp, class _T1, class _A0>
429inline _LIBCPP_INLINE_VISIBILITY
430typename enable_if
431<
432    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
433    _Rp
434>::type
435__invoke(_Rp (_Tp::*__f)(_A0), _T1& __t1, _A0& __a0)
436{
437    return (__t1.*__f)(__a0);
438}
439
440template <class _Rp, class _Tp, class _T1, class _A0, class _A1>
441inline _LIBCPP_INLINE_VISIBILITY
442typename enable_if
443<
444    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
445    _Rp
446>::type
447__invoke(_Rp (_Tp::*__f)(_A0, _A1), _T1& __t1, _A0& __a0, _A1& __a1)
448{
449    return (__t1.*__f)(__a0, __a1);
450}
451
452template <class _Rp, class _Tp, class _T1, class _A0, class _A1, class _A2>
453inline _LIBCPP_INLINE_VISIBILITY
454typename enable_if
455<
456    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
457    _Rp
458>::type
459__invoke(_Rp (_Tp::*__f)(_A0, _A1, _A2), _T1& __t1, _A0& __a0, _A1& __a1, _A2& __a2)
460{
461    return (__t1.*__f)(__a0, __a1, __a2);
462}
463
464template <class _Rp, class _Tp, class _T1>
465inline _LIBCPP_INLINE_VISIBILITY
466typename enable_if
467<
468    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
469    _Rp
470>::type
471__invoke(_Rp (_Tp::*__f)() const, _T1& __t1)
472{
473    return (__t1.*__f)();
474}
475
476template <class _Rp, class _Tp, class _T1, class _A0>
477inline _LIBCPP_INLINE_VISIBILITY
478typename enable_if
479<
480    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
481    _Rp
482>::type
483__invoke(_Rp (_Tp::*__f)(_A0) const, _T1& __t1, _A0& __a0)
484{
485    return (__t1.*__f)(__a0);
486}
487
488template <class _Rp, class _Tp, class _T1, class _A0, class _A1>
489inline _LIBCPP_INLINE_VISIBILITY
490typename enable_if
491<
492    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
493    _Rp
494>::type
495__invoke(_Rp (_Tp::*__f)(_A0, _A1) const, _T1& __t1, _A0& __a0, _A1& __a1)
496{
497    return (__t1.*__f)(__a0, __a1);
498}
499
500template <class _Rp, class _Tp, class _T1, class _A0, class _A1, class _A2>
501inline _LIBCPP_INLINE_VISIBILITY
502typename enable_if
503<
504    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
505    _Rp
506>::type
507__invoke(_Rp (_Tp::*__f)(_A0, _A1, _A2) const, _T1& __t1, _A0& __a0, _A1& __a1, _A2& __a2)
508{
509    return (__t1.*__f)(__a0, __a1, __a2);
510}
511
512template <class _Rp, class _Tp, class _T1>
513inline _LIBCPP_INLINE_VISIBILITY
514typename enable_if
515<
516    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
517    _Rp
518>::type
519__invoke(_Rp (_Tp::*__f)() volatile, _T1& __t1)
520{
521    return (__t1.*__f)();
522}
523
524template <class _Rp, class _Tp, class _T1, class _A0>
525inline _LIBCPP_INLINE_VISIBILITY
526typename enable_if
527<
528    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
529    _Rp
530>::type
531__invoke(_Rp (_Tp::*__f)(_A0) volatile, _T1& __t1, _A0& __a0)
532{
533    return (__t1.*__f)(__a0);
534}
535
536template <class _Rp, class _Tp, class _T1, class _A0, class _A1>
537inline _LIBCPP_INLINE_VISIBILITY
538typename enable_if
539<
540    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
541    _Rp
542>::type
543__invoke(_Rp (_Tp::*__f)(_A0, _A1) volatile, _T1& __t1, _A0& __a0, _A1& __a1)
544{
545    return (__t1.*__f)(__a0, __a1);
546}
547
548template <class _Rp, class _Tp, class _T1, class _A0, class _A1, class _A2>
549inline _LIBCPP_INLINE_VISIBILITY
550typename enable_if
551<
552    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
553    _Rp
554>::type
555__invoke(_Rp (_Tp::*__f)(_A0, _A1, _A2) volatile, _T1& __t1, _A0& __a0, _A1& __a1, _A2& __a2)
556{
557    return (__t1.*__f)(__a0, __a1, __a2);
558}
559
560template <class _Rp, class _Tp, class _T1>
561inline _LIBCPP_INLINE_VISIBILITY
562typename enable_if
563<
564    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
565    _Rp
566>::type
567__invoke(_Rp (_Tp::*__f)() const volatile, _T1& __t1)
568{
569    return (__t1.*__f)();
570}
571
572template <class _Rp, class _Tp, class _T1, class _A0>
573inline _LIBCPP_INLINE_VISIBILITY
574typename enable_if
575<
576    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
577    _Rp
578>::type
579__invoke(_Rp (_Tp::*__f)(_A0) const volatile, _T1& __t1, _A0& __a0)
580{
581    return (__t1.*__f)(__a0);
582}
583
584template <class _Rp, class _Tp, class _T1, class _A0, class _A1>
585inline _LIBCPP_INLINE_VISIBILITY
586typename enable_if
587<
588    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
589    _Rp
590>::type
591__invoke(_Rp (_Tp::*__f)(_A0, _A1) const volatile, _T1& __t1, _A0& __a0, _A1& __a1)
592{
593    return (__t1.*__f)(__a0, __a1);
594}
595
596template <class _Rp, class _Tp, class _T1, class _A0, class _A1, class _A2>
597inline _LIBCPP_INLINE_VISIBILITY
598typename enable_if
599<
600    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
601    _Rp
602>::type
603__invoke(_Rp (_Tp::*__f)(_A0, _A1, _A2) const volatile, _T1& __t1, _A0& __a0, _A1& __a1, _A2& __a2)
604{
605    return (__t1.*__f)(__a0, __a1, __a2);
606}
607
608// second bullet
609
610template <class _Rp, class _Tp, class _T1>
611inline _LIBCPP_INLINE_VISIBILITY
612typename enable_if
613<
614    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
615    _Rp
616>::type
617__invoke(_Rp (_Tp::*__f)(), _T1 __t1)
618{
619    return ((*__t1).*__f)();
620}
621
622template <class _Rp, class _Tp, class _T1, class _A0>
623inline _LIBCPP_INLINE_VISIBILITY
624typename enable_if
625<
626    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
627    _Rp
628>::type
629__invoke(_Rp (_Tp::*__f)(_A0), _T1 __t1, _A0& __a0)
630{
631    return ((*__t1).*__f)(__a0);
632}
633
634template <class _Rp, class _Tp, class _T1, class _A0, class _A1>
635inline _LIBCPP_INLINE_VISIBILITY
636typename enable_if
637<
638    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
639    _Rp
640>::type
641__invoke(_Rp (_Tp::*__f)(_A0, _A1), _T1 __t1, _A0& __a0, _A1& __a1)
642{
643    return ((*__t1).*__f)(__a0, __a1);
644}
645
646template <class _Rp, class _Tp, class _T1, class _A0, class _A1, class _A2>
647inline _LIBCPP_INLINE_VISIBILITY
648typename enable_if
649<
650    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
651    _Rp
652>::type
653__invoke(_Rp (_Tp::*__f)(_A0, _A1, _A2), _T1 __t1, _A0& __a0, _A1& __a1, _A2& __a2)
654{
655    return ((*__t1).*__f)(__a0, __a1, __a2);
656}
657
658template <class _Rp, class _Tp, class _T1>
659inline _LIBCPP_INLINE_VISIBILITY
660typename enable_if
661<
662    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
663    _Rp
664>::type
665__invoke(_Rp (_Tp::*__f)() const, _T1 __t1)
666{
667    return ((*__t1).*__f)();
668}
669
670template <class _Rp, class _Tp, class _T1, class _A0>
671inline _LIBCPP_INLINE_VISIBILITY
672typename enable_if
673<
674    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
675    _Rp
676>::type
677__invoke(_Rp (_Tp::*__f)(_A0) const, _T1 __t1, _A0& __a0)
678{
679    return ((*__t1).*__f)(__a0);
680}
681
682template <class _Rp, class _Tp, class _T1, class _A0, class _A1>
683inline _LIBCPP_INLINE_VISIBILITY
684typename enable_if
685<
686    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
687    _Rp
688>::type
689__invoke(_Rp (_Tp::*__f)(_A0, _A1) const, _T1 __t1, _A0& __a0, _A1& __a1)
690{
691    return ((*__t1).*__f)(__a0, __a1);
692}
693
694template <class _Rp, class _Tp, class _T1, class _A0, class _A1, class _A2>
695inline _LIBCPP_INLINE_VISIBILITY
696typename enable_if
697<
698    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
699    _Rp
700>::type
701__invoke(_Rp (_Tp::*__f)(_A0, _A1, _A2) const, _T1 __t1, _A0& __a0, _A1& __a1, _A2& __a2)
702{
703    return ((*__t1).*__f)(__a0, __a1, __a2);
704}
705
706template <class _Rp, class _Tp, class _T1>
707inline _LIBCPP_INLINE_VISIBILITY
708typename enable_if
709<
710    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
711    _Rp
712>::type
713__invoke(_Rp (_Tp::*__f)() volatile, _T1 __t1)
714{
715    return ((*__t1).*__f)();
716}
717
718template <class _Rp, class _Tp, class _T1, class _A0>
719inline _LIBCPP_INLINE_VISIBILITY
720typename enable_if
721<
722    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
723    _Rp
724>::type
725__invoke(_Rp (_Tp::*__f)(_A0) volatile, _T1 __t1, _A0& __a0)
726{
727    return ((*__t1).*__f)(__a0);
728}
729
730template <class _Rp, class _Tp, class _T1, class _A0, class _A1>
731inline _LIBCPP_INLINE_VISIBILITY
732typename enable_if
733<
734    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
735    _Rp
736>::type
737__invoke(_Rp (_Tp::*__f)(_A0, _A1) volatile, _T1 __t1, _A0& __a0, _A1& __a1)
738{
739    return ((*__t1).*__f)(__a0, __a1);
740}
741
742template <class _Rp, class _Tp, class _T1, class _A0, class _A1, class _A2>
743inline _LIBCPP_INLINE_VISIBILITY
744typename enable_if
745<
746    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
747    _Rp
748>::type
749__invoke(_Rp (_Tp::*__f)(_A0, _A1, _A2) volatile, _T1 __t1, _A0& __a0, _A1& __a1, _A2& __a2)
750{
751    return ((*__t1).*__f)(__a0, __a1, __a2);
752}
753
754template <class _Rp, class _Tp, class _T1>
755inline _LIBCPP_INLINE_VISIBILITY
756typename enable_if
757<
758    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
759    _Rp
760>::type
761__invoke(_Rp (_Tp::*__f)() const volatile, _T1 __t1)
762{
763    return ((*__t1).*__f)();
764}
765
766template <class _Rp, class _Tp, class _T1, class _A0>
767inline _LIBCPP_INLINE_VISIBILITY
768typename enable_if
769<
770    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
771    _Rp
772>::type
773__invoke(_Rp (_Tp::*__f)(_A0) const volatile, _T1 __t1, _A0& __a0)
774{
775    return ((*__t1).*__f)(__a0);
776}
777
778template <class _Rp, class _Tp, class _T1, class _A0, class _A1>
779inline _LIBCPP_INLINE_VISIBILITY
780typename enable_if
781<
782    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
783    _Rp
784>::type
785__invoke(_Rp (_Tp::*__f)(_A0, _A1) const volatile, _T1 __t1, _A0& __a0, _A1& __a1)
786{
787    return ((*__t1).*__f)(__a0, __a1);
788}
789
790template <class _Rp, class _Tp, class _T1, class _A0, class _A1, class _A2>
791inline _LIBCPP_INLINE_VISIBILITY
792typename enable_if
793<
794    !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
795    _Rp
796>::type
797__invoke(_Rp (_Tp::*__f)(_A0, _A1, _A2) const volatile, _T1 __t1, _A0& __a0, _A1& __a1, _A2& __a2)
798{
799    return ((*__t1).*__f)(__a0, __a1, __a2);
800}
801
802// third bullet
803
804template <class _Rp, class _Tp, class _T1>
805inline _LIBCPP_INLINE_VISIBILITY
806typename enable_if
807<
808    is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
809    typename __apply_cv<_T1, _Rp>::type&
810>::type
811__invoke(_Rp _Tp::* __f, _T1& __t1)
812{
813    return __t1.*__f;
814}
815
816template <class _Rp, class _Tp>
817inline _LIBCPP_INLINE_VISIBILITY
818void
819__invoke(_Rp _Tp::*)
820{
821}
822
823// template <class _Dp, class _Rp, class _Tp, class _T1>
824// inline _LIBCPP_INLINE_VISIBILITY
825// typename enable_if
826// <
827//     is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
828//     typename __ref_return1<_Rp _Tp::*, _T1>::type
829// >::type
830// __invoke(_Rp _Tp::* __f, _T1& __t1)
831// {
832//     return __t1.*__f;
833// }
834
835// forth bullet
836
837template <class _T1, class _Rp, bool>
838struct __4th_helper
839{
840};
841
842template <class _T1, class _Rp>
843struct __4th_helper<_T1, _Rp, true>
844{
845    typedef typename __apply_cv<decltype(*_VSTD::declval<_T1>()), _Rp>::type type;
846};
847
848template <class _Rp, class _Tp, class _T1>
849inline _LIBCPP_INLINE_VISIBILITY
850typename __4th_helper<_T1, _Rp,
851                      !is_base_of<_Tp,
852                                  typename remove_reference<_T1>::type
853                                 >::value
854                     >::type&
855__invoke(_Rp _Tp::* __f, _T1& __t1)
856{
857    return (*__t1).*__f;
858}
859
860// template <class _Dp, class _Rp, class _Tp, class _T1>
861// inline _LIBCPP_INLINE_VISIBILITY
862// typename enable_if
863// <
864//     !is_base_of<_Tp, typename remove_reference<_T1>::type>::value,
865//     typename __ref_return1<_Rp _Tp::*, _T1>::type
866// >::type
867// __invoke(_Rp _Tp::* __f, _T1 __t1)
868// {
869//     return (*__t1).*__f;
870// }
871
872// fifth bullet
873
874template <class _Fp>
875inline _LIBCPP_INLINE_VISIBILITY
876decltype(declval<_Fp>()())
877__invoke(_Fp __f)
878{
879    return __f();
880}
881
882template <class _Fp, class _A0>
883inline _LIBCPP_INLINE_VISIBILITY
884decltype(declval<_Fp>()(declval<_A0&>()))
885__invoke(_Fp __f, _A0& __a0)
886{
887    return __f(__a0);
888}
889
890template <class _Fp, class _A0, class _A1>
891inline _LIBCPP_INLINE_VISIBILITY
892decltype(declval<_Fp>()(declval<_A0&>(), declval<_A1&>()))
893__invoke(_Fp __f, _A0& __a0, _A1& __a1)
894{
895    return __f(__a0, __a1);
896}
897
898template <class _Fp, class _A0, class _A1, class _A2>
899inline _LIBCPP_INLINE_VISIBILITY
900decltype(declval<_Fp>()(declval<_A0&>(), declval<_A1&>(), declval<_A2&>()))
901__invoke(_Fp __f, _A0& __a0, _A1& __a1, _A2& __a2)
902{
903    return __f(__a0, __a1, __a2);
904}
905
906// template <class _Rp, class _Fp>
907// inline _LIBCPP_INLINE_VISIBILITY
908// _Rp
909// __invoke(_Fp& __f)
910// {
911//     return __f();
912// }
913//
914// template <class _Rp, class _Fp, class _A0>
915// inline _LIBCPP_INLINE_VISIBILITY
916// typename enable_if
917// <
918//     !is_member_pointer<_Fp>::value,
919//     _Rp
920// >::type
921// __invoke(_Fp& __f, _A0& __a0)
922// {
923//     return __f(__a0);
924// }
925//
926// template <class _Rp, class _Fp, class _A0, class _A1>
927// inline _LIBCPP_INLINE_VISIBILITY
928// _Rp
929// __invoke(_Fp& __f, _A0& __a0, _A1& __a1)
930// {
931//     return __f(__a0, __a1);
932// }
933//
934// template <class _Rp, class _Fp, class _A0, class _A1, class _A2>
935// inline _LIBCPP_INLINE_VISIBILITY
936// _Rp
937// __invoke(_Fp& __f, _A0& __a0, _A1& __a1, _A2& __a2)
938// {
939//     return __f(__a0, __a1, __a2);
940// }
941
942template <class _Tp>
943struct __has_type
944{
945private:
946    struct __two {char __lx; char __lxx;};
947    template <class _Up> static __two __test(...);
948    template <class _Up> static char __test(typename _Up::type* = 0);
949public:
950    static const bool value = sizeof(__test<_Tp>(0)) == 1;
951};
952
953template <class _Fp, bool = __has_result_type<__weak_result_type<_Fp> >::value>
954struct __invoke_return
955{
956    typedef typename __weak_result_type<_Fp>::result_type type;
957};
958
959template <class _Fp>
960struct __invoke_return<_Fp, false>
961{
962    typedef decltype(__invoke(_VSTD::declval<_Fp>())) type;
963};
964
965template <class _Tp, class _A0>
966struct __invoke_return0
967{
968    typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_A0>())) type;
969};
970
971template <class _Rp, class _Tp, class _A0>
972struct __invoke_return0<_Rp _Tp::*, _A0>
973{
974    typedef typename __apply_cv<_A0, _Rp>::type& type;
975};
976
977template <class _Rp, class _Tp, class _A0>
978struct __invoke_return0<_Rp _Tp::*, _A0*>
979{
980    typedef typename __apply_cv<_A0, _Rp>::type& type;
981};
982
983template <class _Tp, class _A0, class _A1>
984struct __invoke_return1
985{
986    typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_A0>(),
987                                                    _VSTD::declval<_A1>())) type;
988};
989
990template <class _Tp, class _A0, class _A1, class _A2>
991struct __invoke_return2
992{
993    typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_A0>(),
994                                                    _VSTD::declval<_A1>(),
995                                                    _VSTD::declval<_A2>())) type;
996};
997
998template <class _Tp>
999class _LIBCPP_TYPE_VIS_ONLY reference_wrapper
1000    : public __weak_result_type<_Tp>
1001{
1002public:
1003    // types
1004    typedef _Tp type;
1005private:
1006    type* __f_;
1007
1008public:
1009    // construct/copy/destroy
1010    _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) : __f_(&__f) {}
1011
1012    // access
1013    _LIBCPP_INLINE_VISIBILITY operator type&    () const {return *__f_;}
1014    _LIBCPP_INLINE_VISIBILITY          type& get() const {return *__f_;}
1015
1016    // invoke
1017
1018    _LIBCPP_INLINE_VISIBILITY
1019    typename __invoke_return<type&>::type
1020       operator() () const
1021       {
1022           return __invoke(get());
1023       }
1024
1025    template <class _A0>
1026       _LIBCPP_INLINE_VISIBILITY
1027       typename __invoke_return0<type&, _A0>::type
1028          operator() (_A0& __a0) const
1029          {
1030              return __invoke(get(), __a0);
1031          }
1032
1033    template <class _A0, class _A1>
1034       _LIBCPP_INLINE_VISIBILITY
1035       typename __invoke_return1<type&, _A0, _A1>::type
1036          operator() (_A0& __a0, _A1& __a1) const
1037          {
1038              return __invoke(get(), __a0, __a1);
1039          }
1040
1041    template <class _A0, class _A1, class _A2>
1042       _LIBCPP_INLINE_VISIBILITY
1043       typename __invoke_return2<type&, _A0, _A1, _A2>::type
1044          operator() (_A0& __a0, _A1& __a1, _A2& __a2) const
1045          {
1046              return __invoke(get(), __a0, __a1, __a2);
1047          }
1048};
1049
1050template <class _Tp> struct __is_reference_wrapper_impl : public false_type {};
1051template <class _Tp> struct __is_reference_wrapper_impl<reference_wrapper<_Tp> > : public true_type {};
1052template <class _Tp> struct __is_reference_wrapper
1053    : public __is_reference_wrapper_impl<typename remove_cv<_Tp>::type> {};
1054
1055template <class _Tp>
1056inline _LIBCPP_INLINE_VISIBILITY
1057reference_wrapper<_Tp>
1058ref(_Tp& __t)
1059{
1060    return reference_wrapper<_Tp>(__t);
1061}
1062
1063template <class _Tp>
1064inline _LIBCPP_INLINE_VISIBILITY
1065reference_wrapper<_Tp>
1066ref(reference_wrapper<_Tp> __t)
1067{
1068    return ref(__t.get());
1069}
1070
1071template <class _Tp>
1072inline _LIBCPP_INLINE_VISIBILITY
1073reference_wrapper<const _Tp>
1074cref(const _Tp& __t)
1075{
1076    return reference_wrapper<const _Tp>(__t);
1077}
1078
1079template <class _Tp>
1080inline _LIBCPP_INLINE_VISIBILITY
1081reference_wrapper<const _Tp>
1082cref(reference_wrapper<_Tp> __t)
1083{
1084    return cref(__t.get());
1085}
1086
1087#endif  // _LIBCPP_FUNCTIONAL_BASE_03
1088