blob: 8927f9d7f3736f4de64fbeba047deab8377e3ed4 [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{
61 _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
62 {return __x < __y;}
63};
64
Marshall Clowff464092013-07-29 14:21:53 +000065#if _LIBCPP_STD_VER > 11
66template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000067struct _LIBCPP_TYPE_VIS_ONLY less<void>
Marshall Clowff464092013-07-29 14:21:53 +000068{
69 template <class _T1, class _T2> _LIBCPP_INLINE_VISIBILITY
70 auto operator()(_T1&& __t, _T2&& __u) const
71 { return _VSTD::forward<_T1>(__t) < _VSTD::forward<_T2>(__u); }
Marshall Clow4a0a9812013-08-13 01:11:06 +000072 typedef void is_transparent;
Marshall Clowff464092013-07-29 14:21:53 +000073};
74#endif
75
Howard Hinnanta4e87ab2013-08-08 18:38:55 +000076// addressof
77
78template <class _Tp>
79inline _LIBCPP_INLINE_VISIBILITY
80_Tp*
81addressof(_Tp& __x) _NOEXCEPT
82{
83 return (_Tp*)&reinterpret_cast<const volatile char&>(__x);
84}
85
86#if defined(_LIBCPP_HAS_OBJC_ARC) && !defined(_LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF)
87// Objective-C++ Automatic Reference Counting uses qualified pointers
88// that require special addressof() signatures. When
89// _LIBCPP_PREDEFINED_OBJC_ARC_ADDRESSOF is defined, the compiler
90// itself is providing these definitions. Otherwise, we provide them.
91template <class _Tp>
92inline _LIBCPP_INLINE_VISIBILITY
93__strong _Tp*
94addressof(__strong _Tp& __x) _NOEXCEPT
95{
96 return &__x;
97}
98
99#ifdef _LIBCPP_HAS_OBJC_ARC_WEAK
100template <class _Tp>
101inline _LIBCPP_INLINE_VISIBILITY
102__weak _Tp*
103addressof(__weak _Tp& __x) _NOEXCEPT
104{
105 return &__x;
106}
107#endif
108
109template <class _Tp>
110inline _LIBCPP_INLINE_VISIBILITY
111__autoreleasing _Tp*
112addressof(__autoreleasing _Tp& __x) _NOEXCEPT
113{
114 return &__x;
115}
116
117template <class _Tp>
118inline _LIBCPP_INLINE_VISIBILITY
119__unsafe_unretained _Tp*
120addressof(__unsafe_unretained _Tp& __x) _NOEXCEPT
121{
122 return &__x;
123}
124#endif
125
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000126#ifdef _LIBCPP_HAS_NO_VARIADICS
127
128#include <__functional_base_03>
129
130#else // _LIBCPP_HAS_NO_VARIADICS
131
132// __weak_result_type
133
134template <class _Tp>
135struct __derives_from_unary_function
136{
137private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000138 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000139 static __two __test(...);
Howard Hinnant99968442011-11-29 18:15:50 +0000140 template <class _Ap, class _Rp>
141 static unary_function<_Ap, _Rp>
142 __test(const volatile unary_function<_Ap, _Rp>*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000143public:
144 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
145 typedef decltype(__test((_Tp*)0)) type;
146};
147
148template <class _Tp>
149struct __derives_from_binary_function
150{
151private:
Howard Hinnant9c0df142012-10-30 19:06:59 +0000152 struct __two {char __lx; char __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000153 static __two __test(...);
Howard Hinnant99968442011-11-29 18:15:50 +0000154 template <class _A1, class _A2, class _Rp>
155 static binary_function<_A1, _A2, _Rp>
156 __test(const volatile binary_function<_A1, _A2, _Rp>*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000157public:
158 static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
159 typedef decltype(__test((_Tp*)0)) type;
160};
161
162template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
163struct __maybe_derive_from_unary_function // bool is true
164 : public __derives_from_unary_function<_Tp>::type
165{
166};
167
168template <class _Tp>
169struct __maybe_derive_from_unary_function<_Tp, false>
170{
171};
172
173template <class _Tp, bool = __derives_from_binary_function<_Tp>::value>
174struct __maybe_derive_from_binary_function // bool is true
175 : public __derives_from_binary_function<_Tp>::type
176{
177};
178
179template <class _Tp>
180struct __maybe_derive_from_binary_function<_Tp, false>
181{
182};
183
184template <class _Tp, bool = __has_result_type<_Tp>::value>
185struct __weak_result_type_imp // bool is true
186 : public __maybe_derive_from_unary_function<_Tp>,
187 public __maybe_derive_from_binary_function<_Tp>
188{
189 typedef typename _Tp::result_type result_type;
190};
191
192template <class _Tp>
193struct __weak_result_type_imp<_Tp, false>
194 : public __maybe_derive_from_unary_function<_Tp>,
195 public __maybe_derive_from_binary_function<_Tp>
196{
197};
198
199template <class _Tp>
200struct __weak_result_type
201 : public __weak_result_type_imp<_Tp>
202{
203};
204
205// 0 argument case
206
Howard Hinnant99968442011-11-29 18:15:50 +0000207template <class _Rp>
208struct __weak_result_type<_Rp ()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209{
Howard Hinnant99968442011-11-29 18:15:50 +0000210 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000211};
212
Howard Hinnant99968442011-11-29 18:15:50 +0000213template <class _Rp>
214struct __weak_result_type<_Rp (&)()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000215{
Howard Hinnant99968442011-11-29 18:15:50 +0000216 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217};
218
Howard Hinnant99968442011-11-29 18:15:50 +0000219template <class _Rp>
220struct __weak_result_type<_Rp (*)()>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000221{
Howard Hinnant99968442011-11-29 18:15:50 +0000222 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223};
224
225// 1 argument case
226
Howard Hinnant99968442011-11-29 18:15:50 +0000227template <class _Rp, class _A1>
228struct __weak_result_type<_Rp (_A1)>
229 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000230{
231};
232
Howard Hinnant99968442011-11-29 18:15:50 +0000233template <class _Rp, class _A1>
234struct __weak_result_type<_Rp (&)(_A1)>
235 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000236{
237};
238
Howard Hinnant99968442011-11-29 18:15:50 +0000239template <class _Rp, class _A1>
240struct __weak_result_type<_Rp (*)(_A1)>
241 : public unary_function<_A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000242{
243};
244
Howard Hinnant99968442011-11-29 18:15:50 +0000245template <class _Rp, class _Cp>
246struct __weak_result_type<_Rp (_Cp::*)()>
247 : public unary_function<_Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248{
249};
250
Howard Hinnant99968442011-11-29 18:15:50 +0000251template <class _Rp, class _Cp>
252struct __weak_result_type<_Rp (_Cp::*)() const>
253 : public unary_function<const _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000254{
255};
256
Howard Hinnant99968442011-11-29 18:15:50 +0000257template <class _Rp, class _Cp>
258struct __weak_result_type<_Rp (_Cp::*)() volatile>
259 : public unary_function<volatile _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260{
261};
262
Howard Hinnant99968442011-11-29 18:15:50 +0000263template <class _Rp, class _Cp>
264struct __weak_result_type<_Rp (_Cp::*)() const volatile>
265 : public unary_function<const volatile _Cp*, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266{
267};
268
269// 2 argument case
270
Howard Hinnant99968442011-11-29 18:15:50 +0000271template <class _Rp, class _A1, class _A2>
272struct __weak_result_type<_Rp (_A1, _A2)>
273 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274{
275};
276
Howard Hinnant99968442011-11-29 18:15:50 +0000277template <class _Rp, class _A1, class _A2>
278struct __weak_result_type<_Rp (*)(_A1, _A2)>
279 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280{
281};
282
Howard Hinnant99968442011-11-29 18:15:50 +0000283template <class _Rp, class _A1, class _A2>
284struct __weak_result_type<_Rp (&)(_A1, _A2)>
285 : public binary_function<_A1, _A2, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286{
287};
288
Howard Hinnant99968442011-11-29 18:15:50 +0000289template <class _Rp, class _Cp, class _A1>
290struct __weak_result_type<_Rp (_Cp::*)(_A1)>
291 : public binary_function<_Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000292{
293};
294
Howard Hinnant99968442011-11-29 18:15:50 +0000295template <class _Rp, class _Cp, class _A1>
296struct __weak_result_type<_Rp (_Cp::*)(_A1) const>
297 : public binary_function<const _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000298{
299};
300
Howard Hinnant99968442011-11-29 18:15:50 +0000301template <class _Rp, class _Cp, class _A1>
302struct __weak_result_type<_Rp (_Cp::*)(_A1) volatile>
303 : public binary_function<volatile _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000304{
305};
306
Howard Hinnant99968442011-11-29 18:15:50 +0000307template <class _Rp, class _Cp, class _A1>
308struct __weak_result_type<_Rp (_Cp::*)(_A1) const volatile>
309 : public binary_function<const volatile _Cp*, _A1, _Rp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000310{
311};
312
313// 3 or more arguments
314
Howard Hinnant99968442011-11-29 18:15:50 +0000315template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
316struct __weak_result_type<_Rp (_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000317{
Howard Hinnant99968442011-11-29 18:15:50 +0000318 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000319};
320
Howard Hinnant99968442011-11-29 18:15:50 +0000321template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
322struct __weak_result_type<_Rp (&)(_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000323{
Howard Hinnant99968442011-11-29 18:15:50 +0000324 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325};
326
Howard Hinnant99968442011-11-29 18:15:50 +0000327template <class _Rp, class _A1, class _A2, class _A3, class ..._A4>
328struct __weak_result_type<_Rp (*)(_A1, _A2, _A3, _A4...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329{
Howard Hinnant99968442011-11-29 18:15:50 +0000330 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000331};
332
Howard Hinnant99968442011-11-29 18:15:50 +0000333template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
334struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...)>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000335{
Howard Hinnant99968442011-11-29 18:15:50 +0000336 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337};
338
Howard Hinnant99968442011-11-29 18:15:50 +0000339template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
340struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000341{
Howard Hinnant99968442011-11-29 18:15:50 +0000342 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000343};
344
Howard Hinnant99968442011-11-29 18:15:50 +0000345template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
346struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) volatile>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347{
Howard Hinnant99968442011-11-29 18:15:50 +0000348 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000349};
350
Howard Hinnant99968442011-11-29 18:15:50 +0000351template <class _Rp, class _Cp, class _A1, class _A2, class ..._A3>
352struct __weak_result_type<_Rp (_Cp::*)(_A1, _A2, _A3...) const volatile>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000353{
Howard Hinnant99968442011-11-29 18:15:50 +0000354 typedef _Rp result_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355};
356
357// __invoke
358
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000359// bullets 1 and 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000360
Howard Hinnantecc97422013-05-07 23:40:12 +0000361template <class _Fp, class _A0, class ..._Args,
362 class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000363inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000364auto
Howard Hinnant99968442011-11-29 18:15:50 +0000365__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000366 -> decltype((_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000367{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000368 return (_VSTD::forward<_A0>(__a0).*__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369}
370
Howard Hinnantecc97422013-05-07 23:40:12 +0000371template <class _Fp, class _A0, class ..._Args,
372 class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000374auto
Howard Hinnant99968442011-11-29 18:15:50 +0000375__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000376 -> decltype(((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000378 return ((*_VSTD::forward<_A0>(__a0)).*__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379}
380
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000381// bullets 3 and 4
382
Howard Hinnantecc97422013-05-07 23:40:12 +0000383template <class _Fp, class _A0,
384 class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000386auto
Howard Hinnant99968442011-11-29 18:15:50 +0000387__invoke(_Fp&& __f, _A0&& __a0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000388 -> decltype(_VSTD::forward<_A0>(__a0).*__f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000390 return _VSTD::forward<_A0>(__a0).*__f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391}
392
Howard Hinnantecc97422013-05-07 23:40:12 +0000393template <class _Fp, class _A0,
394 class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000396auto
Howard Hinnant99968442011-11-29 18:15:50 +0000397__invoke(_Fp&& __f, _A0&& __a0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000398 -> decltype((*_VSTD::forward<_A0>(__a0)).*__f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000399{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000400 return (*_VSTD::forward<_A0>(__a0)).*__f;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401}
402
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000403// bullet 5
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404
Howard Hinnant99968442011-11-29 18:15:50 +0000405template <class _Fp, class ..._Args>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000406inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd89e4b2011-05-20 22:02:53 +0000407auto
Howard Hinnant99968442011-11-29 18:15:50 +0000408__invoke(_Fp&& __f, _Args&& ...__args)
409 -> decltype(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000410{
Howard Hinnant99968442011-11-29 18:15:50 +0000411 return _VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000412}
413
414template <class _Tp, class ..._Args>
415struct __invoke_return
416{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000417 typedef decltype(__invoke(_VSTD::declval<_Tp>(), _VSTD::declval<_Args>()...)) type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000418};
419
420template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000421class _LIBCPP_TYPE_VIS_ONLY reference_wrapper
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000422 : public __weak_result_type<_Tp>
423{
424public:
425 // types
426 typedef _Tp type;
427private:
428 type* __f_;
429
430public:
431 // construct/copy/destroy
Howard Hinnanta4e87ab2013-08-08 18:38:55 +0000432 _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT
433 : __f_(_VSTD::addressof(__f)) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000434#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435 private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
436#endif
437
438 // access
Howard Hinnant603d2c02011-05-28 17:59:48 +0000439 _LIBCPP_INLINE_VISIBILITY operator type& () const _NOEXCEPT {return *__f_;}
440 _LIBCPP_INLINE_VISIBILITY type& get() const _NOEXCEPT {return *__f_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000441
442 // invoke
443 template <class... _ArgTypes>
Howard Hinnant99acc502010-09-21 17:32:39 +0000444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant57cff292011-05-19 15:05:04 +0000445 typename __invoke_of<type&, _ArgTypes...>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000446 operator() (_ArgTypes&&... __args) const
447 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000448 return __invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449 }
450};
451
452template <class _Tp> struct ____is_reference_wrapper : public false_type {};
453template <class _Tp> struct ____is_reference_wrapper<reference_wrapper<_Tp> > : public true_type {};
454template <class _Tp> struct __is_reference_wrapper
455 : public ____is_reference_wrapper<typename remove_cv<_Tp>::type> {};
456
457template <class _Tp>
458inline _LIBCPP_INLINE_VISIBILITY
459reference_wrapper<_Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000460ref(_Tp& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000461{
462 return reference_wrapper<_Tp>(__t);
463}
464
465template <class _Tp>
466inline _LIBCPP_INLINE_VISIBILITY
467reference_wrapper<_Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000468ref(reference_wrapper<_Tp> __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000469{
470 return ref(__t.get());
471}
472
473template <class _Tp>
474inline _LIBCPP_INLINE_VISIBILITY
475reference_wrapper<const _Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000476cref(const _Tp& __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000477{
478 return reference_wrapper<const _Tp>(__t);
479}
480
481template <class _Tp>
482inline _LIBCPP_INLINE_VISIBILITY
483reference_wrapper<const _Tp>
Howard Hinnant603d2c02011-05-28 17:59:48 +0000484cref(reference_wrapper<_Tp> __t) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000485{
486 return cref(__t.get());
487}
488
Howard Hinnant73d21a42010-09-04 23:28:19 +0000489#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
490#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
491
Howard Hinnantec3773c2011-12-01 20:21:04 +0000492template <class _Tp> void ref(const _Tp&&) = delete;
493template <class _Tp> void cref(const _Tp&&) = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000494
495#else // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
496
Howard Hinnantec3773c2011-12-01 20:21:04 +0000497template <class _Tp> void ref(const _Tp&&);// = delete;
498template <class _Tp> void cref(const _Tp&&);// = delete;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000499
500#endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
501
502#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503
504#endif // _LIBCPP_HAS_NO_VARIADICS
505
Marshall Clow4a0a9812013-08-13 01:11:06 +0000506#if _LIBCPP_STD_VER > 11
507template <class _Tp1, class _Tp2 = void>
508struct __is_transparent
509{
510private:
511 struct __two {char __lx; char __lxx;};
512 template <class _Up> static __two __test(...);
513 template <class _Up> static char __test(typename _Up::is_transparent* = 0);
514public:
515 static const bool value = sizeof(__test<_Tp1>(0)) == 1;
516};
517#endif
518
Marshall Clow599e60d2013-09-12 02:11:16 +0000519// allocator_arg_t
520
521struct _LIBCPP_TYPE_VIS_ONLY allocator_arg_t { };
522
523#if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MEMORY)
524extern const allocator_arg_t allocator_arg;
525#else
526constexpr allocator_arg_t allocator_arg = allocator_arg_t();
527#endif
528
529// uses_allocator
530
531template <class _Tp>
532struct __has_allocator_type
533{
534private:
535 struct __two {char __lx; char __lxx;};
536 template <class _Up> static __two __test(...);
537 template <class _Up> static char __test(typename _Up::allocator_type* = 0);
538public:
539 static const bool value = sizeof(__test<_Tp>(0)) == 1;
540};
541
542template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>
543struct __uses_allocator
544 : public integral_constant<bool,
545 is_convertible<_Alloc, typename _Tp::allocator_type>::value>
546{
547};
548
549template <class _Tp, class _Alloc>
550struct __uses_allocator<_Tp, _Alloc, false>
551 : public false_type
552{
553};
554
555template <class _Tp, class _Alloc>
556struct _LIBCPP_TYPE_VIS_ONLY uses_allocator
557 : public __uses_allocator<_Tp, _Alloc>
558{
559};
560
561#ifndef _LIBCPP_HAS_NO_VARIADICS
562
563// allocator construction
564
565template <class _Tp, class _Alloc, class ..._Args>
566struct __uses_alloc_ctor_imp
567{
568 static const bool __ua = uses_allocator<_Tp, _Alloc>::value;
569 static const bool __ic =
570 is_constructible<_Tp, allocator_arg_t, _Alloc, _Args...>::value;
571 static const int value = __ua ? 2 - __ic : 0;
572};
573
574template <class _Tp, class _Alloc, class ..._Args>
575struct __uses_alloc_ctor
576 : integral_constant<int, __uses_alloc_ctor_imp<_Tp, _Alloc, _Args...>::value>
577 {};
578
579template <class _Tp, class _Allocator, class... _Args>
580inline _LIBCPP_INLINE_VISIBILITY
581void __user_alloc_construct_impl (integral_constant<int, 0>, _Tp *__storage, const _Allocator &, _Args &&... __args )
582{
583 new (__storage) _Tp (_VSTD::forward<_Args>(__args)...);
584}
585
586template <class _Tp, class _Allocator, class... _Args>
587inline _LIBCPP_INLINE_VISIBILITY
588void __user_alloc_construct_impl (integral_constant<int, 1>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
589{
590 new (__storage) _Tp (allocator_arg, __a, _VSTD::forward<_Args>(__args)...);
591}
592
593template <class _Tp, class _Allocator, class... _Args>
594inline _LIBCPP_INLINE_VISIBILITY
595void __user_alloc_construct_impl (integral_constant<int, 2>, _Tp *__storage, const _Allocator &__a, _Args &&... __args )
596{
597 new (__storage) _Tp (_VSTD::forward<_Args>(__args)..., __a);
598}
599
600template <class _Tp, class _Allocator, class... _Args>
601inline _LIBCPP_INLINE_VISIBILITY
602void __user_alloc_construct (_Tp *__storage, const _Allocator &__a, _Args &&... __args)
603{
604 __user_alloc_construct_impl(
605 __uses_alloc_ctor<_Tp, _Allocator>(),
606 __storage, __a, _VSTD::forward<_Args>(__args)...
607 );
608}
609#endif // _LIBCPP_HAS_NO_VARIADICS
Marshall Clow4a0a9812013-08-13 01:11:06 +0000610
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611_LIBCPP_END_NAMESPACE_STD
612
613#endif // _LIBCPP_FUNCTIONAL_BASE