blob: e174e0ca003b3a4d6620afbe5c34db947a166796 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUNCTIONAL_BASE
12#define _LIBCPP_FUNCTIONAL_BASE
13
14#include <__config>
15#include <type_traits>
16#include <typeinfo>
17#include <exception>
Marshall Clow599e60d2013-09-12 02:11:16 +000018#include <new>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000019
Howard Hinnant08e17472011-10-17 20:05:10 +000020#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000022#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023
24_LIBCPP_BEGIN_NAMESPACE_STD
25
26template <class _Arg, class _Result>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000027struct _LIBCPP_TYPE_VIS_ONLY unary_function
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028{
29 typedef _Arg argument_type;
30 typedef _Result result_type;
31};
32
33template <class _Arg1, class _Arg2, class _Result>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000034struct _LIBCPP_TYPE_VIS_ONLY binary_function
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000035{
36 typedef _Arg1 first_argument_type;
37 typedef _Arg2 second_argument_type;
38 typedef _Result result_type;
39};
40
Howard Hinnant0f678bd2013-08-12 18:38:34 +000041template <class _Tp> struct _LIBCPP_TYPE_VIS_ONLY hash;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042
43template <class _Tp>
44struct __has_result_type
45{
46private:
Howard Hinnant9c0df142012-10-30 19:06:59 +000047 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048 template <class _Up> static __two __test(...);
49 template <class _Up> static char __test(typename _Up::result_type* = 0);
50public:
51 static const bool value = sizeof(__test<_Tp>(0)) == 1;
52};
53
Marshall Clowff464092013-07-29 14:21:53 +000054#if _LIBCPP_STD_VER > 11
55template <class _Tp = void>
56#else
Howard Hinnant3fadda32012-02-21 21:02:58 +000057template <class _Tp>
Marshall Clowff464092013-07-29 14:21:53 +000058#endif
Howard Hinnant0f678bd2013-08-12 18:38:34 +000059struct _LIBCPP_TYPE_VIS_ONLY less : binary_function<_Tp, _Tp, bool>
Howard Hinnant3fadda32012-02-21 21:02:58 +000060{
Marshall Clow9738caf2013-09-28 19:06:12 +000061 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
62 bool operator()(const _Tp& __x, const _Tp& __y) const
Howard Hinnant3fadda32012-02-21 21:02:58 +000063 {return __x < __y;}
64};
65
Marshall Clowff464092013-07-29 14:21:53 +000066#if _LIBCPP_STD_VER > 11
67template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000068struct _LIBCPP_TYPE_VIS_ONLY less<void>
Marshall Clowff464092013-07-29 14:21:53 +000069{
Marshall Clow9738caf2013-09-28 19:06:12 +000070 template <class _T1, class _T2>
71 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clowff464092013-07-29 14:21:53 +000072 auto operator()(_T1&& __t, _T2&& __u) const
73 { return _VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u); }
Marshall Clow4a0a9812013-08-13 01:11:06 +000074 typedef void is_transparent;
Marshall Clowff464092013-07-29 14:21:53 +000075};
76#endif
77
Howard Hinnanta4e87ab2013-08-08 18:38:55 +000078// addressof
79
80template <class _Tp>
81inline _LIBCPP_INLINE_VISIBILITY
82_Tp*
83addressof(_Tp& __x) _NOEXCEPT
84{
85 return (_Tp*)&reinterpret_cast<const volatile char&>(__x);
86}
87
88#if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
89// Objective-C++ Automatic Reference Counting uses qualified pointers
90// that require special addressof() signatures. When
91// _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
92// itself is providing these definitions. Otherwise, we provide them.
93template <class _Tp>
94inline _LIBCPP_INLINE_VISIBILITY
95__strong _Tp*
96addressof(__strong _Tp& __x) _NOEXCEPT
97{
98 return &__x;
99}
100
101#ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
102template <class _Tp>
103inline _LIBCPP_INLINE_VISIBILITY
104__weak _Tp*
105addressof(__weak _Tp& __x) _NOEXCEPT
106{
107 return &__x;
108}
109#endif
110
111template <class _Tp>
112inline _LIBCPP_INLINE_VISIBILITY
113__autoreleasing _Tp*
114addressof(__autoreleasing _Tp& __x) _NOEXCEPT
115{
116 return &__x;
117}
118
119template <class _Tp>
120inline _LIBCPP_INLINE_VISIBILITY
121__unsafe_unretained _Tp*
122addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
123{
124 return &__x;
125}
126#endif
127
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128#ifdef _LIBCPP_HAS_NO_VARIADICS
129
130#include <__functional_base_03>
131
132#else // _LIBCPP_HAS_NO_VARIADICS
133
134// __weak_result_type
135
136template <class _Tp>
137struct __derives_from_unary_function
138{
139private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000140 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141 static __two __test(...);
Howard Hinnant99968442011-11-29 18:15:50 +0000142 template <class _Ap, class _Rp>
143 static unary_function<_Ap, _Rp>
144 __test(const volatile unary_function<_Ap, _Rp>*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000145public:
146 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
147 typedef decltype(__test((_Tp*)0)) type;
148};
149
150template <class _Tp>
151struct __derives_from_binary_function
152{
153private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000154 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000155 static __two __test(...);
Howard Hinnant99968442011-11-29 18:15:50 +0000156 template <class _A1, class _A2, class _Rp>
157 static binary_function<_A1, _A2, _Rp>
158 __test(const volatile binary_function<_A1, _A2, _Rp>*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159public:
160 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
161 typedef decltype(__test((_Tp*)0)) type;
162};
163
164template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
165struct __maybe_derive_from_unary_function // bool is true
166 : public __derives_from_unary_function<_Tp>::type
167{
168};
169
170template <class _Tp>
171struct __maybe_derive_from_unary_function<_Tp, false>
172{
173};
174
175template <class _Tp, bool = __derives_from_binary_function<_Tp>::value>
176struct __maybe_derive_from_binary_function // bool is true
177 : public __derives_from_binary_function<_Tp>::type
178{
179};
180
181template <class _Tp>
182struct __maybe_derive_from_binary_function<_Tp, false>
183{
184};
185
186template <class _Tp, bool = __has_result_type<_Tp>::value>
187struct __weak_result_type_imp // bool is true
188 : public __maybe_derive_from_unary_function<_Tp>,
189 public __maybe_derive_from_binary_function<_Tp>
190{
191 typedef typename _Tp::result_type result_type;
192};
193
194template <class _Tp>
195struct __weak_result_type_imp<_Tp, false>
196 : public __maybe_derive_from_unary_function<_Tp>,
197 public __maybe_derive_from_binary_function<_Tp>
198{
199};
200
201template <class _Tp>
202struct __weak_result_type
203 : public __weak_result_type_imp<_Tp>
204{
205};
206
207// 0 argument case
208
Howard Hinnant99968442011-11-29 18:15:50 +0000209template <class _Rp>
210struct __weak_result_type<_Rp ()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000211{
Howard Hinnant99968442011-11-29 18:15:50 +0000212 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213};
214
Howard Hinnant99968442011-11-29 18:15:50 +0000215template <class _Rp>
216struct __weak_result_type<_Rp (&)()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217{
Howard Hinnant99968442011-11-29 18:15:50 +0000218 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000219};
220
Howard Hinnant99968442011-11-29 18:15:50 +0000221template <class _Rp>
222struct __weak_result_type<_Rp (*)()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223{
Howard Hinnant99968442011-11-29 18:15:50 +0000224 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000225};
226
227// 1 argument case
228
Howard Hinnant99968442011-11-29 18:15:50 +0000229template <class _Rp, class _A1>
230struct __weak_result_type<_Rp (_A1)>
231 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000232{
233};
234
Howard Hinnant99968442011-11-29 18:15:50 +0000235template <class _Rp, class _A1>
236struct __weak_result_type<_Rp (&)(_A1)>
237 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000238{
239};
240
Howard Hinnant99968442011-11-29 18:15:50 +0000241template <class _Rp, class _A1>
242struct __weak_result_type<_Rp (*)(_A1)>
243 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000244{
245};
246
Howard Hinnant99968442011-11-29 18:15:50 +0000247template <class _Rp, class _Cp>
248struct __weak_result_type<_Rp (_Cp::*)()>
249 : public unary_function<_Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000250{
251};
252
Howard Hinnant99968442011-11-29 18:15:50 +0000253template <class _Rp, class _Cp>
254struct __weak_result_type<_Rp (_Cp::*)() const>
255 : public unary_function<const _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256{
257};
258
Howard Hinnant99968442011-11-29 18:15:50 +0000259template <class _Rp, class _Cp>
260struct __weak_result_type<_Rp (_Cp::*)() volatile>
261 : public unary_function<volatile _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262{
263};
264
Howard Hinnant99968442011-11-29 18:15:50 +0000265template <class _Rp, class _Cp>
266struct __weak_result_type<_Rp (_Cp::*)() const volatile>
267 : public unary_function<const volatile _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268{
269};
270
271// 2 argument case
272
Howard Hinnant99968442011-11-29 18:15:50 +0000273template <class _Rp, class _A1, class _A2>
274struct __weak_result_type<_Rp (_A1, _A2)>
275 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276{
277};
278
Howard Hinnant99968442011-11-29 18:15:50 +0000279template <class _Rp, class _A1, class _A2>
280struct __weak_result_type<_Rp (*)(_A1, _A2)>
281 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000282{
283};
284
Howard Hinnant99968442011-11-29 18:15:50 +0000285template <class _Rp, class _A1, class _A2>
286struct __weak_result_type<_Rp (&)(_A1, _A2)>
287 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000288{
289};
290
Howard Hinnant99968442011-11-29 18:15:50 +0000291template <class _Rp, class _Cp, class _A1>
292struct __weak_result_type<_Rp (_Cp::*)(_A1)>
293 : public binary_function<_Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294{
295};
296
Howard Hinnant99968442011-11-29 18:15:50 +0000297template <class _Rp, class _Cp, class _A1>
298struct __weak_result_type<_Rp (_Cp::*)(_A1) const>
299 : public binary_function<const _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000300{
301};
302
Howard Hinnant99968442011-11-29 18:15:50 +0000303template <class _Rp, class _Cp, class _A1>
304struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile>
305 : public binary_function<volatile _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306{
307};
308
Howard Hinnant99968442011-11-29 18:15:50 +0000309template <class _Rp, class _Cp, class _A1>
310struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile>
311 : public binary_function<const volatile _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000312{
313};
314
315// 3 or more arguments
316
Howard Hinnant99968442011-11-29 18:15:50 +0000317template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
318struct __weak_result_type<_Rp (_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000319{
Howard Hinnant99968442011-11-29 18:15:50 +0000320 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321};
322
Howard Hinnant99968442011-11-29 18:15:50 +0000323template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
324struct __weak_result_type<_Rp (&)(_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325{
Howard Hinnant99968442011-11-29 18:15:50 +0000326 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327};
328
Howard Hinnant99968442011-11-29 18:15:50 +0000329template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
330struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000331{
Howard Hinnant99968442011-11-29 18:15:50 +0000332 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333};
334
Howard Hinnant99968442011-11-29 18:15:50 +0000335template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
336struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337{
Howard Hinnant99968442011-11-29 18:15:50 +0000338 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000339};
340
Howard Hinnant99968442011-11-29 18:15:50 +0000341template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
342struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000343{
Howard Hinnant99968442011-11-29 18:15:50 +0000344 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000345};
346
Howard Hinnant99968442011-11-29 18:15:50 +0000347template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
348struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) volatile>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000349{
Howard Hinnant99968442011-11-29 18:15:50 +0000350 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351};
352
Howard Hinnant99968442011-11-29 18:15:50 +0000353template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
354struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const volatile>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355{
Howard Hinnant99968442011-11-29 18:15:50 +0000356 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357};
358
359// __invoke
360
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000361// bullets 1 and 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000362
Howard Hinnantecc97422013-05-07 23:40:12 +0000363template <class _Fp, class _A0, class ..._Args,
364 class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000366auto
Howard Hinnant99968442011-11-29 18:15:50 +0000367__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000368 -> decltype((_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000370 return (_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371}
372
Howard Hinnantecc97422013-05-07 23:40:12 +0000373template <class _Fp, class _A0, class ..._Args,
374 class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000376auto
Howard Hinnant99968442011-11-29 18:15:50 +0000377__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000378 -> decltype(((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000380 return ((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381}
382
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000383// bullets 3 and 4
384
Howard Hinnantecc97422013-05-07 23:40:12 +0000385template <class _Fp, class _A0,
386 class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000388auto
Howard Hinnant99968442011-11-29 18:15:50 +0000389__invoke(_Fp&& __f, _A0&& __a0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000390 -> decltype(_VSTD::forward<_A0>(__a0).*__f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000392 return _VSTD::forward<_A0>(__a0).*__f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000393}
394
Howard Hinnantecc97422013-05-07 23:40:12 +0000395template <class _Fp, class _A0,
396 class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000397inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000398auto
Howard Hinnant99968442011-11-29 18:15:50 +0000399__invoke(_Fp&& __f, _A0&& __a0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000400 -> decltype((*_VSTD::forward<_A0>(__a0)).*__f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000402 return (*_VSTD::forward<_A0>(__a0)).*__f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403}
404
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000405// bullet 5
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000406
Howard Hinnant99968442011-11-29 18:15:50 +0000407template <class _Fp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000409auto
Howard Hinnant99968442011-11-29 18:15:50 +0000410__invoke(_Fp&& __f, _Args&& ...__args)
411 -> decltype(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000412{
Howard Hinnant99968442011-11-29 18:15:50 +0000413 return _VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000414}
415
416template <class _Tp, class ..._Args>
417struct __invoke_return
418{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000419 typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000420};
421
Eric Fiselierc3231d22015-02-10 16:48:45 +0000422template <class _Ret>
423struct __invoke_void_return_wrapper
424{
425 template <class ..._Args>
426 static _Ret __call(_Args&&... __args)
427 {
428 return __invoke(_VSTD::forward<_Args>(__args)...);
429 }
430};
431
432template <>
433struct __invoke_void_return_wrapper<void>
434{
435 template <class ..._Args>
436 static void __call(_Args&&... __args)
437 {
438 __invoke(_VSTD::forward<_Args>(__args)...);
439 }
440};
441
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000443class _LIBCPP_TYPE_VIS_ONLY reference_wrapper
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444 : public __weak_result_type<_Tp>
445{
446public:
447 // types
448 typedef _Tp type;
449private:
450 type* __f_;
451
452public:
453 // construct/copy/destroy
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000454 _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT
455 : __f_(_VSTD::addressof(__f)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000456#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457 private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
458#endif
459
460 // access
Howard Hinnant603d2c02011-05-28 17:59:48 +0000461 _LIBCPP_INLINE_VISIBILITY operator type& () const _NOEXCEPT {return *__f_;}
462 _LIBCPP_INLINE_VISIBILITY type& get() const _NOEXCEPT {return *__f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000463
464 // invoke
465 template <class... _ArgTypes>
Howard Hinnant99acc502010-09-21 17:32:39 +0000466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57cff292011-05-19 15:05:04 +0000467 typename __invoke_of<type&, _ArgTypes...>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000468 operator() (_ArgTypes&&... __args) const
469 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000470 return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471 }
472};
473
Marshall Clow0ea7f8c2014-01-06 14:00:09 +0000474template <class _Tp> struct __is_reference_wrapper_impl : public false_type {};
475template <class _Tp> struct __is_reference_wrapper_impl<reference_wrapper<_Tp> > : public true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476template <class _Tp> struct __is_reference_wrapper
Marshall Clow0ea7f8c2014-01-06 14:00:09 +0000477 : public __is_reference_wrapper_impl<typename remove_cv<_Tp>::type> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478
479template <class _Tp>
480inline _LIBCPP_INLINE_VISIBILITY
481reference_wrapper<_Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000482ref(_Tp& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000483{
484 return reference_wrapper<_Tp>(__t);
485}
486
487template <class _Tp>
488inline _LIBCPP_INLINE_VISIBILITY
489reference_wrapper<_Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000490ref(reference_wrapper<_Tp> __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491{
492 return ref(__t.get());
493}
494
495template <class _Tp>
496inline _LIBCPP_INLINE_VISIBILITY
497reference_wrapper<const _Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000498cref(const _Tp& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000499{
500 return reference_wrapper<const _Tp>(__t);
501}
502
503template <class _Tp>
504inline _LIBCPP_INLINE_VISIBILITY
505reference_wrapper<const _Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000506cref(reference_wrapper<_Tp> __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000507{
508 return cref(__t.get());
509}
510
Howard Hinnant73d21a42010-09-04 23:28:19 +0000511#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
512#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
513
Howard Hinnantec3773c2011-12-01 20:21:04 +0000514template <class _Tp> void ref(const _Tp&&) = delete;
515template <class _Tp> void cref(const _Tp&&) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000516
517#else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
518
Howard Hinnantec3773c2011-12-01 20:21:04 +0000519template <class _Tp> void ref(const _Tp&&);// = delete;
520template <class _Tp> void cref(const _Tp&&);// = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000521
522#endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
523
524#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000525
526#endif // _LIBCPP_HAS_NO_VARIADICS
527
Marshall Clow4a0a9812013-08-13 01:11:06 +0000528#if _LIBCPP_STD_VER > 11
529template <class _Tp1, class _Tp2 = void>
530struct __is_transparent
531{
532private:
533 struct __two {char __lx; char __lxx;};
534 template <class _Up> static __two __test(...);
535 template <class _Up> static char __test(typename _Up::is_transparent* = 0);
536public:
537 static const bool value = sizeof(__test<_Tp1>(0)) == 1;
538};
539#endif
540
Marshall Clow599e60d2013-09-12 02:11:16 +0000541// allocator_arg_t
542
543struct _LIBCPP_TYPE_VIS_ONLY allocator_arg_t { };
544
545#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MEMORY)
546extern const allocator_arg_t allocator_arg;
547#else
548constexpr allocator_arg_t allocator_arg = allocator_arg_t();
549#endif
550
551// uses_allocator
552
553template <class _Tp>
554struct __has_allocator_type
555{
556private:
557 struct __two {char __lx; char __lxx;};
558 template <class _Up> static __two __test(...);
559 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
560public:
561 static const bool value = sizeof(__test<_Tp>(0)) == 1;
562};
563
564template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
565struct __uses_allocator
566 : public integral_constant<bool,
567 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
568{
569};
570
571template <class _Tp, class _Alloc>
572struct __uses_allocator<_Tp, _Alloc, false>
573 : public false_type
574{
575};
576
577template <class _Tp, class _Alloc>
578struct _LIBCPP_TYPE_VIS_ONLY uses_allocator
579 : public __uses_allocator<_Tp, _Alloc>
580{
581};
582
583#ifndef _LIBCPP_HAS_NO_VARIADICS
584
585// allocator construction
586
587template <class _Tp, class _Alloc, class ..._Args>
588struct __uses_alloc_ctor_imp
589{
590 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
591 static const bool __ic =
592 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
593 static const int value = __ua ? 2 - __ic : 0;
594};
595
596template <class _Tp, class _Alloc, class ..._Args>
597struct __uses_alloc_ctor
598 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
599 {};
600
601template <class _Tp, class _Allocator, class... _Args>
602inline _LIBCPP_INLINE_VISIBILITY
603void __user_alloc_construct_impl (integral_constant<int, 0>, _Tp *__storage, const _Allocator &, _Args &&... __args )
604{
605 new (__storage) _Tp (_VSTD::forward<_Args>(__args)...);
606}
607
608template <class _Tp, class _Allocator, class... _Args>
609inline _LIBCPP_INLINE_VISIBILITY
610void __user_alloc_construct_impl (integral_constant<int, 1>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
611{
612 new (__storage) _Tp (allocator_arg, __a, _VSTD::forward<_Args>(__args)...);
613}
614
615template <class _Tp, class _Allocator, class... _Args>
616inline _LIBCPP_INLINE_VISIBILITY
617void __user_alloc_construct_impl (integral_constant<int, 2>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
618{
619 new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a);
620}
621
622template <class _Tp, class _Allocator, class... _Args>
623inline _LIBCPP_INLINE_VISIBILITY
624void __user_alloc_construct (_Tp *__storage, const _Allocator &__a, _Args &&... __args)
625{
626 __user_alloc_construct_impl(
627 __uses_alloc_ctor<_Tp, _Allocator>(),
628 __storage, __a, _VSTD::forward<_Args>(__args)...
629 );
630}
631#endif // _LIBCPP_HAS_NO_VARIADICS
Marshall Clow4a0a9812013-08-13 01:11:06 +0000632
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633_LIBCPP_END_NAMESPACE_STD
634
635#endif // _LIBCPP_FUNCTIONAL_BASE