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