]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/functional
* many: Replace uses of __GXX_EXPERIMENTAL_CXX0X__ with __cplusplus.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / functional
CommitLineData
54c1bf78 1// <functional> -*- C++ -*-
de96ac46 2
12ffa228 3// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
28d5436f 4// 2011, 2012 Free Software Foundation, Inc.
de96ac46
BK
5//
6// This file is part of the GNU ISO C++ Library. This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
748086b7 9// Free Software Foundation; either version 3, or (at your option)
de96ac46
BK
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16
748086b7
JJ
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
de96ac46 20
748086b7
JJ
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24// <http://www.gnu.org/licenses/>.
de96ac46 25
54c1bf78
BK
26/*
27 * Copyright (c) 1997
28 * Silicon Graphics Computer Systems, Inc.
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 */
39
143c27b0 40/** @file include/functional
0aa06b18 41 * This is a Standard C++ Library header.
2f9d51b8
PE
42 */
43
1143680e
SE
44#ifndef _GLIBCXX_FUNCTIONAL
45#define _GLIBCXX_FUNCTIONAL 1
54c1bf78
BK
46
47#pragma GCC system_header
285b36d6 48
54c1bf78 49#include <bits/c++config.h>
54c1bf78
BK
50#include <bits/stl_function.h>
51
734f5023 52#if __cplusplus >= 201103L
7aec2c63
PC
53
54#include <typeinfo>
55#include <new>
56#include <tuple>
57#include <type_traits>
15d81a3c 58#include <bits/functexcept.h>
7aec2c63 59#include <bits/functional_hash.h>
7aec2c63 60
12ffa228
BK
61namespace std _GLIBCXX_VISIBILITY(default)
62{
63_GLIBCXX_BEGIN_NAMESPACE_VERSION
53dc5044 64
94df301f
JM
65 template<typename _MemberPointer>
66 class _Mem_fn;
67 template<typename _Tp, typename _Class>
68 _Mem_fn<_Tp _Class::*>
69 mem_fn(_Tp _Class::*);
70
033b71ce 71_GLIBCXX_HAS_NESTED_TYPE(result_type)
7aec2c63 72
7aec2c63
PC
73 /// If we have found a result_type, extract it.
74 template<bool _Has_result_type, typename _Functor>
75 struct _Maybe_get_result_type
76 { };
77
78 template<typename _Functor>
79 struct _Maybe_get_result_type<true, _Functor>
8cfaf4c9 80 { typedef typename _Functor::result_type result_type; };
7aec2c63
PC
81
82 /**
83 * Base class for any function object that has a weak result type, as
84 * defined in 3.3/3 of TR1.
85 */
86 template<typename _Functor>
87 struct _Weak_result_type_impl
033b71ce 88 : _Maybe_get_result_type<__has_result_type<_Functor>::value, _Functor>
8232dc64 89 { };
7aec2c63
PC
90
91 /// Retrieve the result type for a function type.
e9599233 92 template<typename _Res, typename... _ArgTypes>
7aec2c63 93 struct _Weak_result_type_impl<_Res(_ArgTypes...)>
8cfaf4c9
PC
94 { typedef _Res result_type; };
95
e9599233 96 template<typename _Res, typename... _ArgTypes>
8cfaf4c9
PC
97 struct _Weak_result_type_impl<_Res(_ArgTypes......)>
98 { typedef _Res result_type; };
99
e9599233 100 template<typename _Res, typename... _ArgTypes>
8cfaf4c9
PC
101 struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
102 { typedef _Res result_type; };
103
e9599233 104 template<typename _Res, typename... _ArgTypes>
8cfaf4c9
PC
105 struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
106 { typedef _Res result_type; };
107
e9599233 108 template<typename _Res, typename... _ArgTypes>
8cfaf4c9
PC
109 struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
110 { typedef _Res result_type; };
111
e9599233 112 template<typename _Res, typename... _ArgTypes>
8cfaf4c9
PC
113 struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
114 { typedef _Res result_type; };
115
e9599233 116 template<typename _Res, typename... _ArgTypes>
8cfaf4c9
PC
117 struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
118 { typedef _Res result_type; };
119
e9599233 120 template<typename _Res, typename... _ArgTypes>
8cfaf4c9
PC
121 struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
122 { typedef _Res result_type; };
7aec2c63
PC
123
124 /// Retrieve the result type for a function reference.
e9599233 125 template<typename _Res, typename... _ArgTypes>
7aec2c63 126 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
8cfaf4c9
PC
127 { typedef _Res result_type; };
128
e9599233 129 template<typename _Res, typename... _ArgTypes>
8cfaf4c9
PC
130 struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
131 { typedef _Res result_type; };
7aec2c63
PC
132
133 /// Retrieve the result type for a function pointer.
e9599233 134 template<typename _Res, typename... _ArgTypes>
7aec2c63 135 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
8cfaf4c9
PC
136 { typedef _Res result_type; };
137
e9599233 138 template<typename _Res, typename... _ArgTypes>
8cfaf4c9
PC
139 struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
140 { typedef _Res result_type; };
7aec2c63 141
e9599233
BK
142 /// Retrieve result type for a member function pointer.
143 template<typename _Res, typename _Class, typename... _ArgTypes>
7aec2c63 144 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
8cfaf4c9
PC
145 { typedef _Res result_type; };
146
e9599233 147 template<typename _Res, typename _Class, typename... _ArgTypes>
8cfaf4c9
PC
148 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
149 { typedef _Res result_type; };
7aec2c63 150
e9599233
BK
151 /// Retrieve result type for a const member function pointer.
152 template<typename _Res, typename _Class, typename... _ArgTypes>
7aec2c63 153 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
8cfaf4c9
PC
154 { typedef _Res result_type; };
155
e9599233 156 template<typename _Res, typename _Class, typename... _ArgTypes>
8cfaf4c9
PC
157 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
158 { typedef _Res result_type; };
7aec2c63 159
e9599233
BK
160 /// Retrieve result type for a volatile member function pointer.
161 template<typename _Res, typename _Class, typename... _ArgTypes>
7aec2c63 162 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
8cfaf4c9
PC
163 { typedef _Res result_type; };
164
e9599233 165 template<typename _Res, typename _Class, typename... _ArgTypes>
8cfaf4c9
PC
166 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
167 { typedef _Res result_type; };
7aec2c63 168
e9599233
BK
169 /// Retrieve result type for a const volatile member function pointer.
170 template<typename _Res, typename _Class, typename... _ArgTypes>
171 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
8cfaf4c9
PC
172 const volatile>
173 { typedef _Res result_type; };
174
e9599233 175 template<typename _Res, typename _Class, typename... _ArgTypes>
8cfaf4c9
PC
176 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
177 const volatile>
178 { typedef _Res result_type; };
7aec2c63
PC
179
180 /**
181 * Strip top-level cv-qualifiers from the function object and let
182 * _Weak_result_type_impl perform the real work.
183 */
184 template<typename _Functor>
185 struct _Weak_result_type
186 : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
8232dc64 187 { };
7aec2c63 188
7aec2c63
PC
189 /// Determines if the type _Tp derives from unary_function.
190 template<typename _Tp>
191 struct _Derives_from_unary_function : __sfinae_types
192 {
193 private:
194 template<typename _T1, typename _Res>
e9599233 195 static __one __test(const volatile unary_function<_T1, _Res>*);
7aec2c63
PC
196
197 // It's tempting to change "..." to const volatile void*, but
198 // that fails when _Tp is a function type.
199 static __two __test(...);
200
201 public:
202 static const bool value = sizeof(__test((_Tp*)0)) == 1;
203 };
204
205 /// Determines if the type _Tp derives from binary_function.
206 template<typename _Tp>
207 struct _Derives_from_binary_function : __sfinae_types
208 {
209 private:
210 template<typename _T1, typename _T2, typename _Res>
e9599233 211 static __one __test(const volatile binary_function<_T1, _T2, _Res>*);
7aec2c63
PC
212
213 // It's tempting to change "..." to const volatile void*, but
214 // that fails when _Tp is a function type.
215 static __two __test(...);
216
217 public:
218 static const bool value = sizeof(__test((_Tp*)0)) == 1;
219 };
220
7aec2c63
PC
221 /**
222 * Invoke a function object, which may be either a member pointer or a
223 * function object. The first parameter will tell which.
224 */
225 template<typename _Functor, typename... _Args>
226 inline
2b054f63 227 typename enable_if<
e9599233
BK
228 (!is_member_pointer<_Functor>::value
229 && !is_function<_Functor>::value
230 && !is_function<typename remove_pointer<_Functor>::type>::value),
be7f7822 231 typename result_of<_Functor(_Args&&...)>::type
e9599233 232 >::type
d90d97ff 233 __invoke(_Functor& __f, _Args&&... __args)
7aec2c63 234 {
d90d97ff 235 return __f(std::forward<_Args>(__args)...);
7aec2c63
PC
236 }
237
be7f7822
JW
238 template<typename _Functor, typename... _Args>
239 inline
240 typename enable_if<
241 (is_member_pointer<_Functor>::value
242 && !is_function<_Functor>::value
243 && !is_function<typename remove_pointer<_Functor>::type>::value),
244 typename result_of<_Functor(_Args&&...)>::type
245 >::type
246 __invoke(_Functor& __f, _Args&&... __args)
247 {
28d5436f 248 return std::mem_fn(__f)(std::forward<_Args>(__args)...);
be7f7822
JW
249 }
250
7aec2c63
PC
251 // To pick up function references (that will become function pointers)
252 template<typename _Functor, typename... _Args>
253 inline
2b054f63 254 typename enable_if<
e9599233
BK
255 (is_pointer<_Functor>::value
256 && is_function<typename remove_pointer<_Functor>::type>::value),
be7f7822 257 typename result_of<_Functor(_Args&&...)>::type
e9599233 258 >::type
d90d97ff 259 __invoke(_Functor __f, _Args&&... __args)
7aec2c63 260 {
d90d97ff 261 return __f(std::forward<_Args>(__args)...);
7aec2c63
PC
262 }
263
264 /**
265 * Knowing which of unary_function and binary_function _Tp derives
266 * from, derives from the same and ensures that reference_wrapper
267 * will have a weak result type. See cases below.
268 */
269 template<bool _Unary, bool _Binary, typename _Tp>
270 struct _Reference_wrapper_base_impl;
271
be7f7822 272 // None of the nested argument types.
7aec2c63
PC
273 template<typename _Tp>
274 struct _Reference_wrapper_base_impl<false, false, _Tp>
275 : _Weak_result_type<_Tp>
276 { };
277
be7f7822 278 // Nested argument_type only.
7aec2c63
PC
279 template<typename _Tp>
280 struct _Reference_wrapper_base_impl<true, false, _Tp>
be7f7822
JW
281 : _Weak_result_type<_Tp>
282 {
283 typedef typename _Tp::argument_type argument_type;
284 };
7aec2c63 285
be7f7822 286 // Nested first_argument_type and second_argument_type only.
7aec2c63
PC
287 template<typename _Tp>
288 struct _Reference_wrapper_base_impl<false, true, _Tp>
be7f7822
JW
289 : _Weak_result_type<_Tp>
290 {
291 typedef typename _Tp::first_argument_type first_argument_type;
292 typedef typename _Tp::second_argument_type second_argument_type;
293 };
7aec2c63 294
be7f7822 295 // All the nested argument types.
7aec2c63
PC
296 template<typename _Tp>
297 struct _Reference_wrapper_base_impl<true, true, _Tp>
be7f7822 298 : _Weak_result_type<_Tp>
7aec2c63 299 {
be7f7822
JW
300 typedef typename _Tp::argument_type argument_type;
301 typedef typename _Tp::first_argument_type first_argument_type;
302 typedef typename _Tp::second_argument_type second_argument_type;
7aec2c63
PC
303 };
304
be7f7822
JW
305 _GLIBCXX_HAS_NESTED_TYPE(argument_type)
306 _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
307 _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)
308
7aec2c63
PC
309 /**
310 * Derives from unary_function or binary_function when it
311 * can. Specializations handle all of the easy cases. The primary
312 * template determines what to do with a class type, which may
313 * derive from both unary_function and binary_function.
314 */
315 template<typename _Tp>
316 struct _Reference_wrapper_base
317 : _Reference_wrapper_base_impl<
be7f7822
JW
318 __has_argument_type<_Tp>::value,
319 __has_first_argument_type<_Tp>::value
320 && __has_second_argument_type<_Tp>::value,
7aec2c63
PC
321 _Tp>
322 { };
323
324 // - a function type (unary)
325 template<typename _Res, typename _T1>
326 struct _Reference_wrapper_base<_Res(_T1)>
327 : unary_function<_T1, _Res>
328 { };
329
8cfaf4c9
PC
330 template<typename _Res, typename _T1>
331 struct _Reference_wrapper_base<_Res(_T1) const>
332 : unary_function<_T1, _Res>
333 { };
334
335 template<typename _Res, typename _T1>
336 struct _Reference_wrapper_base<_Res(_T1) volatile>
337 : unary_function<_T1, _Res>
338 { };
339
340 template<typename _Res, typename _T1>
341 struct _Reference_wrapper_base<_Res(_T1) const volatile>
342 : unary_function<_T1, _Res>
343 { };
344
7aec2c63
PC
345 // - a function type (binary)
346 template<typename _Res, typename _T1, typename _T2>
347 struct _Reference_wrapper_base<_Res(_T1, _T2)>
348 : binary_function<_T1, _T2, _Res>
349 { };
350
8cfaf4c9
PC
351 template<typename _Res, typename _T1, typename _T2>
352 struct _Reference_wrapper_base<_Res(_T1, _T2) const>
353 : binary_function<_T1, _T2, _Res>
354 { };
355
356 template<typename _Res, typename _T1, typename _T2>
357 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
358 : binary_function<_T1, _T2, _Res>
359 { };
360
361 template<typename _Res, typename _T1, typename _T2>
362 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
363 : binary_function<_T1, _T2, _Res>
364 { };
365
7aec2c63
PC
366 // - a function pointer type (unary)
367 template<typename _Res, typename _T1>
368 struct _Reference_wrapper_base<_Res(*)(_T1)>
369 : unary_function<_T1, _Res>
370 { };
371
372 // - a function pointer type (binary)
373 template<typename _Res, typename _T1, typename _T2>
374 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
375 : binary_function<_T1, _T2, _Res>
376 { };
377
378 // - a pointer to member function type (unary, no qualifiers)
379 template<typename _Res, typename _T1>
380 struct _Reference_wrapper_base<_Res (_T1::*)()>
381 : unary_function<_T1*, _Res>
382 { };
383
384 // - a pointer to member function type (binary, no qualifiers)
385 template<typename _Res, typename _T1, typename _T2>
386 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
387 : binary_function<_T1*, _T2, _Res>
388 { };
389
390 // - a pointer to member function type (unary, const)
391 template<typename _Res, typename _T1>
392 struct _Reference_wrapper_base<_Res (_T1::*)() const>
393 : unary_function<const _T1*, _Res>
394 { };
395
396 // - a pointer to member function type (binary, const)
397 template<typename _Res, typename _T1, typename _T2>
398 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
399 : binary_function<const _T1*, _T2, _Res>
400 { };
401
402 // - a pointer to member function type (unary, volatile)
403 template<typename _Res, typename _T1>
404 struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
405 : unary_function<volatile _T1*, _Res>
406 { };
407
408 // - a pointer to member function type (binary, volatile)
409 template<typename _Res, typename _T1, typename _T2>
410 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
411 : binary_function<volatile _T1*, _T2, _Res>
412 { };
413
414 // - a pointer to member function type (unary, const volatile)
415 template<typename _Res, typename _T1>
416 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
417 : unary_function<const volatile _T1*, _Res>
418 { };
419
420 // - a pointer to member function type (binary, const volatile)
421 template<typename _Res, typename _T1, typename _T2>
422 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
423 : binary_function<const volatile _T1*, _T2, _Res>
424 { };
425
0eb95b0d
BK
426 /**
427 * @brief Primary class template for reference_wrapper.
428 * @ingroup functors
429 * @{
430 */
7aec2c63
PC
431 template<typename _Tp>
432 class reference_wrapper
433 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
434 {
7aec2c63 435 _Tp* _M_data;
be7f7822 436
7aec2c63
PC
437 public:
438 typedef _Tp type;
439
6b14c6d7 440 reference_wrapper(_Tp& __indata) noexcept
882b3d5c 441 : _M_data(std::__addressof(__indata))
7aec2c63
PC
442 { }
443
d90d97ff
JW
444 reference_wrapper(_Tp&&) = delete;
445
6b14c6d7
PC
446 reference_wrapper(const reference_wrapper<_Tp>& __inref) noexcept
447 : _M_data(__inref._M_data)
7aec2c63
PC
448 { }
449
450 reference_wrapper&
6b14c6d7 451 operator=(const reference_wrapper<_Tp>& __inref) noexcept
7aec2c63 452 {
e9599233
BK
453 _M_data = __inref._M_data;
454 return *this;
7aec2c63
PC
455 }
456
6b14c6d7 457 operator _Tp&() const noexcept
7aec2c63
PC
458 { return this->get(); }
459
460 _Tp&
6b14c6d7 461 get() const noexcept
7aec2c63
PC
462 { return *_M_data; }
463
464 template<typename... _Args>
be7f7822 465 typename result_of<_Tp&(_Args&&...)>::type
e9599233
BK
466 operator()(_Args&&... __args) const
467 {
d90d97ff 468 return __invoke(get(), std::forward<_Args>(__args)...);
7aec2c63
PC
469 }
470 };
471
472
0eb95b0d 473 /// Denotes a reference should be taken to a variable.
7aec2c63
PC
474 template<typename _Tp>
475 inline reference_wrapper<_Tp>
6b14c6d7 476 ref(_Tp& __t) noexcept
7aec2c63
PC
477 { return reference_wrapper<_Tp>(__t); }
478
0eb95b0d 479 /// Denotes a const reference should be taken to a variable.
7aec2c63
PC
480 template<typename _Tp>
481 inline reference_wrapper<const _Tp>
6b14c6d7 482 cref(const _Tp& __t) noexcept
7aec2c63
PC
483 { return reference_wrapper<const _Tp>(__t); }
484
be7f7822
JW
485 template<typename _Tp>
486 void ref(const _Tp&&) = delete;
487
488 template<typename _Tp>
489 void cref(const _Tp&&) = delete;
490
0eb95b0d 491 /// Partial specialization.
7aec2c63
PC
492 template<typename _Tp>
493 inline reference_wrapper<_Tp>
6b14c6d7 494 ref(reference_wrapper<_Tp> __t) noexcept
7aec2c63
PC
495 { return ref(__t.get()); }
496
0eb95b0d 497 /// Partial specialization.
7aec2c63
PC
498 template<typename _Tp>
499 inline reference_wrapper<const _Tp>
6b14c6d7 500 cref(reference_wrapper<_Tp> __t) noexcept
7aec2c63
PC
501 { return cref(__t.get()); }
502
0eb95b0d
BK
503 // @} group functors
504
7aec2c63
PC
505 /**
506 * Derives from @c unary_function or @c binary_function, or perhaps
507 * nothing, depending on the number of arguments provided. The
508 * primary template is the basis case, which derives nothing.
509 */
e9599233 510 template<typename _Res, typename... _ArgTypes>
7aec2c63
PC
511 struct _Maybe_unary_or_binary_function { };
512
e9599233
BK
513 /// Derives from @c unary_function, as appropriate.
514 template<typename _Res, typename _T1>
7aec2c63
PC
515 struct _Maybe_unary_or_binary_function<_Res, _T1>
516 : std::unary_function<_T1, _Res> { };
517
e9599233
BK
518 /// Derives from @c binary_function, as appropriate.
519 template<typename _Res, typename _T1, typename _T2>
7aec2c63
PC
520 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
521 : std::binary_function<_T1, _T2, _Res> { };
522
523 /// Implementation of @c mem_fn for member function pointers.
524 template<typename _Res, typename _Class, typename... _ArgTypes>
525 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
526 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>
527 {
528 typedef _Res (_Class::*_Functor)(_ArgTypes...);
529
530 template<typename _Tp>
e9599233
BK
531 _Res
532 _M_call(_Tp& __object, const volatile _Class *,
533 _ArgTypes... __args) const
534 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
535
536 template<typename _Tp>
e9599233
BK
537 _Res
538 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
539 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
540
541 public:
542 typedef _Res result_type;
543
544 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
545
546 // Handle objects
547 _Res
548 operator()(_Class& __object, _ArgTypes... __args) const
fbcedacc 549 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
550
551 // Handle pointers
552 _Res
553 operator()(_Class* __object, _ArgTypes... __args) const
fbcedacc 554 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
555
556 // Handle smart pointers, references and pointers to derived
557 template<typename _Tp>
e9599233 558 _Res
7aec2c63 559 operator()(_Tp& __object, _ArgTypes... __args) const
e9599233
BK
560 {
561 return _M_call(__object, &__object,
562 std::forward<_ArgTypes>(__args)...);
563 }
7aec2c63
PC
564
565 private:
566 _Functor __pmf;
567 };
568
569 /// Implementation of @c mem_fn for const member function pointers.
570 template<typename _Res, typename _Class, typename... _ArgTypes>
571 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const>
e9599233 572 : public _Maybe_unary_or_binary_function<_Res, const _Class*,
7aec2c63
PC
573 _ArgTypes...>
574 {
575 typedef _Res (_Class::*_Functor)(_ArgTypes...) const;
576
577 template<typename _Tp>
e9599233
BK
578 _Res
579 _M_call(_Tp& __object, const volatile _Class *,
580 _ArgTypes... __args) const
581 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
582
583 template<typename _Tp>
e9599233
BK
584 _Res
585 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
586 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
587
588 public:
589 typedef _Res result_type;
590
591 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
592
593 // Handle objects
594 _Res
595 operator()(const _Class& __object, _ArgTypes... __args) const
fbcedacc 596 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
597
598 // Handle pointers
599 _Res
600 operator()(const _Class* __object, _ArgTypes... __args) const
fbcedacc 601 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
602
603 // Handle smart pointers, references and pointers to derived
604 template<typename _Tp>
e9599233
BK
605 _Res operator()(_Tp& __object, _ArgTypes... __args) const
606 {
607 return _M_call(__object, &__object,
608 std::forward<_ArgTypes>(__args)...);
609 }
7aec2c63
PC
610
611 private:
612 _Functor __pmf;
613 };
614
615 /// Implementation of @c mem_fn for volatile member function pointers.
616 template<typename _Res, typename _Class, typename... _ArgTypes>
617 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile>
e9599233 618 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*,
7aec2c63
PC
619 _ArgTypes...>
620 {
621 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile;
622
623 template<typename _Tp>
e9599233
BK
624 _Res
625 _M_call(_Tp& __object, const volatile _Class *,
626 _ArgTypes... __args) const
627 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
628
629 template<typename _Tp>
e9599233
BK
630 _Res
631 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
632 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
633
634 public:
635 typedef _Res result_type;
636
637 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
638
639 // Handle objects
640 _Res
641 operator()(volatile _Class& __object, _ArgTypes... __args) const
fbcedacc 642 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
643
644 // Handle pointers
645 _Res
646 operator()(volatile _Class* __object, _ArgTypes... __args) const
fbcedacc 647 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
648
649 // Handle smart pointers, references and pointers to derived
650 template<typename _Tp>
e9599233 651 _Res
7aec2c63 652 operator()(_Tp& __object, _ArgTypes... __args) const
e9599233
BK
653 {
654 return _M_call(__object, &__object,
655 std::forward<_ArgTypes>(__args)...);
656 }
7aec2c63
PC
657
658 private:
659 _Functor __pmf;
660 };
661
662 /// Implementation of @c mem_fn for const volatile member function pointers.
663 template<typename _Res, typename _Class, typename... _ArgTypes>
664 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile>
e9599233 665 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*,
7aec2c63
PC
666 _ArgTypes...>
667 {
668 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile;
669
670 template<typename _Tp>
e9599233
BK
671 _Res
672 _M_call(_Tp& __object, const volatile _Class *,
673 _ArgTypes... __args) const
674 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
675
676 template<typename _Tp>
e9599233
BK
677 _Res
678 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const
679 { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
680
681 public:
682 typedef _Res result_type;
683
684 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { }
685
686 // Handle objects
e9599233 687 _Res
7aec2c63 688 operator()(const volatile _Class& __object, _ArgTypes... __args) const
fbcedacc 689 { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
690
691 // Handle pointers
e9599233 692 _Res
7aec2c63 693 operator()(const volatile _Class* __object, _ArgTypes... __args) const
fbcedacc 694 { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); }
7aec2c63
PC
695
696 // Handle smart pointers, references and pointers to derived
697 template<typename _Tp>
e9599233
BK
698 _Res operator()(_Tp& __object, _ArgTypes... __args) const
699 {
700 return _M_call(__object, &__object,
701 std::forward<_ArgTypes>(__args)...);
702 }
7aec2c63
PC
703
704 private:
705 _Functor __pmf;
706 };
707
708
8cfaf4c9
PC
709 template<typename _Tp, bool>
710 struct _Mem_fn_const_or_non
711 {
712 typedef const _Tp& type;
713 };
714
715 template<typename _Tp>
716 struct _Mem_fn_const_or_non<_Tp, false>
717 {
718 typedef _Tp& type;
719 };
720
7aec2c63
PC
721 template<typename _Res, typename _Class>
722 class _Mem_fn<_Res _Class::*>
723 {
724 // This bit of genius is due to Peter Dimov, improved slightly by
725 // Douglas Gregor.
726 template<typename _Tp>
e9599233
BK
727 _Res&
728 _M_call(_Tp& __object, _Class *) const
729 { return __object.*__pm; }
7aec2c63
PC
730
731 template<typename _Tp, typename _Up>
e9599233
BK
732 _Res&
733 _M_call(_Tp& __object, _Up * const *) const
734 { return (*__object).*__pm; }
7aec2c63
PC
735
736 template<typename _Tp, typename _Up>
e9599233
BK
737 const _Res&
738 _M_call(_Tp& __object, const _Up * const *) const
739 { return (*__object).*__pm; }
7aec2c63
PC
740
741 template<typename _Tp>
e9599233
BK
742 const _Res&
743 _M_call(_Tp& __object, const _Class *) const
744 { return __object.*__pm; }
7aec2c63
PC
745
746 template<typename _Tp>
e9599233
BK
747 const _Res&
748 _M_call(_Tp& __ptr, const volatile void*) const
749 { return (*__ptr).*__pm; }
7aec2c63
PC
750
751 template<typename _Tp> static _Tp& __get_ref();
752
753 template<typename _Tp>
e9599233 754 static __sfinae_types::__one __check_const(_Tp&, _Class*);
7aec2c63 755 template<typename _Tp, typename _Up>
e9599233 756 static __sfinae_types::__one __check_const(_Tp&, _Up * const *);
7aec2c63 757 template<typename _Tp, typename _Up>
e9599233 758 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *);
7aec2c63 759 template<typename _Tp>
e9599233 760 static __sfinae_types::__two __check_const(_Tp&, const _Class*);
7aec2c63 761 template<typename _Tp>
e9599233 762 static __sfinae_types::__two __check_const(_Tp&, const volatile void*);
7aec2c63
PC
763
764 public:
765 template<typename _Tp>
e9599233 766 struct _Result_type
7aec2c63
PC
767 : _Mem_fn_const_or_non<_Res,
768 (sizeof(__sfinae_types::__two)
769 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))>
e9599233 770 { };
7aec2c63
PC
771
772 template<typename _Signature>
e9599233 773 struct result;
7aec2c63
PC
774
775 template<typename _CVMem, typename _Tp>
e9599233 776 struct result<_CVMem(_Tp)>
7aec2c63
PC
777 : public _Result_type<_Tp> { };
778
779 template<typename _CVMem, typename _Tp>
e9599233 780 struct result<_CVMem(_Tp&)>
7aec2c63
PC
781 : public _Result_type<_Tp> { };
782
783 explicit
784 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { }
785
786 // Handle objects
787 _Res&
788 operator()(_Class& __object) const
789 { return __object.*__pm; }
790
791 const _Res&
792 operator()(const _Class& __object) const
793 { return __object.*__pm; }
794
795 // Handle pointers
796 _Res&
797 operator()(_Class* __object) const
798 { return __object->*__pm; }
799
800 const _Res&
801 operator()(const _Class* __object) const
802 { return __object->*__pm; }
803
804 // Handle smart pointers and derived
805 template<typename _Tp>
e9599233
BK
806 typename _Result_type<_Tp>::type
807 operator()(_Tp& __unknown) const
808 { return _M_call(__unknown, &__unknown); }
7aec2c63
PC
809
810 private:
811 _Res _Class::*__pm;
812 };
813
814 /**
815 * @brief Returns a function object that forwards to the member
816 * pointer @a pm.
0eb95b0d 817 * @ingroup functors
7aec2c63
PC
818 */
819 template<typename _Tp, typename _Class>
820 inline _Mem_fn<_Tp _Class::*>
821 mem_fn(_Tp _Class::* __pm)
822 {
823 return _Mem_fn<_Tp _Class::*>(__pm);
824 }
825
826 /**
827 * @brief Determines if the given type _Tp is a function object
828 * should be treated as a subexpression when evaluating calls to
829 * function objects returned by bind(). [TR1 3.6.1]
0eb95b0d 830 * @ingroup binders
7aec2c63
PC
831 */
832 template<typename _Tp>
833 struct is_bind_expression
2b054f63 834 : public false_type { };
7aec2c63
PC
835
836 /**
837 * @brief Determines if the given type _Tp is a placeholder in a
838 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
0eb95b0d 839 * @ingroup binders
7aec2c63
PC
840 */
841 template<typename _Tp>
842 struct is_placeholder
2b054f63
DF
843 : public integral_constant<int, 0>
844 { };
7aec2c63 845
e02a5443
JW
846 /** @brief The type of placeholder objects defined by libstdc++.
847 * @ingroup binders
848 */
7aec2c63
PC
849 template<int _Num> struct _Placeholder { };
850
12ffa228
BK
851 _GLIBCXX_END_NAMESPACE_VERSION
852
7aec2c63 853 /** @namespace std::placeholders
e02a5443 854 * @brief ISO C++11 entities sub-namespace for functional.
0eb95b0d 855 * @ingroup binders
7aec2c63 856 */
e9599233 857 namespace placeholders
4192922c 858 {
12ffa228 859 _GLIBCXX_BEGIN_NAMESPACE_VERSION
e02a5443
JW
860 /* Define a large number of placeholders. There is no way to
861 * simplify this with variadic templates, because we're introducing
862 * unique names for each.
863 */
4192922c
PC
864 extern const _Placeholder<1> _1;
865 extern const _Placeholder<2> _2;
866 extern const _Placeholder<3> _3;
867 extern const _Placeholder<4> _4;
868 extern const _Placeholder<5> _5;
869 extern const _Placeholder<6> _6;
870 extern const _Placeholder<7> _7;
871 extern const _Placeholder<8> _8;
872 extern const _Placeholder<9> _9;
873 extern const _Placeholder<10> _10;
874 extern const _Placeholder<11> _11;
875 extern const _Placeholder<12> _12;
876 extern const _Placeholder<13> _13;
877 extern const _Placeholder<14> _14;
878 extern const _Placeholder<15> _15;
879 extern const _Placeholder<16> _16;
880 extern const _Placeholder<17> _17;
881 extern const _Placeholder<18> _18;
882 extern const _Placeholder<19> _19;
883 extern const _Placeholder<20> _20;
884 extern const _Placeholder<21> _21;
885 extern const _Placeholder<22> _22;
886 extern const _Placeholder<23> _23;
887 extern const _Placeholder<24> _24;
888 extern const _Placeholder<25> _25;
889 extern const _Placeholder<26> _26;
890 extern const _Placeholder<27> _27;
891 extern const _Placeholder<28> _28;
892 extern const _Placeholder<29> _29;
12ffa228 893 _GLIBCXX_END_NAMESPACE_VERSION
7aec2c63
PC
894 }
895
12ffa228
BK
896 _GLIBCXX_BEGIN_NAMESPACE_VERSION
897
7aec2c63
PC
898 /**
899 * Partial specialization of is_placeholder that provides the placeholder
900 * number for the placeholder objects defined by libstdc++.
0eb95b0d 901 * @ingroup binders
7aec2c63
PC
902 */
903 template<int _Num>
904 struct is_placeholder<_Placeholder<_Num> >
2b054f63
DF
905 : public integral_constant<int, _Num>
906 { };
7aec2c63 907
e02a5443
JW
908 template<int _Num>
909 struct is_placeholder<const _Placeholder<_Num> >
910 : public integral_constant<int, _Num>
911 { };
912
e9599233 913 /**
7aec2c63
PC
914 * Used by _Safe_tuple_element to indicate that there is no tuple
915 * element at this position.
916 */
917 struct _No_tuple_element;
918
919 /**
920 * Implementation helper for _Safe_tuple_element. This primary
921 * template handles the case where it is safe to use @c
922 * tuple_element.
923 */
049d2893 924 template<std::size_t __i, typename _Tuple, bool _IsSafe>
7aec2c63
PC
925 struct _Safe_tuple_element_impl
926 : tuple_element<__i, _Tuple> { };
927
928 /**
929 * Implementation helper for _Safe_tuple_element. This partial
930 * specialization handles the case where it is not safe to use @c
931 * tuple_element. We just return @c _No_tuple_element.
932 */
049d2893 933 template<std::size_t __i, typename _Tuple>
7aec2c63
PC
934 struct _Safe_tuple_element_impl<__i, _Tuple, false>
935 {
936 typedef _No_tuple_element type;
937 };
938
939 /**
940 * Like tuple_element, but returns @c _No_tuple_element when
941 * tuple_element would return an error.
942 */
049d2893 943 template<std::size_t __i, typename _Tuple>
7aec2c63 944 struct _Safe_tuple_element
e9599233 945 : _Safe_tuple_element_impl<__i, _Tuple,
049d2893 946 (__i < tuple_size<_Tuple>::value)>
8232dc64 947 { };
7aec2c63
PC
948
949 /**
950 * Maps an argument to bind() into an actual argument to the bound
951 * function object [TR1 3.6.3/5]. Only the first parameter should
952 * be specified: the rest are used to determine among the various
953 * implementations. Note that, although this class is a function
954 * object, it isn't entirely normal because it takes only two
955 * parameters regardless of the number of parameters passed to the
956 * bind expression. The first parameter is the bound argument and
957 * the second parameter is a tuple containing references to the
958 * rest of the arguments.
959 */
960 template<typename _Arg,
e9599233
BK
961 bool _IsBindExp = is_bind_expression<_Arg>::value,
962 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
7aec2c63
PC
963 class _Mu;
964
965 /**
966 * If the argument is reference_wrapper<_Tp>, returns the
967 * underlying reference. [TR1 3.6.3/5 bullet 1]
968 */
969 template<typename _Tp>
970 class _Mu<reference_wrapper<_Tp>, false, false>
971 {
972 public:
973 typedef _Tp& result_type;
974
975 /* Note: This won't actually work for const volatile
976 * reference_wrappers, because reference_wrapper::get() is const
977 * but not volatile-qualified. This might be a defect in the TR.
978 */
979 template<typename _CVRef, typename _Tuple>
e9599233
BK
980 result_type
981 operator()(_CVRef& __arg, _Tuple&) const volatile
982 { return __arg.get(); }
7aec2c63
PC
983 };
984
985 /**
986 * If the argument is a bind expression, we invoke the underlying
987 * function object with the same cv-qualifiers as we are given and
988 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
989 */
990 template<typename _Arg>
991 class _Mu<_Arg, true, false>
992 {
993 public:
7aec2c63 994 template<typename _CVArg, typename... _Args>
e9599233
BK
995 auto
996 operator()(_CVArg& __arg,
1b3fad81 997 tuple<_Args...>& __tuple) const volatile
e9599233
BK
998 -> decltype(__arg(declval<_Args>()...))
999 {
7aec2c63
PC
1000 // Construct an index tuple and forward to __call
1001 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
1002 _Indexes;
1b3fad81 1003 return this->__call(__arg, __tuple, _Indexes());
7aec2c63
PC
1004 }
1005
1006 private:
1007 // Invokes the underlying function object __arg by unpacking all
e9599233 1008 // of the arguments in the tuple.
049d2893 1009 template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
e9599233
BK
1010 auto
1011 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
7aec2c63 1012 const _Index_tuple<_Indexes...>&) const volatile
e9599233
BK
1013 -> decltype(__arg(declval<_Args>()...))
1014 {
d90d97ff 1015 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...);
7aec2c63
PC
1016 }
1017 };
1018
1019 /**
1020 * If the argument is a placeholder for the Nth argument, returns
1021 * a reference to the Nth argument to the bind function object.
1022 * [TR1 3.6.3/5 bullet 3]
1023 */
1024 template<typename _Arg>
1025 class _Mu<_Arg, false, true>
1026 {
1027 public:
1028 template<typename _Signature> class result;
1029
1030 template<typename _CVMu, typename _CVArg, typename _Tuple>
e9599233
BK
1031 class result<_CVMu(_CVArg, _Tuple)>
1032 {
7aec2c63
PC
1033 // Add a reference, if it hasn't already been done for us.
1034 // This allows us to be a little bit sloppy in constructing
1035 // the tuple that we pass to result_of<...>.
1036 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
1037 - 1), _Tuple>::type
1038 __base_type;
1039
1040 public:
d90d97ff 1041 typedef typename add_rvalue_reference<__base_type>::type type;
7aec2c63
PC
1042 };
1043
1044 template<typename _Tuple>
e9599233
BK
1045 typename result<_Mu(_Arg, _Tuple)>::type
1046 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
1047 {
d90d97ff 1048 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
e9599233 1049 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
7aec2c63
PC
1050 }
1051 };
1052
1053 /**
1054 * If the argument is just a value, returns a reference to that
1055 * value. The cv-qualifiers on the reference are the same as the
1056 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
1057 */
1058 template<typename _Arg>
1059 class _Mu<_Arg, false, false>
1060 {
1061 public:
1062 template<typename _Signature> struct result;
1063
1064 template<typename _CVMu, typename _CVArg, typename _Tuple>
e9599233
BK
1065 struct result<_CVMu(_CVArg, _Tuple)>
1066 {
7aec2c63
PC
1067 typedef typename add_lvalue_reference<_CVArg>::type type;
1068 };
1069
1070 // Pick up the cv-qualifiers of the argument
1071 template<typename _CVArg, typename _Tuple>
e9599233
BK
1072 _CVArg&&
1073 operator()(_CVArg&& __arg, _Tuple&) const volatile
1074 { return std::forward<_CVArg>(__arg); }
7aec2c63
PC
1075 };
1076
1077 /**
1078 * Maps member pointers into instances of _Mem_fn but leaves all
1079 * other function objects untouched. Used by tr1::bind(). The
1080 * primary template handles the non--member-pointer case.
1081 */
1082 template<typename _Tp>
1083 struct _Maybe_wrap_member_pointer
1084 {
1085 typedef _Tp type;
1b3fad81 1086
7aec2c63
PC
1087 static const _Tp&
1088 __do_wrap(const _Tp& __x)
1089 { return __x; }
1b3fad81
JW
1090
1091 static _Tp&&
1092 __do_wrap(_Tp&& __x)
1093 { return static_cast<_Tp&&>(__x); }
7aec2c63
PC
1094 };
1095
1096 /**
1097 * Maps member pointers into instances of _Mem_fn but leaves all
1098 * other function objects untouched. Used by tr1::bind(). This
1099 * partial specialization handles the member pointer case.
1100 */
1101 template<typename _Tp, typename _Class>
1102 struct _Maybe_wrap_member_pointer<_Tp _Class::*>
1103 {
1104 typedef _Mem_fn<_Tp _Class::*> type;
e9599233 1105
7aec2c63
PC
1106 static type
1107 __do_wrap(_Tp _Class::* __pm)
1108 { return type(__pm); }
1109 };
1110
c5a23752
JW
1111 // Specialization needed to prevent "forming reference to void" errors when
1112 // bind<void>() is called, because argument deduction instantiates
1113 // _Maybe_wrap_member_pointer<void> outside the immediate context where
1114 // SFINAE applies.
1115 template<>
1116 struct _Maybe_wrap_member_pointer<void>
1117 {
1118 typedef void type;
1119 };
1120
1b3fad81 1121 // std::get<I> for volatile-qualified tuples
049d2893 1122 template<std::size_t _Ind, typename... _Tp>
1b3fad81
JW
1123 inline auto
1124 __volget(volatile tuple<_Tp...>& __tuple)
1125 -> typename tuple_element<_Ind, tuple<_Tp...>>::type volatile&
1126 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
1127
1128 // std::get<I> for const-volatile-qualified tuples
049d2893 1129 template<std::size_t _Ind, typename... _Tp>
1b3fad81
JW
1130 inline auto
1131 __volget(const volatile tuple<_Tp...>& __tuple)
1132 -> typename tuple_element<_Ind, tuple<_Tp...>>::type const volatile&
1133 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
1134
7aec2c63
PC
1135 /// Type of the function object returned from bind().
1136 template<typename _Signature>
1137 struct _Bind;
1138
1139 template<typename _Functor, typename... _Bound_args>
1140 class _Bind<_Functor(_Bound_args...)>
1141 : public _Weak_result_type<_Functor>
1142 {
1143 typedef _Bind __self_type;
1b3fad81 1144 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
e9599233 1145 _Bound_indexes;
7aec2c63
PC
1146
1147 _Functor _M_f;
1148 tuple<_Bound_args...> _M_bound_args;
1149
1150 // Call unqualified
049d2893 1151 template<typename _Result, typename... _Args, std::size_t... _Indexes>
e9599233
BK
1152 _Result
1153 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
1154 {
1155 return _M_f(_Mu<_Bound_args>()
1156 (get<_Indexes>(_M_bound_args), __args)...);
1157 }
7aec2c63
PC
1158
1159 // Call as const
049d2893 1160 template<typename _Result, typename... _Args, std::size_t... _Indexes>
e9599233
BK
1161 _Result
1162 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
1163 {
1164 return _M_f(_Mu<_Bound_args>()
1165 (get<_Indexes>(_M_bound_args), __args)...);
1166 }
7aec2c63
PC
1167
1168 // Call as volatile
049d2893 1169 template<typename _Result, typename... _Args, std::size_t... _Indexes>
e9599233
BK
1170 _Result
1171 __call_v(tuple<_Args...>&& __args,
8232dc64 1172 _Index_tuple<_Indexes...>) volatile
e9599233
BK
1173 {
1174 return _M_f(_Mu<_Bound_args>()
1175 (__volget<_Indexes>(_M_bound_args), __args)...);
1176 }
7aec2c63
PC
1177
1178 // Call as const volatile
049d2893 1179 template<typename _Result, typename... _Args, std::size_t... _Indexes>
e9599233
BK
1180 _Result
1181 __call_c_v(tuple<_Args...>&& __args,
8232dc64 1182 _Index_tuple<_Indexes...>) const volatile
e9599233
BK
1183 {
1184 return _M_f(_Mu<_Bound_args>()
1185 (__volget<_Indexes>(_M_bound_args), __args)...);
1186 }
7aec2c63
PC
1187
1188 public:
1b3fad81 1189 template<typename... _Args>
e9599233
BK
1190 explicit _Bind(const _Functor& __f, _Args&&... __args)
1191 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1192 { }
1b3fad81
JW
1193
1194 template<typename... _Args>
e9599233
BK
1195 explicit _Bind(_Functor&& __f, _Args&&... __args)
1196 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1197 { }
1b3fad81
JW
1198
1199 _Bind(const _Bind&) = default;
1200
1201 _Bind(_Bind&& __b)
1202 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
d90d97ff 1203 { }
7aec2c63
PC
1204
1205 // Call unqualified
8232dc64 1206 template<typename... _Args, typename _Result
e9599233
BK
1207 = decltype( std::declval<_Functor>()(
1208 _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1b3fad81 1209 std::declval<tuple<_Args...>&>() )... ) )>
e9599233
BK
1210 _Result
1211 operator()(_Args&&... __args)
1212 {
1213 return this->__call<_Result>(
1214 std::forward_as_tuple(std::forward<_Args>(__args)...),
1215 _Bound_indexes());
1216 }
7aec2c63
PC
1217
1218 // Call as const
8232dc64 1219 template<typename... _Args, typename _Result
4702f095
PC
1220 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1221 typename add_const<_Functor>::type>::type>()(
8232dc64 1222 _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1b3fad81 1223 std::declval<tuple<_Args...>&>() )... ) )>
e9599233
BK
1224 _Result
1225 operator()(_Args&&... __args) const
1226 {
1227 return this->__call_c<_Result>(
1228 std::forward_as_tuple(std::forward<_Args>(__args)...),
1229 _Bound_indexes());
1230 }
7aec2c63 1231
7aec2c63 1232 // Call as volatile
8232dc64 1233 template<typename... _Args, typename _Result
4702f095
PC
1234 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1235 typename add_volatile<_Functor>::type>::type>()(
e9599233
BK
1236 _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
1237 std::declval<tuple<_Args...>&>() )... ) )>
1238 _Result
1239 operator()(_Args&&... __args) volatile
1240 {
1241 return this->__call_v<_Result>(
1242 std::forward_as_tuple(std::forward<_Args>(__args)...),
1243 _Bound_indexes());
1244 }
7aec2c63 1245
7aec2c63 1246 // Call as const volatile
8232dc64 1247 template<typename... _Args, typename _Result
4702f095
PC
1248 = decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
1249 typename add_cv<_Functor>::type>::type>()(
e9599233
BK
1250 _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
1251 std::declval<tuple<_Args...>&>() )... ) )>
1252 _Result
1253 operator()(_Args&&... __args) const volatile
1254 {
1255 return this->__call_c_v<_Result>(
1256 std::forward_as_tuple(std::forward<_Args>(__args)...),
1257 _Bound_indexes());
1258 }
7aec2c63
PC
1259 };
1260
1261 /// Type of the function object returned from bind<R>().
1262 template<typename _Result, typename _Signature>
1263 struct _Bind_result;
1264
1265 template<typename _Result, typename _Functor, typename... _Bound_args>
1266 class _Bind_result<_Result, _Functor(_Bound_args...)>
1267 {
1268 typedef _Bind_result __self_type;
e9599233
BK
1269 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1270 _Bound_indexes;
7aec2c63
PC
1271
1272 _Functor _M_f;
1273 tuple<_Bound_args...> _M_bound_args;
1274
c5a23752
JW
1275 // sfinae types
1276 template<typename _Res>
e9599233 1277 struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
c5a23752 1278 template<typename _Res>
e9599233 1279 struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
c5a23752 1280
7aec2c63 1281 // Call unqualified
049d2893 1282 template<typename _Res, typename... _Args, std::size_t... _Indexes>
e9599233
BK
1283 _Result
1284 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1285 typename __disable_if_void<_Res>::type = 0)
1286 {
1287 return _M_f(_Mu<_Bound_args>()
1288 (get<_Indexes>(_M_bound_args), __args)...);
1289 }
7aec2c63 1290
c5a23752 1291 // Call unqualified, return void
049d2893 1292 template<typename _Res, typename... _Args, std::size_t... _Indexes>
e9599233
BK
1293 void
1294 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1295 typename __enable_if_void<_Res>::type = 0)
1296 {
1297 _M_f(_Mu<_Bound_args>()
1b3fad81 1298 (get<_Indexes>(_M_bound_args), __args)...);
e9599233 1299 }
c5a23752 1300
7aec2c63 1301 // Call as const
049d2893 1302 template<typename _Res, typename... _Args, std::size_t... _Indexes>
e9599233
BK
1303 _Result
1304 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1305 typename __disable_if_void<_Res>::type = 0) const
1306 {
1307 return _M_f(_Mu<_Bound_args>()
1308 (get<_Indexes>(_M_bound_args), __args)...);
1309 }
7aec2c63 1310
c5a23752 1311 // Call as const, return void
049d2893 1312 template<typename _Res, typename... _Args, std::size_t... _Indexes>
e9599233
BK
1313 void
1314 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1315 typename __enable_if_void<_Res>::type = 0) const
1316 {
1317 _M_f(_Mu<_Bound_args>()
1b3fad81 1318 (get<_Indexes>(_M_bound_args), __args)...);
e9599233 1319 }
c5a23752 1320
7aec2c63 1321 // Call as volatile
049d2893 1322 template<typename _Res, typename... _Args, std::size_t... _Indexes>
e9599233
BK
1323 _Result
1324 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1325 typename __disable_if_void<_Res>::type = 0) volatile
1326 {
1327 return _M_f(_Mu<_Bound_args>()
1328 (__volget<_Indexes>(_M_bound_args), __args)...);
1329 }
7aec2c63 1330
c5a23752 1331 // Call as volatile, return void
049d2893 1332 template<typename _Res, typename... _Args, std::size_t... _Indexes>
e9599233
BK
1333 void
1334 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1335 typename __enable_if_void<_Res>::type = 0) volatile
1336 {
1337 _M_f(_Mu<_Bound_args>()
1b3fad81 1338 (__volget<_Indexes>(_M_bound_args), __args)...);
e9599233 1339 }
c5a23752 1340
7aec2c63 1341 // Call as const volatile
049d2893 1342 template<typename _Res, typename... _Args, std::size_t... _Indexes>
e9599233
BK
1343 _Result
1344 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
1345 typename __disable_if_void<_Res>::type = 0) const volatile
1346 {
1347 return _M_f(_Mu<_Bound_args>()
1348 (__volget<_Indexes>(_M_bound_args), __args)...);
1349 }
7aec2c63 1350
c5a23752 1351 // Call as const volatile, return void
049d2893 1352 template<typename _Res, typename... _Args, std::size_t... _Indexes>
e9599233
BK
1353 void
1354 __call(tuple<_Args...>&& __args,
1355 _Index_tuple<_Indexes...>,
1356 typename __enable_if_void<_Res>::type = 0) const volatile
1357 {
1358 _M_f(_Mu<_Bound_args>()
1b3fad81 1359 (__volget<_Indexes>(_M_bound_args), __args)...);
e9599233 1360 }
c5a23752 1361
7aec2c63
PC
1362 public:
1363 typedef _Result result_type;
1364
1b3fad81 1365 template<typename... _Args>
e9599233
BK
1366 explicit _Bind_result(const _Functor& __f, _Args&&... __args)
1367 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
1368 { }
1b3fad81
JW
1369
1370 template<typename... _Args>
e9599233
BK
1371 explicit _Bind_result(_Functor&& __f, _Args&&... __args)
1372 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
1373 { }
1b3fad81
JW
1374
1375 _Bind_result(const _Bind_result&) = default;
1376
1377 _Bind_result(_Bind_result&& __b)
1378 : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
d90d97ff 1379 { }
7aec2c63
PC
1380
1381 // Call unqualified
1382 template<typename... _Args>
e9599233
BK
1383 result_type
1384 operator()(_Args&&... __args)
1385 {
1386 return this->__call<_Result>(
1387 std::forward_as_tuple(std::forward<_Args>(__args)...),
1388 _Bound_indexes());
1389 }
7aec2c63
PC
1390
1391 // Call as const
1392 template<typename... _Args>
e9599233
BK
1393 result_type
1394 operator()(_Args&&... __args) const
1395 {
1396 return this->__call<_Result>(
1397 std::forward_as_tuple(std::forward<_Args>(__args)...),
1398 _Bound_indexes());
1399 }
7aec2c63
PC
1400
1401 // Call as volatile
1402 template<typename... _Args>
e9599233
BK
1403 result_type
1404 operator()(_Args&&... __args) volatile
1405 {
1406 return this->__call<_Result>(
1407 std::forward_as_tuple(std::forward<_Args>(__args)...),
1408 _Bound_indexes());
1409 }
7aec2c63
PC
1410
1411 // Call as const volatile
1412 template<typename... _Args>
e9599233
BK
1413 result_type
1414 operator()(_Args&&... __args) const volatile
1415 {
1416 return this->__call<_Result>(
1417 std::forward_as_tuple(std::forward<_Args>(__args)...),
1418 _Bound_indexes());
1419 }
7aec2c63
PC
1420 };
1421
0eb95b0d
BK
1422 /**
1423 * @brief Class template _Bind is always a bind expression.
1424 * @ingroup binders
1425 */
7aec2c63
PC
1426 template<typename _Signature>
1427 struct is_bind_expression<_Bind<_Signature> >
2b054f63 1428 : public true_type { };
7aec2c63 1429
0eb95b0d
BK
1430 /**
1431 * @brief Class template _Bind is always a bind expression.
1432 * @ingroup binders
1433 */
e02a5443
JW
1434 template<typename _Signature>
1435 struct is_bind_expression<const _Bind<_Signature> >
1436 : public true_type { };
1437
1438 /**
1439 * @brief Class template _Bind is always a bind expression.
1440 * @ingroup binders
1441 */
1442 template<typename _Signature>
1443 struct is_bind_expression<volatile _Bind<_Signature> >
1444 : public true_type { };
1445
1446 /**
1447 * @brief Class template _Bind is always a bind expression.
1448 * @ingroup binders
1449 */
1450 template<typename _Signature>
1451 struct is_bind_expression<const volatile _Bind<_Signature>>
1452 : public true_type { };
1453
1454 /**
1455 * @brief Class template _Bind_result is always a bind expression.
1456 * @ingroup binders
1457 */
1458 template<typename _Result, typename _Signature>
1459 struct is_bind_expression<_Bind_result<_Result, _Signature>>
1460 : public true_type { };
1461
1462 /**
1463 * @brief Class template _Bind_result is always a bind expression.
1464 * @ingroup binders
1465 */
1466 template<typename _Result, typename _Signature>
1467 struct is_bind_expression<const _Bind_result<_Result, _Signature>>
1468 : public true_type { };
1469
1470 /**
1471 * @brief Class template _Bind_result is always a bind expression.
1472 * @ingroup binders
1473 */
1474 template<typename _Result, typename _Signature>
1475 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
1476 : public true_type { };
1477
1478 /**
1479 * @brief Class template _Bind_result is always a bind expression.
1480 * @ingroup binders
1481 */
7aec2c63 1482 template<typename _Result, typename _Signature>
e02a5443 1483 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
2b054f63 1484 : public true_type { };
7aec2c63 1485
0f88da8d
JW
1486 // Trait type used to remove std::bind() from overload set via SFINAE
1487 // when first argument has integer type, so that std::bind() will
1488 // not be a better match than ::bind() from the BSD Sockets API.
1489 template<typename _Tp>
1490 class __is_socketlike
1491 {
1492 typedef typename decay<_Tp>::type _Tp2;
1493 public:
1494 static const bool value =
1495 is_integral<_Tp2>::value || is_enum<_Tp2>::value;
1496 };
1497
1498 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1b3fad81
JW
1499 struct _Bind_helper
1500 {
0f88da8d 1501 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
e9599233 1502 __maybe_type;
0f88da8d
JW
1503 typedef typename __maybe_type::type __func_type;
1504 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1b3fad81
JW
1505 };
1506
0f88da8d
JW
1507 // Partial specialization for is_socketlike == true, does not define
1508 // nested type so std::bind() will not participate in overload resolution
1509 // when the first argument might be a socket file descriptor.
1510 template<typename _Func, typename... _BoundArgs>
1511 struct _Bind_helper<true, _Func, _BoundArgs...>
1512 { };
1513
0eb95b0d
BK
1514 /**
1515 * @brief Function template for std::bind.
1516 * @ingroup binders
1517 */
0f88da8d
JW
1518 template<typename _Func, typename... _BoundArgs>
1519 inline typename
1520 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
1521 bind(_Func&& __f, _BoundArgs&&... __args)
7aec2c63 1522 {
0f88da8d 1523 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
1b3fad81
JW
1524 typedef typename __helper_type::__maybe_type __maybe_type;
1525 typedef typename __helper_type::type __result_type;
0f88da8d
JW
1526 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1527 std::forward<_BoundArgs>(__args)...);
e9599233 1528 }
7aec2c63 1529
0f88da8d 1530 template<typename _Result, typename _Func, typename... _BoundArgs>
1b3fad81
JW
1531 struct _Bindres_helper
1532 {
0f88da8d 1533 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
e9599233 1534 __maybe_type;
1b3fad81
JW
1535 typedef typename __maybe_type::type __functor_type;
1536 typedef _Bind_result<_Result,
0f88da8d 1537 __functor_type(typename decay<_BoundArgs>::type...)>
e9599233 1538 type;
1b3fad81
JW
1539 };
1540
0eb95b0d 1541 /**
1b3fad81 1542 * @brief Function template for std::bind<R>.
0eb95b0d
BK
1543 * @ingroup binders
1544 */
0f88da8d 1545 template<typename _Result, typename _Func, typename... _BoundArgs>
7aec2c63 1546 inline
0f88da8d
JW
1547 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1548 bind(_Func&& __f, _BoundArgs&&... __args)
7aec2c63 1549 {
0f88da8d 1550 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1b3fad81
JW
1551 typedef typename __helper_type::__maybe_type __maybe_type;
1552 typedef typename __helper_type::type __result_type;
0f88da8d
JW
1553 return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
1554 std::forward<_BoundArgs>(__args)...);
7aec2c63
PC
1555 }
1556
4880236e
JW
1557 template<typename _Signature>
1558 struct _Bind_simple;
1559
1560 template<typename _Callable, typename... _Args>
1561 struct _Bind_simple<_Callable(_Args...)>
1562 {
1563 typedef typename result_of<_Callable(_Args...)>::type result_type;
1564
1565 template<typename... _Args2, typename = typename
1566 enable_if< sizeof...(_Args) == sizeof...(_Args2)>::type>
1567 explicit
1568 _Bind_simple(const _Callable& __callable, _Args2&&... __args)
1569 : _M_bound(__callable, std::forward<_Args2>(__args)...)
1570 { }
1571
1572 template<typename... _Args2, typename = typename
1573 enable_if< sizeof...(_Args) == sizeof...(_Args2)>::type>
1574 explicit
1575 _Bind_simple(_Callable&& __callable, _Args2&&... __args)
1576 : _M_bound(std::move(__callable), std::forward<_Args2>(__args)...)
1577 { }
1578
1579 _Bind_simple(const _Bind_simple&) = default;
1580 _Bind_simple(_Bind_simple&&) = default;
1581
1582 result_type
1583 operator()()
1584 {
1585 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices;
1586 return _M_invoke(_Indices());
1587 }
1588
1589 private:
1590
049d2893 1591 template<std::size_t... _Indices>
4880236e
JW
1592 typename result_of<_Callable(_Args...)>::type
1593 _M_invoke(_Index_tuple<_Indices...>)
1594 {
1595 // std::bind always forwards bound arguments as lvalues,
1596 // but this type can call functions which only accept rvalues.
1597 return std::forward<_Callable>(std::get<0>(_M_bound))(
1598 std::forward<_Args>(std::get<_Indices+1>(_M_bound))...);
1599 }
1600
1601 std::tuple<_Callable, _Args...> _M_bound;
1602 };
1603
1604 template<typename _Func, typename... _BoundArgs>
1605 struct _Bind_simple_helper
1606 {
1607 typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1608 __maybe_type;
1609 typedef typename __maybe_type::type __func_type;
1610 typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)>
1611 __type;
1612 };
1613
1614 // Simplified version of std::bind for internal use, without support for
1615 // unbound arguments, placeholders or nested bind expressions.
1616 template<typename _Callable, typename... _Args>
1617 typename _Bind_simple_helper<_Callable, _Args...>::__type
1618 __bind_simple(_Callable&& __callable, _Args&&... __args)
1619 {
1620 typedef _Bind_simple_helper<_Callable, _Args...> __helper_type;
1621 typedef typename __helper_type::__maybe_type __maybe_type;
1622 typedef typename __helper_type::__type __result_type;
1623 return __result_type(
1624 __maybe_type::__do_wrap( std::forward<_Callable>(__callable)),
1625 std::forward<_Args>(__args)...);
1626 }
1627
7aec2c63
PC
1628 /**
1629 * @brief Exception class thrown when class template function's
1630 * operator() is called with an empty target.
1631 * @ingroup exceptions
1632 */
e9599233
BK
1633 class bad_function_call : public std::exception
1634 {
1635 public:
8535715d 1636 virtual ~bad_function_call() noexcept;
e9599233 1637 };
7aec2c63 1638
7aec2c63
PC
1639 /**
1640 * Trait identifying "location-invariant" types, meaning that the
1641 * address of the object (or any of its members) will not escape.
1642 * Also implies a trivial copy constructor and assignment operator.
1643 */
1644 template<typename _Tp>
1645 struct __is_location_invariant
8232dc64
PC
1646 : integral_constant<bool, (is_pointer<_Tp>::value
1647 || is_member_pointer<_Tp>::value)>
1648 { };
7aec2c63
PC
1649
1650 class _Undefined_class;
1651
1652 union _Nocopy_types
1653 {
1654 void* _M_object;
1655 const void* _M_const_object;
1656 void (*_M_function_pointer)();
1657 void (_Undefined_class::*_M_member_pointer)();
1658 };
1659
1660 union _Any_data
1661 {
1662 void* _M_access() { return &_M_pod_data[0]; }
1663 const void* _M_access() const { return &_M_pod_data[0]; }
1664
1665 template<typename _Tp>
1666 _Tp&
1667 _M_access()
1668 { return *static_cast<_Tp*>(_M_access()); }
1669
1670 template<typename _Tp>
1671 const _Tp&
1672 _M_access() const
1673 { return *static_cast<const _Tp*>(_M_access()); }
1674
1675 _Nocopy_types _M_unused;
1676 char _M_pod_data[sizeof(_Nocopy_types)];
1677 };
1678
1679 enum _Manager_operation
1680 {
1681 __get_type_info,
1682 __get_functor_ptr,
1683 __clone_functor,
1684 __destroy_functor
1685 };
1686
1687 // Simple type wrapper that helps avoid annoying const problems
1688 // when casting between void pointers and pointers-to-pointers.
1689 template<typename _Tp>
1690 struct _Simple_type_wrapper
1691 {
1692 _Simple_type_wrapper(_Tp __value) : __value(__value) { }
1693
1694 _Tp __value;
1695 };
1696
1697 template<typename _Tp>
1698 struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
1699 : __is_location_invariant<_Tp>
8232dc64 1700 { };
7aec2c63
PC
1701
1702 // Converts a reference to a function object into a callable
1703 // function object.
1704 template<typename _Functor>
1705 inline _Functor&
1706 __callable_functor(_Functor& __f)
1707 { return __f; }
1708
1709 template<typename _Member, typename _Class>
1710 inline _Mem_fn<_Member _Class::*>
1711 __callable_functor(_Member _Class::* &__p)
28d5436f 1712 { return std::mem_fn(__p); }
7aec2c63
PC
1713
1714 template<typename _Member, typename _Class>
1715 inline _Mem_fn<_Member _Class::*>
1716 __callable_functor(_Member _Class::* const &__p)
28d5436f 1717 { return std::mem_fn(__p); }
7aec2c63 1718
181c9de2
JW
1719 template<typename _Member, typename _Class>
1720 inline _Mem_fn<_Member _Class::*>
1721 __callable_functor(_Member _Class::* volatile &__p)
1722 { return std::mem_fn(__p); }
1723
1724 template<typename _Member, typename _Class>
1725 inline _Mem_fn<_Member _Class::*>
1726 __callable_functor(_Member _Class::* const volatile &__p)
1727 { return std::mem_fn(__p); }
1728
7aec2c63
PC
1729 template<typename _Signature>
1730 class function;
1731
1732 /// Base class of all polymorphic function object wrappers.
1733 class _Function_base
1734 {
1735 public:
1736 static const std::size_t _M_max_size = sizeof(_Nocopy_types);
1737 static const std::size_t _M_max_align = __alignof__(_Nocopy_types);
1738
1739 template<typename _Functor>
1740 class _Base_manager
1741 {
1742 protected:
1743 static const bool __stored_locally =
e9599233
BK
1744 (__is_location_invariant<_Functor>::value
1745 && sizeof(_Functor) <= _M_max_size
1746 && __alignof__(_Functor) <= _M_max_align
1747 && (_M_max_align % __alignof__(_Functor) == 0));
1748
7aec2c63
PC
1749 typedef integral_constant<bool, __stored_locally> _Local_storage;
1750
1751 // Retrieve a pointer to the function object
1752 static _Functor*
1753 _M_get_pointer(const _Any_data& __source)
1754 {
1755 const _Functor* __ptr =
3e49fb03 1756 __stored_locally? std::__addressof(__source._M_access<_Functor>())
7aec2c63
PC
1757 /* have stored a pointer */ : __source._M_access<_Functor*>();
1758 return const_cast<_Functor*>(__ptr);
1759 }
1760
1761 // Clone a location-invariant function object that fits within
1762 // an _Any_data structure.
1763 static void
1764 _M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
1765 {
1766 new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
1767 }
1768
1769 // Clone a function object that is not location-invariant or
1770 // that cannot fit into an _Any_data structure.
1771 static void
1772 _M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
1773 {
1774 __dest._M_access<_Functor*>() =
1775 new _Functor(*__source._M_access<_Functor*>());
1776 }
1777
1778 // Destroying a location-invariant object may still require
1779 // destruction.
1780 static void
1781 _M_destroy(_Any_data& __victim, true_type)
1782 {
1783 __victim._M_access<_Functor>().~_Functor();
1784 }
e9599233 1785
7aec2c63
PC
1786 // Destroying an object located on the heap.
1787 static void
1788 _M_destroy(_Any_data& __victim, false_type)
1789 {
1790 delete __victim._M_access<_Functor*>();
1791 }
e9599233 1792
7aec2c63
PC
1793 public:
1794 static bool
1795 _M_manager(_Any_data& __dest, const _Any_data& __source,
1796 _Manager_operation __op)
1797 {
1798 switch (__op)
1799 {
1800#ifdef __GXX_RTTI
1801 case __get_type_info:
1802 __dest._M_access<const type_info*>() = &typeid(_Functor);
1803 break;
1804#endif
1805 case __get_functor_ptr:
1806 __dest._M_access<_Functor*>() = _M_get_pointer(__source);
1807 break;
e9599233 1808
7aec2c63
PC
1809 case __clone_functor:
1810 _M_clone(__dest, __source, _Local_storage());
1811 break;
1812
1813 case __destroy_functor:
1814 _M_destroy(__dest, _Local_storage());
1815 break;
1816 }
1817 return false;
1818 }
1819
1820 static void
ec903a9c
JW
1821 _M_init_functor(_Any_data& __functor, _Functor&& __f)
1822 { _M_init_functor(__functor, std::move(__f), _Local_storage()); }
e9599233 1823
7aec2c63
PC
1824 template<typename _Signature>
1825 static bool
1826 _M_not_empty_function(const function<_Signature>& __f)
e9599233 1827 { return static_cast<bool>(__f); }
7aec2c63
PC
1828
1829 template<typename _Tp>
1830 static bool
1831 _M_not_empty_function(const _Tp*& __fp)
1832 { return __fp; }
1833
1834 template<typename _Class, typename _Tp>
1835 static bool
1836 _M_not_empty_function(_Tp _Class::* const& __mp)
1837 { return __mp; }
1838
1839 template<typename _Tp>
1840 static bool
1841 _M_not_empty_function(const _Tp&)
1842 { return true; }
1843
1844 private:
e9599233 1845 static void
ec903a9c
JW
1846 _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
1847 { new (__functor._M_access()) _Functor(std::move(__f)); }
7aec2c63
PC
1848
1849 static void
ec903a9c
JW
1850 _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
1851 { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
7aec2c63
PC
1852 };
1853
1854 template<typename _Functor>
1855 class _Ref_manager : public _Base_manager<_Functor*>
1856 {
1857 typedef _Function_base::_Base_manager<_Functor*> _Base;
1858
143a1ce1 1859 public:
7aec2c63
PC
1860 static bool
1861 _M_manager(_Any_data& __dest, const _Any_data& __source,
1862 _Manager_operation __op)
1863 {
1864 switch (__op)
1865 {
1866#ifdef __GXX_RTTI
1867 case __get_type_info:
1868 __dest._M_access<const type_info*>() = &typeid(_Functor);
1869 break;
af13a7a6 1870#endif
7aec2c63
PC
1871 case __get_functor_ptr:
1872 __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
1873 return is_const<_Functor>::value;
1874 break;
e9599233 1875
7aec2c63
PC
1876 default:
1877 _Base::_M_manager(__dest, __source, __op);
1878 }
1879 return false;
1880 }
1881
1882 static void
1883 _M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
1884 {
1885 // TBD: Use address_of function instead.
1886 _Base::_M_init_functor(__functor, &__f.get());
1887 }
1888 };
1889
1890 _Function_base() : _M_manager(0) { }
e9599233 1891
7aec2c63
PC
1892 ~_Function_base()
1893 {
1894 if (_M_manager)
1895 _M_manager(_M_functor, _M_functor, __destroy_functor);
1896 }
1897
1898
1899 bool _M_empty() const { return !_M_manager; }
1900
1901 typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
e9599233 1902 _Manager_operation);
7aec2c63
PC
1903
1904 _Any_data _M_functor;
1905 _Manager_type _M_manager;
1906 };
1907
1908 template<typename _Signature, typename _Functor>
1909 class _Function_handler;
1910
1911 template<typename _Res, typename _Functor, typename... _ArgTypes>
1912 class _Function_handler<_Res(_ArgTypes...), _Functor>
1913 : public _Function_base::_Base_manager<_Functor>
1914 {
1915 typedef _Function_base::_Base_manager<_Functor> _Base;
1916
1917 public:
1918 static _Res
1919 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1920 {
e9599233
BK
1921 return (*_Base::_M_get_pointer(__functor))(
1922 std::forward<_ArgTypes>(__args)...);
7aec2c63
PC
1923 }
1924 };
1925
1926 template<typename _Functor, typename... _ArgTypes>
1927 class _Function_handler<void(_ArgTypes...), _Functor>
1928 : public _Function_base::_Base_manager<_Functor>
1929 {
1930 typedef _Function_base::_Base_manager<_Functor> _Base;
1931
1932 public:
1933 static void
1934 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1935 {
e9599233
BK
1936 (*_Base::_M_get_pointer(__functor))(
1937 std::forward<_ArgTypes>(__args)...);
7aec2c63
PC
1938 }
1939 };
1940
1941 template<typename _Res, typename _Functor, typename... _ArgTypes>
1942 class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
1943 : public _Function_base::_Ref_manager<_Functor>
1944 {
1945 typedef _Function_base::_Ref_manager<_Functor> _Base;
1946
1947 public:
1948 static _Res
1949 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1950 {
e9599233
BK
1951 return __callable_functor(**_Base::_M_get_pointer(__functor))(
1952 std::forward<_ArgTypes>(__args)...);
7aec2c63
PC
1953 }
1954 };
1955
1956 template<typename _Functor, typename... _ArgTypes>
1957 class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
1958 : public _Function_base::_Ref_manager<_Functor>
1959 {
1960 typedef _Function_base::_Ref_manager<_Functor> _Base;
1961
1962 public:
1963 static void
1964 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1965 {
e9599233
BK
1966 __callable_functor(**_Base::_M_get_pointer(__functor))(
1967 std::forward<_ArgTypes>(__args)...);
7aec2c63
PC
1968 }
1969 };
1970
e9599233
BK
1971 template<typename _Class, typename _Member, typename _Res,
1972 typename... _ArgTypes>
7aec2c63
PC
1973 class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
1974 : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
1975 {
1976 typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
e9599233 1977 _Base;
7aec2c63
PC
1978
1979 public:
1980 static _Res
1981 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
1982 {
28d5436f 1983 return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
e9599233 1984 std::forward<_ArgTypes>(__args)...);
7aec2c63
PC
1985 }
1986 };
1987
1988 template<typename _Class, typename _Member, typename... _ArgTypes>
1989 class _Function_handler<void(_ArgTypes...), _Member _Class::*>
1990 : public _Function_base::_Base_manager<
e9599233 1991 _Simple_type_wrapper< _Member _Class::* > >
7aec2c63
PC
1992 {
1993 typedef _Member _Class::* _Functor;
1994 typedef _Simple_type_wrapper<_Functor> _Wrapper;
1995 typedef _Function_base::_Base_manager<_Wrapper> _Base;
1996
143a1ce1 1997 public:
7aec2c63
PC
1998 static bool
1999 _M_manager(_Any_data& __dest, const _Any_data& __source,
e9599233 2000 _Manager_operation __op)
7aec2c63 2001 {
e9599233 2002 switch (__op)
7aec2c63
PC
2003 {
2004#ifdef __GXX_RTTI
2005 case __get_type_info:
2006 __dest._M_access<const type_info*>() = &typeid(_Functor);
2007 break;
e9599233 2008#endif
7aec2c63
PC
2009 case __get_functor_ptr:
2010 __dest._M_access<_Functor*>() =
2011 &_Base::_M_get_pointer(__source)->__value;
2012 break;
e9599233 2013
7aec2c63
PC
2014 default:
2015 _Base::_M_manager(__dest, __source, __op);
2016 }
e9599233 2017 return false;
7aec2c63
PC
2018 }
2019
2020 static void
2021 _M_invoke(const _Any_data& __functor, _ArgTypes... __args)
2022 {
28d5436f 2023 std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
e9599233 2024 std::forward<_ArgTypes>(__args)...);
7aec2c63
PC
2025 }
2026 };
2027
0eb95b0d
BK
2028 /**
2029 * @brief Primary class template for std::function.
2030 * @ingroup functors
2031 *
2032 * Polymorphic function wrapper.
2033 */
7aec2c63
PC
2034 template<typename _Res, typename... _ArgTypes>
2035 class function<_Res(_ArgTypes...)>
2036 : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
2037 private _Function_base
2038 {
2039 typedef _Res _Signature_type(_ArgTypes...);
e9599233 2040
143a1ce1
JW
2041 template<typename _Functor>
2042 using _Invoke = decltype(__callable_functor(std::declval<_Functor&>())
2043 (std::declval<_ArgTypes>()...) );
2044
2045 template<typename _CallRes, typename _Res1>
2046 struct _CheckResult
2047 : is_convertible<_CallRes, _Res1> { };
2048
2049 template<typename _CallRes>
2050 struct _CheckResult<_CallRes, void>
2051 : true_type { };
2052
2053 template<typename _Functor>
2054 using _Callable = _CheckResult<_Invoke<_Functor>, _Res>;
2055
2056 template<typename _Cond, typename _Tp>
2057 using _Requires = typename enable_if<_Cond::value, _Tp>::type;
e9599233 2058
7aec2c63
PC
2059 public:
2060 typedef _Res result_type;
e9599233 2061
7aec2c63 2062 // [3.7.2.1] construct/copy/destroy
e9599233 2063
7aec2c63
PC
2064 /**
2065 * @brief Default construct creates an empty function call wrapper.
2066 * @post @c !(bool)*this
2067 */
6b14c6d7
PC
2068 function() noexcept
2069 : _Function_base() { }
e9599233 2070
7aec2c63 2071 /**
6d00745b 2072 * @brief Creates an empty function call wrapper.
7aec2c63
PC
2073 * @post @c !(bool)*this
2074 */
6b14c6d7
PC
2075 function(nullptr_t) noexcept
2076 : _Function_base() { }
e9599233 2077
7aec2c63
PC
2078 /**
2079 * @brief %Function copy constructor.
93c66bc6
BK
2080 * @param __x A %function object with identical call signature.
2081 * @post @c bool(*this) == bool(__x)
7aec2c63
PC
2082 *
2083 * The newly-created %function contains a copy of the target of @a
93c66bc6 2084 * __x (if it has one).
7aec2c63
PC
2085 */
2086 function(const function& __x);
2087
2088 /**
2089 * @brief %Function move constructor.
93c66bc6 2090 * @param __x A %function object rvalue with identical call signature.
7aec2c63 2091 *
93c66bc6 2092 * The newly-created %function contains the target of @a __x
7aec2c63
PC
2093 * (if it has one).
2094 */
2095 function(function&& __x) : _Function_base()
2096 {
e9599233 2097 __x.swap(*this);
7aec2c63
PC
2098 }
2099
2100 // TODO: needs allocator_arg_t
e9599233 2101
7aec2c63
PC
2102 /**
2103 * @brief Builds a %function that targets a copy of the incoming
2104 * function object.
93c66bc6 2105 * @param __f A %function object that is callable with parameters of
7aec2c63
PC
2106 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2107 * to @c Res.
2108 *
93c66bc6
BK
2109 * The newly-created %function object will target a copy of
2110 * @a __f. If @a __f is @c reference_wrapper<F>, then this function
7aec2c63 2111 * object will contain a reference to the function object @c
93c66bc6 2112 * __f.get(). If @a __f is a NULL function pointer or NULL
7aec2c63
PC
2113 * pointer-to-member, the newly-created object will be empty.
2114 *
93c66bc6 2115 * If @a __f is a non-NULL function pointer or an object of type @c
7aec2c63
PC
2116 * reference_wrapper<F>, this function will not throw.
2117 */
143a1ce1
JW
2118 template<typename _Functor,
2119 typename = _Requires<_Callable<_Functor>, void>>
2120 function(_Functor);
7aec2c63
PC
2121
2122 /**
2123 * @brief %Function assignment operator.
93c66bc6 2124 * @param __x A %function with identical call signature.
7aec2c63
PC
2125 * @post @c (bool)*this == (bool)x
2126 * @returns @c *this
2127 *
93c66bc6 2128 * The target of @a __x is copied to @c *this. If @a __x has no
7aec2c63
PC
2129 * target, then @c *this will be empty.
2130 *
93c66bc6 2131 * If @a __x targets a function pointer or a reference to a function
7aec2c63
PC
2132 * object, then this operation will not throw an %exception.
2133 */
2134 function&
2135 operator=(const function& __x)
2136 {
e9599233
BK
2137 function(__x).swap(*this);
2138 return *this;
7aec2c63
PC
2139 }
2140
2141 /**
2142 * @brief %Function move-assignment operator.
93c66bc6 2143 * @param __x A %function rvalue with identical call signature.
7aec2c63
PC
2144 * @returns @c *this
2145 *
93c66bc6 2146 * The target of @a __x is moved to @c *this. If @a __x has no
7aec2c63
PC
2147 * target, then @c *this will be empty.
2148 *
93c66bc6 2149 * If @a __x targets a function pointer or a reference to a function
7aec2c63
PC
2150 * object, then this operation will not throw an %exception.
2151 */
2152 function&
2153 operator=(function&& __x)
2154 {
e9599233
BK
2155 function(std::move(__x)).swap(*this);
2156 return *this;
7aec2c63
PC
2157 }
2158
2159 /**
2160 * @brief %Function assignment to zero.
2161 * @post @c !(bool)*this
2162 * @returns @c *this
2163 *
2164 * The target of @c *this is deallocated, leaving it empty.
2165 */
2166 function&
6d00745b 2167 operator=(nullptr_t)
7aec2c63 2168 {
e9599233 2169 if (_M_manager)
7aec2c63
PC
2170 {
2171 _M_manager(_M_functor, _M_functor, __destroy_functor);
2172 _M_manager = 0;
2173 _M_invoker = 0;
2174 }
e9599233 2175 return *this;
7aec2c63
PC
2176 }
2177
2178 /**
2179 * @brief %Function assignment to a new target.
93c66bc6 2180 * @param __f A %function object that is callable with parameters of
7aec2c63
PC
2181 * type @c T1, @c T2, ..., @c TN and returns a value convertible
2182 * to @c Res.
2183 * @return @c *this
2184 *
2185 * This %function object wrapper will target a copy of @a
93c66bc6 2186 * __f. If @a __f is @c reference_wrapper<F>, then this function
7aec2c63 2187 * object will contain a reference to the function object @c
93c66bc6 2188 * __f.get(). If @a __f is a NULL function pointer or NULL
7aec2c63
PC
2189 * pointer-to-member, @c this object will be empty.
2190 *
93c66bc6 2191 * If @a __f is a non-NULL function pointer or an object of type @c
7aec2c63
PC
2192 * reference_wrapper<F>, this function will not throw.
2193 */
2194 template<typename _Functor>
143a1ce1 2195 _Requires<_Callable<_Functor>, function&>
ec903a9c 2196 operator=(_Functor&& __f)
7aec2c63 2197 {
ec903a9c 2198 function(std::forward<_Functor>(__f)).swap(*this);
7aec2c63
PC
2199 return *this;
2200 }
2201
2202 /// @overload
2203 template<typename _Functor>
143a1ce1 2204 function&
6b14c6d7 2205 operator=(reference_wrapper<_Functor> __f) noexcept
7aec2c63
PC
2206 {
2207 function(__f).swap(*this);
2208 return *this;
2209 }
2210
2211 // [3.7.2.2] function modifiers
e9599233 2212
7aec2c63
PC
2213 /**
2214 * @brief Swap the targets of two %function objects.
93c66bc6 2215 * @param __x A %function with identical call signature.
7aec2c63 2216 *
93c66bc6 2217 * Swap the targets of @c this function object and @a __f. This
7aec2c63
PC
2218 * function will not throw an %exception.
2219 */
2220 void swap(function& __x)
2221 {
6703f185
PC
2222 std::swap(_M_functor, __x._M_functor);
2223 std::swap(_M_manager, __x._M_manager);
2224 std::swap(_M_invoker, __x._M_invoker);
7aec2c63
PC
2225 }
2226
2227 // TODO: needs allocator_arg_t
2228 /*
2229 template<typename _Functor, typename _Alloc>
e9599233
BK
2230 void
2231 assign(_Functor&& __f, const _Alloc& __a)
2232 {
2233 function(allocator_arg, __a,
2234 std::forward<_Functor>(__f)).swap(*this);
2235 }
7aec2c63 2236 */
e9599233 2237
7aec2c63
PC
2238 // [3.7.2.3] function capacity
2239
2240 /**
2241 * @brief Determine if the %function wrapper has a target.
2242 *
2243 * @return @c true when this %function object contains a target,
2244 * or @c false when it is empty.
2245 *
2246 * This function will not throw an %exception.
2247 */
6b14c6d7 2248 explicit operator bool() const noexcept
7aec2c63
PC
2249 { return !_M_empty(); }
2250
2251 // [3.7.2.4] function invocation
2252
2253 /**
2254 * @brief Invokes the function targeted by @c *this.
2255 * @returns the result of the target.
2256 * @throws bad_function_call when @c !(bool)*this
2257 *
2258 * The function call operator invokes the target function object
2259 * stored by @c this.
2260 */
2261 _Res operator()(_ArgTypes... __args) const;
2262
2263#ifdef __GXX_RTTI
2264 // [3.7.2.5] function target access
2265 /**
2266 * @brief Determine the type of the target of this function object
2267 * wrapper.
2268 *
2269 * @returns the type identifier of the target function object, or
2270 * @c typeid(void) if @c !(bool)*this.
2271 *
2272 * This function will not throw an %exception.
2273 */
6b14c6d7 2274 const type_info& target_type() const noexcept;
e9599233 2275
7aec2c63
PC
2276 /**
2277 * @brief Access the stored target function object.
2278 *
2279 * @return Returns a pointer to the stored target function object,
2280 * if @c typeid(Functor).equals(target_type()); otherwise, a NULL
2281 * pointer.
2282 *
2283 * This function will not throw an %exception.
2284 */
6b14c6d7 2285 template<typename _Functor> _Functor* target() noexcept;
e9599233 2286
7aec2c63 2287 /// @overload
6b14c6d7 2288 template<typename _Functor> const _Functor* target() const noexcept;
7aec2c63
PC
2289#endif
2290
7aec2c63
PC
2291 private:
2292 typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
2293 _Invoker_type _M_invoker;
2294 };
2295
0eb95b0d 2296 // Out-of-line member definitions.
7aec2c63
PC
2297 template<typename _Res, typename... _ArgTypes>
2298 function<_Res(_ArgTypes...)>::
2299 function(const function& __x)
2300 : _Function_base()
2301 {
2302 if (static_cast<bool>(__x))
2303 {
2304 _M_invoker = __x._M_invoker;
2305 _M_manager = __x._M_manager;
2306 __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
2307 }
2308 }
2309
2310 template<typename _Res, typename... _ArgTypes>
143a1ce1 2311 template<typename _Functor, typename>
7aec2c63 2312 function<_Res(_ArgTypes...)>::
143a1ce1 2313 function(_Functor __f)
7aec2c63
PC
2314 : _Function_base()
2315 {
2316 typedef _Function_handler<_Signature_type, _Functor> _My_handler;
2317
2318 if (_My_handler::_M_not_empty_function(__f))
2319 {
2320 _M_invoker = &_My_handler::_M_invoke;
2321 _M_manager = &_My_handler::_M_manager;
ec903a9c 2322 _My_handler::_M_init_functor(_M_functor, std::move(__f));
7aec2c63
PC
2323 }
2324 }
2325
2326 template<typename _Res, typename... _ArgTypes>
2327 _Res
2328 function<_Res(_ArgTypes...)>::
2329 operator()(_ArgTypes... __args) const
2330 {
2331 if (_M_empty())
e9599233 2332 __throw_bad_function_call();
7aec2c63
PC
2333 return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
2334 }
2335
2336#ifdef __GXX_RTTI
2337 template<typename _Res, typename... _ArgTypes>
2338 const type_info&
2339 function<_Res(_ArgTypes...)>::
6b14c6d7 2340 target_type() const noexcept
7aec2c63
PC
2341 {
2342 if (_M_manager)
e9599233
BK
2343 {
2344 _Any_data __typeinfo_result;
2345 _M_manager(__typeinfo_result, _M_functor, __get_type_info);
2346 return *__typeinfo_result._M_access<const type_info*>();
2347 }
7aec2c63
PC
2348 else
2349 return typeid(void);
2350 }
2351
2352 template<typename _Res, typename... _ArgTypes>
2353 template<typename _Functor>
2354 _Functor*
2355 function<_Res(_ArgTypes...)>::
6b14c6d7 2356 target() noexcept
7aec2c63
PC
2357 {
2358 if (typeid(_Functor) == target_type() && _M_manager)
2359 {
2360 _Any_data __ptr;
2361 if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
2362 && !is_const<_Functor>::value)
2363 return 0;
2364 else
2365 return __ptr._M_access<_Functor*>();
2366 }
2367 else
2368 return 0;
2369 }
2370
2371 template<typename _Res, typename... _ArgTypes>
2372 template<typename _Functor>
2373 const _Functor*
2374 function<_Res(_ArgTypes...)>::
6b14c6d7 2375 target() const noexcept
7aec2c63
PC
2376 {
2377 if (typeid(_Functor) == target_type() && _M_manager)
2378 {
2379 _Any_data __ptr;
2380 _M_manager(__ptr, _M_functor, __get_functor_ptr);
2381 return __ptr._M_access<const _Functor*>();
2382 }
2383 else
2384 return 0;
2385 }
2386#endif
2387
2388 // [20.7.15.2.6] null pointer comparisons
2389
2390 /**
2391 * @brief Compares a polymorphic function object wrapper against 0
2392 * (the NULL pointer).
2393 * @returns @c true if the wrapper has no target, @c false otherwise
2394 *
2395 * This function will not throw an %exception.
2396 */
2397 template<typename _Res, typename... _Args>
2398 inline bool
6b14c6d7 2399 operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
7aec2c63
PC
2400 { return !static_cast<bool>(__f); }
2401
2402 /// @overload
2403 template<typename _Res, typename... _Args>
2404 inline bool
6b14c6d7 2405 operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
7aec2c63
PC
2406 { return !static_cast<bool>(__f); }
2407
2408 /**
2409 * @brief Compares a polymorphic function object wrapper against 0
2410 * (the NULL pointer).
2411 * @returns @c false if the wrapper has no target, @c true otherwise
2412 *
2413 * This function will not throw an %exception.
2414 */
2415 template<typename _Res, typename... _Args>
2416 inline bool
6b14c6d7 2417 operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
7aec2c63
PC
2418 { return static_cast<bool>(__f); }
2419
2420 /// @overload
2421 template<typename _Res, typename... _Args>
2422 inline bool
6b14c6d7 2423 operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
7aec2c63
PC
2424 { return static_cast<bool>(__f); }
2425
2426 // [20.7.15.2.7] specialized algorithms
2427
2428 /**
2429 * @brief Swap the targets of two polymorphic function object wrappers.
2430 *
2431 * This function will not throw an %exception.
2432 */
2433 template<typename _Res, typename... _Args>
2434 inline void
2435 swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
2436 { __x.swap(__y); }
53dc5044 2437
12ffa228
BK
2438_GLIBCXX_END_NAMESPACE_VERSION
2439} // namespace std
7aec2c63 2440
734f5023 2441#endif // C++11
af13a7a6 2442
e133ace8 2443#endif // _GLIBCXX_FUNCTIONAL