Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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