functional 73.3 KB
Newer Older
1
// <functional> -*- C++ -*-
2

3
// Copyright (C) 2001-2014 Free Software Foundation, Inc.
4 5 6 7
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
8
// Free Software Foundation; either version 3, or (at your option)
9 10 11 12 13 14 15
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

16 17 18
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
19

20 21 22 23
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
 * Copyright (c) 1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 */

39
/** @file include/functional
40
 *  This is a Standard C++ Library header.
Phil Edwards committed
41 42
 */

43 44
#ifndef _GLIBCXX_FUNCTIONAL
#define _GLIBCXX_FUNCTIONAL 1
45 46

#pragma GCC system_header
Benjamin Kosnik committed
47

48 49 50
#include <bits/c++config.h>
#include <bits/stl_function.h>

51
#if __cplusplus >= 201103L
Paolo Carlini committed
52 53 54 55 56

#include <typeinfo>
#include <new>
#include <tuple>
#include <type_traits>
57
#include <bits/functexcept.h>
Paolo Carlini committed
58 59
#include <bits/functional_hash.h>

60 61 62
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
63

64 65 66 67
  template<typename _MemberPointer>
    class _Mem_fn;
  template<typename _Tp, typename _Class>
    _Mem_fn<_Tp _Class::*>
68
    mem_fn(_Tp _Class::*) noexcept;
69

Paolo Carlini committed
70
  /// If we have found a result_type, extract it.
71
  template<typename _Functor, typename = __void_t<>>
Paolo Carlini committed
72 73 74 75
    struct _Maybe_get_result_type
    { };

  template<typename _Functor>
76 77
    struct _Maybe_get_result_type<_Functor,
				  __void_t<typename _Functor::result_type>>
78
    { typedef typename _Functor::result_type result_type; };
Paolo Carlini committed
79 80 81

  /**
   *  Base class for any function object that has a weak result type, as
82
   *  defined in 20.8.2 [func.require] of C++11.
Paolo Carlini committed
83 84 85
  */
  template<typename _Functor>
    struct _Weak_result_type_impl
86
    : _Maybe_get_result_type<_Functor>
87
    { };
Paolo Carlini committed
88 89

  /// Retrieve the result type for a function type.
90
  template<typename _Res, typename... _ArgTypes>
Paolo Carlini committed
91
    struct _Weak_result_type_impl<_Res(_ArgTypes...)>
92 93
    { typedef _Res result_type; };

94
  template<typename _Res, typename... _ArgTypes>
95 96 97
    struct _Weak_result_type_impl<_Res(_ArgTypes......)>
    { typedef _Res result_type; };

98
  template<typename _Res, typename... _ArgTypes>
99 100 101
    struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
    { typedef _Res result_type; };

102
  template<typename _Res, typename... _ArgTypes>
103 104 105
    struct _Weak_result_type_impl<_Res(_ArgTypes......) const>
    { typedef _Res result_type; };

106
  template<typename _Res, typename... _ArgTypes>
107 108 109
    struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile>
    { typedef _Res result_type; };

110
  template<typename _Res, typename... _ArgTypes>
111 112 113
    struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile>
    { typedef _Res result_type; };

114
  template<typename _Res, typename... _ArgTypes>
115 116 117
    struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile>
    { typedef _Res result_type; };

118
  template<typename _Res, typename... _ArgTypes>
119 120
    struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile>
    { typedef _Res result_type; };
Paolo Carlini committed
121 122

  /// Retrieve the result type for a function reference.
123
  template<typename _Res, typename... _ArgTypes>
Paolo Carlini committed
124
    struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)>
125 126
    { typedef _Res result_type; };

127
  template<typename _Res, typename... _ArgTypes>
128 129
    struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)>
    { typedef _Res result_type; };
Paolo Carlini committed
130 131

  /// Retrieve the result type for a function pointer.
132
  template<typename _Res, typename... _ArgTypes>
Paolo Carlini committed
133
    struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)>
134 135
    { typedef _Res result_type; };

136
  template<typename _Res, typename... _ArgTypes>
137 138
    struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)>
    { typedef _Res result_type; };
Paolo Carlini committed
139

140 141
  /// Retrieve result type for a member function pointer.
  template<typename _Res, typename _Class, typename... _ArgTypes>
Paolo Carlini committed
142
    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)>
143 144
    { typedef _Res result_type; };

145
  template<typename _Res, typename _Class, typename... _ArgTypes>
146 147
    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)>
    { typedef _Res result_type; };
Paolo Carlini committed
148

149 150
  /// Retrieve result type for a const member function pointer.
  template<typename _Res, typename _Class, typename... _ArgTypes>
Paolo Carlini committed
151
    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const>
152 153
    { typedef _Res result_type; };

154
  template<typename _Res, typename _Class, typename... _ArgTypes>
155 156
    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const>
    { typedef _Res result_type; };
Paolo Carlini committed
157

158 159
  /// Retrieve result type for a volatile member function pointer.
  template<typename _Res, typename _Class, typename... _ArgTypes>
Paolo Carlini committed
160
    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile>
161 162
    { typedef _Res result_type; };

163
  template<typename _Res, typename _Class, typename... _ArgTypes>
164 165
    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile>
    { typedef _Res result_type; };
Paolo Carlini committed
166

167 168 169
  /// Retrieve result type for a const volatile member function pointer.
  template<typename _Res, typename _Class, typename... _ArgTypes>
    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)
170 171 172
				  const volatile>
    { typedef _Res result_type; };

173
  template<typename _Res, typename _Class, typename... _ArgTypes>
174 175 176
    struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)
				  const volatile>
    { typedef _Res result_type; };
Paolo Carlini committed
177 178 179 180 181 182 183 184

  /**
   *  Strip top-level cv-qualifiers from the function object and let
   *  _Weak_result_type_impl perform the real work.
  */
  template<typename _Functor>
    struct _Weak_result_type
    : _Weak_result_type_impl<typename remove_cv<_Functor>::type>
185
    { };
Paolo Carlini committed
186 187 188 189 190 191 192

  /**
   * Invoke a function object, which may be either a member pointer or a
   * function object. The first parameter will tell which.
   */
  template<typename _Functor, typename... _Args>
    inline
193
    typename enable_if<
194 195 196
	     (!is_member_pointer<_Functor>::value
	      && !is_function<_Functor>::value
	      && !is_function<typename remove_pointer<_Functor>::type>::value),
197
	     typename result_of<_Functor&(_Args&&...)>::type
198
	   >::type
199
    __invoke(_Functor& __f, _Args&&... __args)
Paolo Carlini committed
200
    {
201
      return __f(std::forward<_Args>(__args)...);
Paolo Carlini committed
202 203
    }

204 205 206 207 208 209 210 211 212 213
  template<typename _Functor, typename... _Args>
    inline
    typename enable_if<
             (is_member_pointer<_Functor>::value
              && !is_function<_Functor>::value
              && !is_function<typename remove_pointer<_Functor>::type>::value),
             typename result_of<_Functor(_Args&&...)>::type
           >::type
    __invoke(_Functor& __f, _Args&&... __args)
    {
214
      return std::mem_fn(__f)(std::forward<_Args>(__args)...);
215 216
    }

Paolo Carlini committed
217 218 219
  // To pick up function references (that will become function pointers)
  template<typename _Functor, typename... _Args>
    inline
220
    typename enable_if<
221 222
	     (is_pointer<_Functor>::value
	      && is_function<typename remove_pointer<_Functor>::type>::value),
223
	     typename result_of<_Functor(_Args&&...)>::type
224
	   >::type
225
    __invoke(_Functor __f, _Args&&... __args)
Paolo Carlini committed
226
    {
227
      return __f(std::forward<_Args>(__args)...);
Paolo Carlini committed
228 229 230 231 232 233 234 235 236 237
    }

  /**
   *  Knowing which of unary_function and binary_function _Tp derives
   *  from, derives from the same and ensures that reference_wrapper
   *  will have a weak result type. See cases below.
   */
  template<bool _Unary, bool _Binary, typename _Tp>
    struct _Reference_wrapper_base_impl;

238
  // None of the nested argument types.
Paolo Carlini committed
239 240 241 242 243
  template<typename _Tp>
    struct _Reference_wrapper_base_impl<false, false, _Tp>
    : _Weak_result_type<_Tp>
    { };

244
  // Nested argument_type only.
Paolo Carlini committed
245 246
  template<typename _Tp>
    struct _Reference_wrapper_base_impl<true, false, _Tp>
247 248 249 250
    : _Weak_result_type<_Tp>
    {
      typedef typename _Tp::argument_type argument_type;
    };
Paolo Carlini committed
251

252
  // Nested first_argument_type and second_argument_type only.
Paolo Carlini committed
253 254
  template<typename _Tp>
    struct _Reference_wrapper_base_impl<false, true, _Tp>
255 256 257 258 259
    : _Weak_result_type<_Tp>
    {
      typedef typename _Tp::first_argument_type first_argument_type;
      typedef typename _Tp::second_argument_type second_argument_type;
    };
Paolo Carlini committed
260

261
  // All the nested argument types.
Paolo Carlini committed
262 263
   template<typename _Tp>
    struct _Reference_wrapper_base_impl<true, true, _Tp>
264
    : _Weak_result_type<_Tp>
Paolo Carlini committed
265
    {
266 267 268
      typedef typename _Tp::argument_type argument_type;
      typedef typename _Tp::first_argument_type first_argument_type;
      typedef typename _Tp::second_argument_type second_argument_type;
Paolo Carlini committed
269 270
    };

271 272 273 274
  _GLIBCXX_HAS_NESTED_TYPE(argument_type)
  _GLIBCXX_HAS_NESTED_TYPE(first_argument_type)
  _GLIBCXX_HAS_NESTED_TYPE(second_argument_type)

Paolo Carlini committed
275 276 277 278 279 280 281 282 283
  /**
   *  Derives from unary_function or binary_function when it
   *  can. Specializations handle all of the easy cases. The primary
   *  template determines what to do with a class type, which may
   *  derive from both unary_function and binary_function.
  */
  template<typename _Tp>
    struct _Reference_wrapper_base
    : _Reference_wrapper_base_impl<
284 285 286
      __has_argument_type<_Tp>::value,
      __has_first_argument_type<_Tp>::value
      && __has_second_argument_type<_Tp>::value,
Paolo Carlini committed
287 288 289 290 291 292 293 294 295
      _Tp>
    { };

  // - a function type (unary)
  template<typename _Res, typename _T1>
    struct _Reference_wrapper_base<_Res(_T1)>
    : unary_function<_T1, _Res>
    { };

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  template<typename _Res, typename _T1>
    struct _Reference_wrapper_base<_Res(_T1) const>
    : unary_function<_T1, _Res>
    { };

  template<typename _Res, typename _T1>
    struct _Reference_wrapper_base<_Res(_T1) volatile>
    : unary_function<_T1, _Res>
    { };

  template<typename _Res, typename _T1>
    struct _Reference_wrapper_base<_Res(_T1) const volatile>
    : unary_function<_T1, _Res>
    { };

Paolo Carlini committed
311 312 313 314 315 316
  // - a function type (binary)
  template<typename _Res, typename _T1, typename _T2>
    struct _Reference_wrapper_base<_Res(_T1, _T2)>
    : binary_function<_T1, _T2, _Res>
    { };

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  template<typename _Res, typename _T1, typename _T2>
    struct _Reference_wrapper_base<_Res(_T1, _T2) const>
    : binary_function<_T1, _T2, _Res>
    { };

  template<typename _Res, typename _T1, typename _T2>
    struct _Reference_wrapper_base<_Res(_T1, _T2) volatile>
    : binary_function<_T1, _T2, _Res>
    { };

  template<typename _Res, typename _T1, typename _T2>
    struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile>
    : binary_function<_T1, _T2, _Res>
    { };

Paolo Carlini committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  // - a function pointer type (unary)
  template<typename _Res, typename _T1>
    struct _Reference_wrapper_base<_Res(*)(_T1)>
    : unary_function<_T1, _Res>
    { };

  // - a function pointer type (binary)
  template<typename _Res, typename _T1, typename _T2>
    struct _Reference_wrapper_base<_Res(*)(_T1, _T2)>
    : binary_function<_T1, _T2, _Res>
    { };

  // - a pointer to member function type (unary, no qualifiers)
  template<typename _Res, typename _T1>
    struct _Reference_wrapper_base<_Res (_T1::*)()>
    : unary_function<_T1*, _Res>
    { };

  // - a pointer to member function type (binary, no qualifiers)
  template<typename _Res, typename _T1, typename _T2>
    struct _Reference_wrapper_base<_Res (_T1::*)(_T2)>
    : binary_function<_T1*, _T2, _Res>
    { };

  // - a pointer to member function type (unary, const)
  template<typename _Res, typename _T1>
    struct _Reference_wrapper_base<_Res (_T1::*)() const>
    : unary_function<const _T1*, _Res>
    { };

  // - a pointer to member function type (binary, const)
  template<typename _Res, typename _T1, typename _T2>
    struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const>
    : binary_function<const _T1*, _T2, _Res>
    { };

  // - a pointer to member function type (unary, volatile)
  template<typename _Res, typename _T1>
    struct _Reference_wrapper_base<_Res (_T1::*)() volatile>
    : unary_function<volatile _T1*, _Res>
    { };

  // - a pointer to member function type (binary, volatile)
  template<typename _Res, typename _T1, typename _T2>
    struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile>
    : binary_function<volatile _T1*, _T2, _Res>
    { };

  // - a pointer to member function type (unary, const volatile)
  template<typename _Res, typename _T1>
    struct _Reference_wrapper_base<_Res (_T1::*)() const volatile>
    : unary_function<const volatile _T1*, _Res>
    { };

  // - a pointer to member function type (binary, const volatile)
  template<typename _Res, typename _T1, typename _T2>
    struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile>
    : binary_function<const volatile _T1*, _T2, _Res>
    { };

392 393 394 395 396
  /**
   *  @brief Primary class template for reference_wrapper.
   *  @ingroup functors
   *  @{
   */
Paolo Carlini committed
397 398 399 400 401
  template<typename _Tp>
    class reference_wrapper
    : public _Reference_wrapper_base<typename remove_cv<_Tp>::type>
    {
      _Tp* _M_data;
402

Paolo Carlini committed
403 404 405
    public:
      typedef _Tp type;

406
      reference_wrapper(_Tp& __indata) noexcept
Paolo Carlini committed
407
      : _M_data(std::__addressof(__indata))
Paolo Carlini committed
408 409
      { }

410 411
      reference_wrapper(_Tp&&) = delete;

412
      reference_wrapper(const reference_wrapper&) = default;
Paolo Carlini committed
413 414

      reference_wrapper&
415
      operator=(const reference_wrapper&) = default;
Paolo Carlini committed
416

417
      operator _Tp&() const noexcept
Paolo Carlini committed
418 419 420
      { return this->get(); }

      _Tp&
421
      get() const noexcept
Paolo Carlini committed
422 423 424
      { return *_M_data; }

      template<typename... _Args>
425
	typename result_of<_Tp&(_Args&&...)>::type
426 427
	operator()(_Args&&... __args) const
	{
428
	  return __invoke(get(), std::forward<_Args>(__args)...);
Paolo Carlini committed
429 430 431 432
	}
    };


433
  /// Denotes a reference should be taken to a variable.
Paolo Carlini committed
434 435
  template<typename _Tp>
    inline reference_wrapper<_Tp>
436
    ref(_Tp& __t) noexcept
Paolo Carlini committed
437 438
    { return reference_wrapper<_Tp>(__t); }

439
  /// Denotes a const reference should be taken to a variable.
Paolo Carlini committed
440 441
  template<typename _Tp>
    inline reference_wrapper<const _Tp>
442
    cref(const _Tp& __t) noexcept
Paolo Carlini committed
443 444
    { return reference_wrapper<const _Tp>(__t); }

445 446 447 448 449 450
  template<typename _Tp>
    void ref(const _Tp&&) = delete;

  template<typename _Tp>
    void cref(const _Tp&&) = delete;

451
  /// Partial specialization.
Paolo Carlini committed
452 453
  template<typename _Tp>
    inline reference_wrapper<_Tp>
454
    ref(reference_wrapper<_Tp> __t) noexcept
Paolo Carlini committed
455 456
    { return ref(__t.get()); }

457
  /// Partial specialization.
Paolo Carlini committed
458 459
  template<typename _Tp>
    inline reference_wrapper<const _Tp>
460
    cref(reference_wrapper<_Tp> __t) noexcept
Paolo Carlini committed
461 462
    { return cref(__t.get()); }

463 464
  // @} group functors

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
  template<typename... _Types>
    struct _Pack : integral_constant<size_t, sizeof...(_Types)>
    { };

  template<typename _From, typename _To, bool = _From::value == _To::value>
    struct _AllConvertible : false_type
    { };

  template<typename... _From, typename... _To>
    struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true>
    : __and_<is_convertible<_From, _To>...>
    { };

  template<typename _Tp1, typename _Tp2>
    using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type,
				    typename std::decay<_Tp2>::type>>;

Paolo Carlini committed
482 483 484 485 486
  /**
   * Derives from @c unary_function or @c binary_function, or perhaps
   * nothing, depending on the number of arguments provided. The
   * primary template is the basis case, which derives nothing.
   */
487
  template<typename _Res, typename... _ArgTypes>
Paolo Carlini committed
488 489
    struct _Maybe_unary_or_binary_function { };

490 491
  /// Derives from @c unary_function, as appropriate.
  template<typename _Res, typename _T1>
Paolo Carlini committed
492 493 494
    struct _Maybe_unary_or_binary_function<_Res, _T1>
    : std::unary_function<_T1, _Res> { };

495 496
  /// Derives from @c binary_function, as appropriate.
  template<typename _Res, typename _T1, typename _T2>
Paolo Carlini committed
497 498 499
    struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
    : std::binary_function<_T1, _T2, _Res> { };

500 501 502
  template<typename _Signature>
    struct _Mem_fn_traits;

Paolo Carlini committed
503
  template<typename _Res, typename _Class, typename... _ArgTypes>
504
    struct _Mem_fn_traits_base
Paolo Carlini committed
505
    {
506 507 508 509 510
      using __result_type = _Res;
      using __class_type =  _Class;
      using __arg_types = _Pack<_ArgTypes...>;
      using __maybe_type
	= _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
511
      using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
Paolo Carlini committed
512 513
    };

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
#define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL)		\
  template<typename _Res, typename _Class, typename... _ArgTypes>	\
    struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF>	\
    : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...>		\
    {									\
      using __pmf_type  = _Res (_Class::*)(_ArgTypes...) _CV _REF;	\
      using __lvalue = _LVAL;						\
      using __rvalue = _RVAL;						\
      using __vararg = false_type;					\
    };									\
  template<typename _Res, typename _Class, typename... _ArgTypes>	\
    struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF>	\
    : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...>		\
    {									\
      using __pmf_type  = _Res (_Class::*)(_ArgTypes... ...) _CV _REF;	\
      using __lvalue = _LVAL;						\
      using __rvalue = _RVAL;						\
      using __vararg = true_type;					\
532
    };
Paolo Carlini committed
533

534 535 536 537 538
#define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL)		\
  _GLIBCXX_MEM_FN_TRAITS2(		, _REF, _LVAL, _RVAL)	\
  _GLIBCXX_MEM_FN_TRAITS2(const		, _REF, _LVAL, _RVAL)	\
  _GLIBCXX_MEM_FN_TRAITS2(volatile	, _REF, _LVAL, _RVAL)	\
  _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL)
Paolo Carlini committed
539

540 541 542
_GLIBCXX_MEM_FN_TRAITS( , true_type, true_type)
_GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type)
_GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
Paolo Carlini committed
543

544 545
#undef _GLIBCXX_MEM_FN_TRAITS
#undef _GLIBCXX_MEM_FN_TRAITS2
546

547 548 549 550 551 552
  template<typename _MemFunPtr,
	   bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
    class _Mem_fn_base
    : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
    {
      using _Traits = _Mem_fn_traits<_MemFunPtr>;
Paolo Carlini committed
553

554 555 556
      using _Class = typename _Traits::__class_type;
      using _ArgTypes = typename _Traits::__arg_types;
      using _Pmf = typename _Traits::__pmf_type;
Paolo Carlini committed
557

558 559
      using _Arity = typename _Traits::__arity;
      using _Varargs = typename _Traits::__vararg;
560

561 562
      template<typename _Func, typename... _BoundArgs>
	friend struct _Bind_check_arity;
563

564 565 566 567 568 569 570
      // for varargs functions we just check the number of arguments,
      // otherwise we also check they are convertible.
      template<typename _Args>
	using _CheckArgs = typename conditional<_Varargs::value,
	  __bool_constant<(_Args::value >= _ArgTypes::value)>,
	  _AllConvertible<_Args, _ArgTypes>
	>::type;
Paolo Carlini committed
571 572

    public:
573 574
      using result_type = typename _Traits::__result_type;

575
      explicit _Mem_fn_base(_Pmf __pmf) : _M_pmf(__pmf) { }
Paolo Carlini committed
576 577

      // Handle objects
578 579
      template<typename... _Args, typename _Req
               = _Require<typename _Traits::__lvalue,
580
                          _CheckArgs<_Pack<_Args...>>>>
581 582 583 584 585 586
	result_type
	operator()(_Class& __object, _Args&&... __args) const
	{ return (__object.*_M_pmf)(std::forward<_Args>(__args)...); }

      template<typename... _Args, typename _Req
               = _Require<typename _Traits::__rvalue,
587
                          _CheckArgs<_Pack<_Args...>>>>
588 589
	result_type
	operator()(_Class&& __object, _Args&&... __args) const
590
	{
591
	  return (std::move(__object).*_M_pmf)(std::forward<_Args>(__args)...);
592
	}
Paolo Carlini committed
593 594

      // Handle pointers
595 596
      template<typename... _Args, typename _Req
               = _Require<typename _Traits::__lvalue,
597
                          _CheckArgs<_Pack<_Args...>>>>
598 599 600
	result_type
	operator()(_Class* __object, _Args&&... __args) const
	{ return (__object->*_M_pmf)(std::forward<_Args>(__args)...); }
Paolo Carlini committed
601 602

      // Handle smart pointers, references and pointers to derived
603 604
      template<typename _Tp, typename... _Args, typename _Req
               = _Require<_NotSame<_Class, _Tp>, _NotSame<_Class*, _Tp>,
605
                          _CheckArgs<_Pack<_Args...>>>>
606 607
	result_type
	operator()(_Tp&& __object, _Args&&... __args) const
608
	{
609
	  return _M_call(std::forward<_Tp>(__object), &__object,
610
	      std::forward<_Args>(__args)...);
611
	}
Paolo Carlini committed
612

613 614
      // Handle reference wrappers
      template<typename _Tp, typename... _Args, typename _Req
615 616
               = _Require<is_base_of<_Class, _Tp>, typename _Traits::__lvalue,
                          _CheckArgs<_Pack<_Args...>>>>
617
	result_type
618 619
	operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
	{ return operator()(__ref.get(), std::forward<_Args>(__args)...); }
620

Paolo Carlini committed
621
    private:
622 623 624 625 626 627 628 629 630 631 632 633 634 635
      template<typename _Tp, typename... _Args>
	result_type
	_M_call(_Tp&& __object, const volatile _Class *,
		_Args&&... __args) const
	{
	  return (std::forward<_Tp>(__object).*_M_pmf)
	    (std::forward<_Args>(__args)...);
	}

      template<typename _Tp, typename... _Args>
	result_type
	_M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const
	{ return ((*__ptr).*_M_pmf)(std::forward<_Args>(__args)...); }

636
      _Pmf _M_pmf;
637 638
    };

639
  // Partial specialization for member object pointers.
Paolo Carlini committed
640
  template<typename _Res, typename _Class>
641
    class _Mem_fn_base<_Res _Class::*, false>
Paolo Carlini committed
642
    {
643 644
      using __pm_type = _Res _Class::*;

Paolo Carlini committed
645 646
      // This bit of genius is due to Peter Dimov, improved slightly by
      // Douglas Gregor.
647
      // Made less elegant to support perfect forwarding and noexcept.
Paolo Carlini committed
648
      template<typename _Tp>
649 650 651
	auto
	_M_call(_Tp&& __object, const _Class *) const noexcept
	-> decltype(std::forward<_Tp>(__object).*std::declval<__pm_type&>())
652
	{ return std::forward<_Tp>(__object).*_M_pm; }
Paolo Carlini committed
653 654

      template<typename _Tp, typename _Up>
655 656 657
	auto
	_M_call(_Tp&& __object, _Up * const *) const noexcept
	-> decltype((*std::forward<_Tp>(__object)).*std::declval<__pm_type&>())
658
	{ return (*std::forward<_Tp>(__object)).*_M_pm; }
Paolo Carlini committed
659 660

      template<typename _Tp>
661 662 663 664
	auto
	_M_call(_Tp&& __ptr, const volatile void*) const
	noexcept(noexcept((*__ptr).*std::declval<__pm_type&>()))
	-> decltype((*__ptr).*std::declval<__pm_type&>())
665
	{ return (*__ptr).*_M_pm; }
Paolo Carlini committed
666

667
      using _Arity = integral_constant<size_t, 0>;
668 669 670 671
      using _Varargs = false_type;

      template<typename _Func, typename... _BoundArgs>
	friend struct _Bind_check_arity;
672

673
    public:
Paolo Carlini committed
674
      explicit
675
      _Mem_fn_base(_Res _Class::*__pm) noexcept : _M_pm(__pm) { }
Paolo Carlini committed
676 677 678

      // Handle objects
      _Res&
679
      operator()(_Class& __object) const noexcept
680
      { return __object.*_M_pm; }
Paolo Carlini committed
681 682

      const _Res&
683
      operator()(const _Class& __object) const noexcept
684
      { return __object.*_M_pm; }
Paolo Carlini committed
685

686 687
      _Res&&
      operator()(_Class&& __object) const noexcept
688
      { return std::forward<_Class>(__object).*_M_pm; }
689 690 691

      const _Res&&
      operator()(const _Class&& __object) const noexcept
692
      { return std::forward<const _Class>(__object).*_M_pm; }
693

Paolo Carlini committed
694 695
      // Handle pointers
      _Res&
696
      operator()(_Class* __object) const noexcept
697
      { return __object->*_M_pm; }
Paolo Carlini committed
698 699

      const _Res&
700
      operator()(const _Class* __object) const noexcept
701
      { return __object->*_M_pm; }
Paolo Carlini committed
702 703

      // Handle smart pointers and derived
704
      template<typename _Tp, typename _Req = _Require<_NotSame<_Class*, _Tp>>>
705 706
	auto
	operator()(_Tp&& __unknown) const
707
	noexcept(noexcept(std::declval<_Mem_fn_base*>()->_M_call
708 709 710 711
			  (std::forward<_Tp>(__unknown), &__unknown)))
	-> decltype(this->_M_call(std::forward<_Tp>(__unknown), &__unknown))
	{ return _M_call(std::forward<_Tp>(__unknown), &__unknown); }

712
      template<typename _Tp, typename _Req = _Require<is_base_of<_Class, _Tp>>>
713 714
	auto
	operator()(reference_wrapper<_Tp> __ref) const
715
	noexcept(noexcept(std::declval<_Mem_fn_base&>()(__ref.get())))
716
	-> decltype((*this)(__ref.get()))
717
	{ return (*this)(__ref.get()); }
Paolo Carlini committed
718 719

    private:
720 721 722 723 724 725 726 727
      _Res _Class::*_M_pm;
    };

  template<typename _Res, typename _Class>
    struct _Mem_fn<_Res _Class::*>
    : _Mem_fn_base<_Res _Class::*>
    {
      using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
Paolo Carlini committed
728 729
    };

730 731
  // _GLIBCXX_RESOLVE_LIB_DEFECTS
  // 2048.  Unnecessary mem_fn overloads
Paolo Carlini committed
732 733 734
  /**
   *  @brief Returns a function object that forwards to the member
   *  pointer @a pm.
735
   *  @ingroup functors
Paolo Carlini committed
736 737 738
   */
  template<typename _Tp, typename _Class>
    inline _Mem_fn<_Tp _Class::*>
739
    mem_fn(_Tp _Class::* __pm) noexcept
Paolo Carlini committed
740 741 742 743 744 745 746 747
    {
      return _Mem_fn<_Tp _Class::*>(__pm);
    }

  /**
   *  @brief Determines if the given type _Tp is a function object
   *  should be treated as a subexpression when evaluating calls to
   *  function objects returned by bind(). [TR1 3.6.1]
748
   *  @ingroup binders
Paolo Carlini committed
749 750 751
   */
  template<typename _Tp>
    struct is_bind_expression
752
    : public false_type { };
Paolo Carlini committed
753 754 755 756

  /**
   *  @brief Determines if the given type _Tp is a placeholder in a
   *  bind() expression and, if so, which placeholder it is. [TR1 3.6.2]
757
   *  @ingroup binders
Paolo Carlini committed
758 759 760
   */
  template<typename _Tp>
    struct is_placeholder
761 762
    : public integral_constant<int, 0>
    { };
Paolo Carlini committed
763

764 765 766
  /** @brief The type of placeholder objects defined by libstdc++.
   *  @ingroup binders
   */
Paolo Carlini committed
767 768
  template<int _Num> struct _Placeholder { };

769 770
  _GLIBCXX_END_NAMESPACE_VERSION

Paolo Carlini committed
771
  /** @namespace std::placeholders
772
   *  @brief ISO C++11 entities sub-namespace for functional.
773
   *  @ingroup binders
Paolo Carlini committed
774
   */
775
  namespace placeholders
776
  {
777
  _GLIBCXX_BEGIN_NAMESPACE_VERSION
778 779 780 781
  /* Define a large number of placeholders. There is no way to
   * simplify this with variadic templates, because we're introducing
   * unique names for each.
   */
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
    extern const _Placeholder<1> _1;
    extern const _Placeholder<2> _2;
    extern const _Placeholder<3> _3;
    extern const _Placeholder<4> _4;
    extern const _Placeholder<5> _5;
    extern const _Placeholder<6> _6;
    extern const _Placeholder<7> _7;
    extern const _Placeholder<8> _8;
    extern const _Placeholder<9> _9;
    extern const _Placeholder<10> _10;
    extern const _Placeholder<11> _11;
    extern const _Placeholder<12> _12;
    extern const _Placeholder<13> _13;
    extern const _Placeholder<14> _14;
    extern const _Placeholder<15> _15;
    extern const _Placeholder<16> _16;
    extern const _Placeholder<17> _17;
    extern const _Placeholder<18> _18;
    extern const _Placeholder<19> _19;
    extern const _Placeholder<20> _20;
    extern const _Placeholder<21> _21;
    extern const _Placeholder<22> _22;
    extern const _Placeholder<23> _23;
    extern const _Placeholder<24> _24;
    extern const _Placeholder<25> _25;
    extern const _Placeholder<26> _26;
    extern const _Placeholder<27> _27;
    extern const _Placeholder<28> _28;
    extern const _Placeholder<29> _29;
811
  _GLIBCXX_END_NAMESPACE_VERSION
Paolo Carlini committed
812 813
  }

814 815
  _GLIBCXX_BEGIN_NAMESPACE_VERSION

Paolo Carlini committed
816 817 818
  /**
   *  Partial specialization of is_placeholder that provides the placeholder
   *  number for the placeholder objects defined by libstdc++.
819
   *  @ingroup binders
Paolo Carlini committed
820 821 822
   */
  template<int _Num>
    struct is_placeholder<_Placeholder<_Num> >
823 824
    : public integral_constant<int, _Num>
    { };
Paolo Carlini committed
825

826 827 828 829 830
  template<int _Num>
    struct is_placeholder<const _Placeholder<_Num> >
    : public integral_constant<int, _Num>
    { };

831
  /**
Paolo Carlini committed
832 833 834 835 836 837 838 839 840 841
   * Used by _Safe_tuple_element to indicate that there is no tuple
   * element at this position.
   */
  struct _No_tuple_element;

  /**
   * Implementation helper for _Safe_tuple_element. This primary
   * template handles the case where it is safe to use @c
   * tuple_element.
   */
842
  template<std::size_t __i, typename _Tuple, bool _IsSafe>
Paolo Carlini committed
843 844 845 846 847 848 849 850
    struct _Safe_tuple_element_impl
    : tuple_element<__i, _Tuple> { };

  /**
   * Implementation helper for _Safe_tuple_element. This partial
   * specialization handles the case where it is not safe to use @c
   * tuple_element. We just return @c _No_tuple_element.
   */
851
  template<std::size_t __i, typename _Tuple>
Paolo Carlini committed
852 853 854 855 856 857 858 859 860
    struct _Safe_tuple_element_impl<__i, _Tuple, false>
    {
      typedef _No_tuple_element type;
    };

  /**
   * Like tuple_element, but returns @c _No_tuple_element when
   * tuple_element would return an error.
   */
861
 template<std::size_t __i, typename _Tuple>
Paolo Carlini committed
862
   struct _Safe_tuple_element
863
   : _Safe_tuple_element_impl<__i, _Tuple,
864
			      (__i < tuple_size<_Tuple>::value)>
865
   { };
Paolo Carlini committed
866 867 868 869 870 871 872 873 874 875 876 877 878

  /**
   *  Maps an argument to bind() into an actual argument to the bound
   *  function object [TR1 3.6.3/5]. Only the first parameter should
   *  be specified: the rest are used to determine among the various
   *  implementations. Note that, although this class is a function
   *  object, it isn't entirely normal because it takes only two
   *  parameters regardless of the number of parameters passed to the
   *  bind expression. The first parameter is the bound argument and
   *  the second parameter is a tuple containing references to the
   *  rest of the arguments.
   */
  template<typename _Arg,
879 880
	   bool _IsBindExp = is_bind_expression<_Arg>::value,
	   bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
Paolo Carlini committed
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
    class _Mu;

  /**
   *  If the argument is reference_wrapper<_Tp>, returns the
   *  underlying reference. [TR1 3.6.3/5 bullet 1]
   */
  template<typename _Tp>
    class _Mu<reference_wrapper<_Tp>, false, false>
    {
    public:
      typedef _Tp& result_type;

      /* Note: This won't actually work for const volatile
       * reference_wrappers, because reference_wrapper::get() is const
       * but not volatile-qualified. This might be a defect in the TR.
       */
      template<typename _CVRef, typename _Tuple>
898 899 900
	result_type
	operator()(_CVRef& __arg, _Tuple&) const volatile
	{ return __arg.get(); }
Paolo Carlini committed
901 902 903 904 905 906 907 908 909 910 911 912
    };

  /**
   *  If the argument is a bind expression, we invoke the underlying
   *  function object with the same cv-qualifiers as we are given and
   *  pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2]
   */
  template<typename _Arg>
    class _Mu<_Arg, true, false>
    {
    public:
      template<typename _CVArg, typename... _Args>
913 914
	auto
	operator()(_CVArg& __arg,
915
		   tuple<_Args...>& __tuple) const volatile
916 917
	-> decltype(__arg(declval<_Args>()...))
	{
Paolo Carlini committed
918 919 920
	  // Construct an index tuple and forward to __call
	  typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
	    _Indexes;
921
	  return this->__call(__arg, __tuple, _Indexes());
Paolo Carlini committed
922 923 924 925
	}

    private:
      // Invokes the underlying function object __arg by unpacking all
926
      // of the arguments in the tuple.
927
      template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
928 929
	auto
	__call(_CVArg& __arg, tuple<_Args...>& __tuple,
Paolo Carlini committed
930
	       const _Index_tuple<_Indexes...>&) const volatile
931 932
	-> decltype(__arg(declval<_Args>()...))
	{
933
	  return __arg(std::forward<_Args>(std::get<_Indexes>(__tuple))...);
Paolo Carlini committed
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
	}
    };

  /**
   *  If the argument is a placeholder for the Nth argument, returns
   *  a reference to the Nth argument to the bind function object.
   *  [TR1 3.6.3/5 bullet 3]
   */
  template<typename _Arg>
    class _Mu<_Arg, false, true>
    {
    public:
      template<typename _Signature> class result;

      template<typename _CVMu, typename _CVArg, typename _Tuple>
949 950
	class result<_CVMu(_CVArg, _Tuple)>
	{
Paolo Carlini committed
951 952 953 954 955 956 957 958
	  // Add a reference, if it hasn't already been done for us.
	  // This allows us to be a little bit sloppy in constructing
	  // the tuple that we pass to result_of<...>.
	  typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value
						- 1), _Tuple>::type
	    __base_type;

	public:
959
	  typedef typename add_rvalue_reference<__base_type>::type type;
Paolo Carlini committed
960 961 962
	};

      template<typename _Tuple>
963 964 965
	typename result<_Mu(_Arg, _Tuple)>::type
	operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
	{
966
	  return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>(
967
	      ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple));
Paolo Carlini committed
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
	}
    };

  /**
   *  If the argument is just a value, returns a reference to that
   *  value. The cv-qualifiers on the reference are the same as the
   *  cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4]
   */
  template<typename _Arg>
    class _Mu<_Arg, false, false>
    {
    public:
      template<typename _Signature> struct result;

      template<typename _CVMu, typename _CVArg, typename _Tuple>
983 984
	struct result<_CVMu(_CVArg, _Tuple)>
	{
Paolo Carlini committed
985 986 987 988 989
	  typedef typename add_lvalue_reference<_CVArg>::type type;
	};

      // Pick up the cv-qualifiers of the argument
      template<typename _CVArg, typename _Tuple>
990 991 992
	_CVArg&&
	operator()(_CVArg&& __arg, _Tuple&) const volatile
	{ return std::forward<_CVArg>(__arg); }
Paolo Carlini committed
993 994 995 996
    };

  /**
   *  Maps member pointers into instances of _Mem_fn but leaves all
997 998
   *  other function objects untouched. Used by std::bind(). The
   *  primary template handles the non-member-pointer case.
Paolo Carlini committed
999 1000 1001 1002 1003
   */
  template<typename _Tp>
    struct _Maybe_wrap_member_pointer
    {
      typedef _Tp type;
1004

Paolo Carlini committed
1005 1006 1007
      static const _Tp&
      __do_wrap(const _Tp& __x)
      { return __x; }
1008 1009 1010 1011

      static _Tp&&
      __do_wrap(_Tp&& __x)
      { return static_cast<_Tp&&>(__x); }
Paolo Carlini committed
1012 1013 1014 1015
    };

  /**
   *  Maps member pointers into instances of _Mem_fn but leaves all
1016
   *  other function objects untouched. Used by std::bind(). This
Paolo Carlini committed
1017 1018 1019 1020 1021 1022
   *  partial specialization handles the member pointer case.
   */
  template<typename _Tp, typename _Class>
    struct _Maybe_wrap_member_pointer<_Tp _Class::*>
    {
      typedef _Mem_fn<_Tp _Class::*> type;
1023

Paolo Carlini committed
1024 1025 1026 1027 1028
      static type
      __do_wrap(_Tp _Class::* __pm)
      { return type(__pm); }
    };

1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
  // Specialization needed to prevent "forming reference to void" errors when
  // bind<void>() is called, because argument deduction instantiates
  // _Maybe_wrap_member_pointer<void> outside the immediate context where
  // SFINAE applies.
  template<>
    struct _Maybe_wrap_member_pointer<void>
    {
      typedef void type;
    };

1039
  // std::get<I> for volatile-qualified tuples
1040
  template<std::size_t _Ind, typename... _Tp>
1041 1042
    inline auto
    __volget(volatile tuple<_Tp...>& __tuple)
1043
    -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
1044 1045 1046
    { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }

  // std::get<I> for const-volatile-qualified tuples
1047
  template<std::size_t _Ind, typename... _Tp>
1048 1049
    inline auto
    __volget(const volatile tuple<_Tp...>& __tuple)
1050
    -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile&
1051 1052
    { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }

Paolo Carlini committed
1053 1054 1055 1056 1057 1058 1059 1060 1061
  /// Type of the function object returned from bind().
  template<typename _Signature>
    struct _Bind;

   template<typename _Functor, typename... _Bound_args>
    class _Bind<_Functor(_Bound_args...)>
    : public _Weak_result_type<_Functor>
    {
      typedef _Bind __self_type;
1062
      typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
1063
	_Bound_indexes;
Paolo Carlini committed
1064 1065 1066 1067 1068

      _Functor _M_f;
      tuple<_Bound_args...> _M_bound_args;

      // Call unqualified
1069
      template<typename _Result, typename... _Args, std::size_t... _Indexes>
1070 1071 1072 1073
	_Result
	__call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
	{
	  return _M_f(_Mu<_Bound_args>()
1074
		      (std::get<_Indexes>(_M_bound_args), __args)...);
1075
	}
Paolo Carlini committed
1076 1077

      // Call as const
1078
      template<typename _Result, typename... _Args, std::size_t... _Indexes>
1079 1080 1081 1082
	_Result
	__call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
	{
	  return _M_f(_Mu<_Bound_args>()
1083
		      (std::get<_Indexes>(_M_bound_args), __args)...);
1084
	}
Paolo Carlini committed
1085 1086

      // Call as volatile
1087
      template<typename _Result, typename... _Args, std::size_t... _Indexes>
1088 1089
	_Result
	__call_v(tuple<_Args...>&& __args,
1090
		 _Index_tuple<_Indexes...>) volatile
1091 1092 1093 1094
	{
	  return _M_f(_Mu<_Bound_args>()
		      (__volget<_Indexes>(_M_bound_args), __args)...);
	}
Paolo Carlini committed
1095 1096

      // Call as const volatile
1097
      template<typename _Result, typename... _Args, std::size_t... _Indexes>
1098 1099
	_Result
	__call_c_v(tuple<_Args...>&& __args,
1100
		   _Index_tuple<_Indexes...>) const volatile
1101 1102 1103 1104
	{
	  return _M_f(_Mu<_Bound_args>()
		      (__volget<_Indexes>(_M_bound_args), __args)...);
	}
Paolo Carlini committed
1105 1106

     public:
1107
      template<typename... _Args>
1108 1109 1110
	explicit _Bind(const _Functor& __f, _Args&&... __args)
	: _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
	{ }
1111 1112

      template<typename... _Args>
1113 1114 1115
	explicit _Bind(_Functor&& __f, _Args&&... __args)
	: _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
	{ }
1116 1117 1118 1119 1120

      _Bind(const _Bind&) = default;

      _Bind(_Bind&& __b)
      : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1121
      { }
Paolo Carlini committed
1122 1123

      // Call unqualified
1124
      template<typename... _Args, typename _Result
1125 1126
	= decltype( std::declval<_Functor>()(
	      _Mu<_Bound_args>()( std::declval<_Bound_args&>(),
1127
				  std::declval<tuple<_Args...>&>() )... ) )>
1128 1129 1130 1131 1132 1133 1134
	_Result
	operator()(_Args&&... __args)
	{
	  return this->__call<_Result>(
	      std::forward_as_tuple(std::forward<_Args>(__args)...),
	      _Bound_indexes());
	}
Paolo Carlini committed
1135 1136

      // Call as const
1137
      template<typename... _Args, typename _Result
Paolo Carlini committed
1138 1139
	= decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
		       typename add_const<_Functor>::type>::type>()(
1140
	      _Mu<_Bound_args>()( std::declval<const _Bound_args&>(),
1141
				  std::declval<tuple<_Args...>&>() )... ) )>
1142 1143 1144 1145 1146 1147 1148
	_Result
	operator()(_Args&&... __args) const
	{
	  return this->__call_c<_Result>(
	      std::forward_as_tuple(std::forward<_Args>(__args)...),
	      _Bound_indexes());
	}
Paolo Carlini committed
1149 1150

      // Call as volatile
1151
      template<typename... _Args, typename _Result
Paolo Carlini committed
1152 1153
	= decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
                       typename add_volatile<_Functor>::type>::type>()(
1154 1155 1156 1157 1158 1159 1160 1161 1162
	      _Mu<_Bound_args>()( std::declval<volatile _Bound_args&>(),
				  std::declval<tuple<_Args...>&>() )... ) )>
	_Result
	operator()(_Args&&... __args) volatile
	{
	  return this->__call_v<_Result>(
	      std::forward_as_tuple(std::forward<_Args>(__args)...),
	      _Bound_indexes());
	}
Paolo Carlini committed
1163 1164

      // Call as const volatile
1165
      template<typename... _Args, typename _Result
Paolo Carlini committed
1166 1167
	= decltype( std::declval<typename enable_if<(sizeof...(_Args) >= 0),
                       typename add_cv<_Functor>::type>::type>()(
1168 1169 1170 1171 1172 1173 1174 1175 1176
	      _Mu<_Bound_args>()( std::declval<const volatile _Bound_args&>(),
				  std::declval<tuple<_Args...>&>() )... ) )>
	_Result
	operator()(_Args&&... __args) const volatile
	{
	  return this->__call_c_v<_Result>(
	      std::forward_as_tuple(std::forward<_Args>(__args)...),
	      _Bound_indexes());
	}
Paolo Carlini committed
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
    };

  /// Type of the function object returned from bind<R>().
  template<typename _Result, typename _Signature>
    struct _Bind_result;

  template<typename _Result, typename _Functor, typename... _Bound_args>
    class _Bind_result<_Result, _Functor(_Bound_args...)>
    {
      typedef _Bind_result __self_type;
1187 1188
      typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
	_Bound_indexes;
Paolo Carlini committed
1189 1190 1191 1192

      _Functor _M_f;
      tuple<_Bound_args...> _M_bound_args;

1193 1194
      // sfinae types
      template<typename _Res>
1195
	struct __enable_if_void : enable_if<is_void<_Res>::value, int> { };
1196
      template<typename _Res>
1197
	struct __disable_if_void : enable_if<!is_void<_Res>::value, int> { };
1198

Paolo Carlini committed
1199
      // Call unqualified
1200
      template<typename _Res, typename... _Args, std::size_t... _Indexes>
1201 1202 1203 1204 1205
	_Result
	__call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
	    typename __disable_if_void<_Res>::type = 0)
	{
	  return _M_f(_Mu<_Bound_args>()
1206
		      (std::get<_Indexes>(_M_bound_args), __args)...);
1207
	}
Paolo Carlini committed
1208

1209
      // Call unqualified, return void
1210
      template<typename _Res, typename... _Args, std::size_t... _Indexes>
1211 1212 1213 1214 1215
	void
	__call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
	    typename __enable_if_void<_Res>::type = 0)
	{
	  _M_f(_Mu<_Bound_args>()
1216
	       (std::get<_Indexes>(_M_bound_args), __args)...);
1217
	}
1218

Paolo Carlini committed
1219
      // Call as const
1220
      template<typename _Res, typename... _Args, std::size_t... _Indexes>
1221 1222 1223 1224 1225
	_Result
	__call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
	    typename __disable_if_void<_Res>::type = 0) const
	{
	  return _M_f(_Mu<_Bound_args>()
1226
		      (std::get<_Indexes>(_M_bound_args), __args)...);
1227
	}
Paolo Carlini committed
1228

1229
      // Call as const, return void
1230
      template<typename _Res, typename... _Args, std::size_t... _Indexes>
1231 1232 1233 1234 1235
	void
	__call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
	    typename __enable_if_void<_Res>::type = 0) const
	{
	  _M_f(_Mu<_Bound_args>()
1236
	       (std::get<_Indexes>(_M_bound_args),  __args)...);
1237
	}
1238

Paolo Carlini committed
1239
      // Call as volatile
1240
      template<typename _Res, typename... _Args, std::size_t... _Indexes>
1241 1242 1243 1244 1245 1246 1247
	_Result
	__call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
	    typename __disable_if_void<_Res>::type = 0) volatile
	{
	  return _M_f(_Mu<_Bound_args>()
		      (__volget<_Indexes>(_M_bound_args), __args)...);
	}
Paolo Carlini committed
1248

1249
      // Call as volatile, return void
1250
      template<typename _Res, typename... _Args, std::size_t... _Indexes>
1251 1252 1253 1254 1255
	void
	__call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
	    typename __enable_if_void<_Res>::type = 0) volatile
	{
	  _M_f(_Mu<_Bound_args>()
1256
	       (__volget<_Indexes>(_M_bound_args), __args)...);
1257
	}
1258

Paolo Carlini committed
1259
      // Call as const volatile
1260
      template<typename _Res, typename... _Args, std::size_t... _Indexes>
1261 1262 1263 1264 1265 1266 1267
	_Result
	__call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>,
	    typename __disable_if_void<_Res>::type = 0) const volatile
	{
	  return _M_f(_Mu<_Bound_args>()
		      (__volget<_Indexes>(_M_bound_args), __args)...);
	}
Paolo Carlini committed
1268

1269
      // Call as const volatile, return void
1270
      template<typename _Res, typename... _Args, std::size_t... _Indexes>
1271 1272 1273 1274 1275 1276
	void
	__call(tuple<_Args...>&& __args,
	       _Index_tuple<_Indexes...>,
	    typename __enable_if_void<_Res>::type = 0) const volatile
	{
	  _M_f(_Mu<_Bound_args>()
1277
	       (__volget<_Indexes>(_M_bound_args), __args)...);
1278
	}
1279

Paolo Carlini committed
1280 1281 1282
    public:
      typedef _Result result_type;

1283
      template<typename... _Args>
1284 1285 1286
	explicit _Bind_result(const _Functor& __f, _Args&&... __args)
	: _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
	{ }
1287 1288

      template<typename... _Args>
1289 1290 1291
	explicit _Bind_result(_Functor&& __f, _Args&&... __args)
	: _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
	{ }
1292 1293 1294 1295 1296

      _Bind_result(const _Bind_result&) = default;

      _Bind_result(_Bind_result&& __b)
      : _M_f(std::move(__b._M_f)), _M_bound_args(std::move(__b._M_bound_args))
1297
      { }
Paolo Carlini committed
1298 1299 1300

      // Call unqualified
      template<typename... _Args>
1301 1302 1303 1304 1305 1306 1307
	result_type
	operator()(_Args&&... __args)
	{
	  return this->__call<_Result>(
	      std::forward_as_tuple(std::forward<_Args>(__args)...),
	      _Bound_indexes());
	}
Paolo Carlini committed
1308 1309 1310

      // Call as const
      template<typename... _Args>
1311 1312 1313 1314 1315 1316 1317
	result_type
	operator()(_Args&&... __args) const
	{
	  return this->__call<_Result>(
	      std::forward_as_tuple(std::forward<_Args>(__args)...),
	      _Bound_indexes());
	}
Paolo Carlini committed
1318 1319 1320

      // Call as volatile
      template<typename... _Args>
1321 1322 1323 1324 1325 1326 1327
	result_type
	operator()(_Args&&... __args) volatile
	{
	  return this->__call<_Result>(
	      std::forward_as_tuple(std::forward<_Args>(__args)...),
	      _Bound_indexes());
	}
Paolo Carlini committed
1328 1329 1330

      // Call as const volatile
      template<typename... _Args>
1331 1332 1333 1334 1335 1336 1337
	result_type
	operator()(_Args&&... __args) const volatile
	{
	  return this->__call<_Result>(
	      std::forward_as_tuple(std::forward<_Args>(__args)...),
	      _Bound_indexes());
	}
Paolo Carlini committed
1338 1339
    };

1340 1341 1342 1343
  /**
   *  @brief Class template _Bind is always a bind expression.
   *  @ingroup binders
   */
Paolo Carlini committed
1344 1345
  template<typename _Signature>
    struct is_bind_expression<_Bind<_Signature> >
1346
    : public true_type { };
Paolo Carlini committed
1347

1348 1349 1350 1351
  /**
   *  @brief Class template _Bind is always a bind expression.
   *  @ingroup binders
   */
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
  template<typename _Signature>
    struct is_bind_expression<const _Bind<_Signature> >
    : public true_type { };

  /**
   *  @brief Class template _Bind is always a bind expression.
   *  @ingroup binders
   */
  template<typename _Signature>
    struct is_bind_expression<volatile _Bind<_Signature> >
    : public true_type { };

  /**
   *  @brief Class template _Bind is always a bind expression.
   *  @ingroup binders
   */
  template<typename _Signature>
    struct is_bind_expression<const volatile _Bind<_Signature>>
    : public true_type { };

  /**
   *  @brief Class template _Bind_result is always a bind expression.
   *  @ingroup binders
   */
  template<typename _Result, typename _Signature>
    struct is_bind_expression<_Bind_result<_Result, _Signature>>
    : public true_type { };

  /**
   *  @brief Class template _Bind_result is always a bind expression.
   *  @ingroup binders
   */
  template<typename _Result, typename _Signature>
    struct is_bind_expression<const _Bind_result<_Result, _Signature>>
    : public true_type { };

  /**
   *  @brief Class template _Bind_result is always a bind expression.
   *  @ingroup binders
   */
  template<typename _Result, typename _Signature>
    struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
    : public true_type { };

  /**
   *  @brief Class template _Bind_result is always a bind expression.
   *  @ingroup binders
   */
Paolo Carlini committed
1400
  template<typename _Result, typename _Signature>
1401
    struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
1402
    : public true_type { };
Paolo Carlini committed
1403

1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
  template<typename _Func, typename... _BoundArgs>
    struct _Bind_check_arity { };

  template<typename _Ret, typename... _Args, typename... _BoundArgs>
    struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
    {
      static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
                   "Wrong number of arguments for function");
    };

1414 1415 1416 1417 1418 1419 1420
  template<typename _Ret, typename... _Args, typename... _BoundArgs>
    struct _Bind_check_arity<_Ret (*)(_Args......), _BoundArgs...>
    {
      static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
                   "Wrong number of arguments for function");
    };

1421 1422 1423 1424
  template<typename _Tp, typename _Class, typename... _BoundArgs>
    struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
    {
      using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
1425 1426 1427 1428 1429
      using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
      static_assert(_Varargs::value
		    ? sizeof...(_BoundArgs) >= _Arity::value + 1
		    : sizeof...(_BoundArgs) == _Arity::value + 1,
		    "Wrong number of arguments for pointer-to-member");
1430 1431
    };

1432 1433 1434
  // Trait type used to remove std::bind() from overload set via SFINAE
  // when first argument has integer type, so that std::bind() will
  // not be a better match than ::bind() from the BSD Sockets API.
1435 1436
  template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
    using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
1437 1438

  template<bool _SocketLike, typename _Func, typename... _BoundArgs>
1439
    struct _Bind_helper
1440
    : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1441
    {
1442
      typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1443
	__maybe_type;
1444 1445
      typedef typename __maybe_type::type __func_type;
      typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
1446 1447
    };

1448 1449 1450 1451 1452 1453 1454
  // Partial specialization for is_socketlike == true, does not define
  // nested type so std::bind() will not participate in overload resolution
  // when the first argument might be a socket file descriptor.
  template<typename _Func, typename... _BoundArgs>
    struct _Bind_helper<true, _Func, _BoundArgs...>
    { };

1455 1456 1457 1458
  /**
   *  @brief Function template for std::bind.
   *  @ingroup binders
   */
1459 1460 1461 1462
  template<typename _Func, typename... _BoundArgs>
    inline typename
    _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
    bind(_Func&& __f, _BoundArgs&&... __args)
Paolo Carlini committed
1463
    {
1464
      typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
1465 1466
      typedef typename __helper_type::__maybe_type __maybe_type;
      typedef typename __helper_type::type __result_type;
1467 1468
      return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
			   std::forward<_BoundArgs>(__args)...);
1469
    }
Paolo Carlini committed
1470

1471
  template<typename _Result, typename _Func, typename... _BoundArgs>
1472
    struct _Bindres_helper
1473
    : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1474
    {
1475
      typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
1476
	__maybe_type;
1477 1478
      typedef typename __maybe_type::type __functor_type;
      typedef _Bind_result<_Result,
1479
			   __functor_type(typename decay<_BoundArgs>::type...)>
1480
	type;
1481 1482
    };

1483
  /**
1484
   *  @brief Function template for std::bind<R>.
1485 1486
   *  @ingroup binders
   */
1487
  template<typename _Result, typename _Func, typename... _BoundArgs>
Paolo Carlini committed
1488
    inline
1489 1490
    typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
    bind(_Func&& __f, _BoundArgs&&... __args)
Paolo Carlini committed
1491
    {
1492
      typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1493 1494
      typedef typename __helper_type::__maybe_type __maybe_type;
      typedef typename __helper_type::type __result_type;
1495 1496
      return __result_type(__maybe_type::__do_wrap(std::forward<_Func>(__f)),
			   std::forward<_BoundArgs>(__args)...);
Paolo Carlini committed
1497 1498
    }

1499 1500 1501 1502 1503 1504 1505 1506
  template<typename _Signature>
    struct _Bind_simple;

  template<typename _Callable, typename... _Args>
    struct _Bind_simple<_Callable(_Args...)>
    {
      typedef typename result_of<_Callable(_Args...)>::type result_type;

1507
      template<typename _Tp, typename... _Up>
1508
        explicit
1509 1510
        _Bind_simple(_Tp&& __f, _Up&&... __args)
        : _M_bound(std::forward<_Tp>(__f), std::forward<_Up>(__args)...)
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
        { }

      _Bind_simple(const _Bind_simple&) = default;
      _Bind_simple(_Bind_simple&&) = default;

      result_type
      operator()()
      {
        typedef typename _Build_index_tuple<sizeof...(_Args)>::__type _Indices;
        return _M_invoke(_Indices());
      }

    private:
1524
      template<std::size_t... _Indices>
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
        typename result_of<_Callable(_Args...)>::type
        _M_invoke(_Index_tuple<_Indices...>)
        {
	  // std::bind always forwards bound arguments as lvalues,
	  // but this type can call functions which only accept rvalues.
          return std::forward<_Callable>(std::get<0>(_M_bound))(
              std::forward<_Args>(std::get<_Indices+1>(_M_bound))...);
        }

      std::tuple<_Callable, _Args...> _M_bound;
    };

  template<typename _Func, typename... _BoundArgs>
    struct _Bind_simple_helper
1539
    : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
    {
      typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
        __maybe_type;
      typedef typename __maybe_type::type __func_type;
      typedef _Bind_simple<__func_type(typename decay<_BoundArgs>::type...)>
       	__type;
    };

  // Simplified version of std::bind for internal use, without support for
  // unbound arguments, placeholders or nested bind expressions.
  template<typename _Callable, typename... _Args>
    typename _Bind_simple_helper<_Callable, _Args...>::__type
    __bind_simple(_Callable&& __callable, _Args&&... __args)
    {
      typedef _Bind_simple_helper<_Callable, _Args...> __helper_type;
      typedef typename __helper_type::__maybe_type __maybe_type;
      typedef typename __helper_type::__type __result_type;
      return __result_type(
          __maybe_type::__do_wrap( std::forward<_Callable>(__callable)),
          std::forward<_Args>(__args)...);
    }

Paolo Carlini committed
1562 1563 1564 1565 1566
  /**
   *  @brief Exception class thrown when class template function's
   *  operator() is called with an empty target.
   *  @ingroup exceptions
   */
1567 1568 1569
  class bad_function_call : public std::exception
  {
  public:
1570
    virtual ~bad_function_call() noexcept;
1571 1572

    const char* what() const noexcept;
1573
  };
Paolo Carlini committed
1574 1575 1576 1577

  /**
   *  Trait identifying "location-invariant" types, meaning that the
   *  address of the object (or any of its members) will not escape.
1578 1579
   *  Trivially copyable types are location-invariant and users can
   *  specialize this trait for other types.
Paolo Carlini committed
1580 1581 1582
   */
  template<typename _Tp>
    struct __is_location_invariant
1583
    : is_trivially_copyable<_Tp>::type
1584
    { };
Paolo Carlini committed
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635

  class _Undefined_class;

  union _Nocopy_types
  {
    void*       _M_object;
    const void* _M_const_object;
    void (*_M_function_pointer)();
    void (_Undefined_class::*_M_member_pointer)();
  };

  union _Any_data
  {
    void*       _M_access()       { return &_M_pod_data[0]; }
    const void* _M_access() const { return &_M_pod_data[0]; }

    template<typename _Tp>
      _Tp&
      _M_access()
      { return *static_cast<_Tp*>(_M_access()); }

    template<typename _Tp>
      const _Tp&
      _M_access() const
      { return *static_cast<const _Tp*>(_M_access()); }

    _Nocopy_types _M_unused;
    char _M_pod_data[sizeof(_Nocopy_types)];
  };

  enum _Manager_operation
  {
    __get_type_info,
    __get_functor_ptr,
    __clone_functor,
    __destroy_functor
  };

  // Simple type wrapper that helps avoid annoying const problems
  // when casting between void pointers and pointers-to-pointers.
  template<typename _Tp>
    struct _Simple_type_wrapper
    {
      _Simple_type_wrapper(_Tp __value) : __value(__value) { }

      _Tp __value;
    };

  template<typename _Tp>
    struct __is_location_invariant<_Simple_type_wrapper<_Tp> >
    : __is_location_invariant<_Tp>
1636
    { };
Paolo Carlini committed
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647

  // Converts a reference to a function object into a callable
  // function object.
  template<typename _Functor>
    inline _Functor&
    __callable_functor(_Functor& __f)
    { return __f; }

  template<typename _Member, typename _Class>
    inline _Mem_fn<_Member _Class::*>
    __callable_functor(_Member _Class::* &__p)
1648
    { return std::mem_fn(__p); }
Paolo Carlini committed
1649 1650 1651 1652

  template<typename _Member, typename _Class>
    inline _Mem_fn<_Member _Class::*>
    __callable_functor(_Member _Class::* const &__p)
1653
    { return std::mem_fn(__p); }
Paolo Carlini committed
1654

1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
  template<typename _Member, typename _Class>
    inline _Mem_fn<_Member _Class::*>
    __callable_functor(_Member _Class::* volatile &__p)
    { return std::mem_fn(__p); }

  template<typename _Member, typename _Class>
    inline _Mem_fn<_Member _Class::*>
    __callable_functor(_Member _Class::* const volatile &__p)
    { return std::mem_fn(__p); }

Paolo Carlini committed
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
  template<typename _Signature>
    class function;

  /// Base class of all polymorphic function object wrappers.
  class _Function_base
  {
  public:
    static const std::size_t _M_max_size = sizeof(_Nocopy_types);
    static const std::size_t _M_max_align = __alignof__(_Nocopy_types);

    template<typename _Functor>
      class _Base_manager
      {
      protected:
	static const bool __stored_locally =
1680 1681 1682 1683 1684
	(__is_location_invariant<_Functor>::value
	 && sizeof(_Functor) <= _M_max_size
	 && __alignof__(_Functor) <= _M_max_align
	 && (_M_max_align % __alignof__(_Functor) == 0));

Paolo Carlini committed
1685 1686 1687 1688 1689 1690 1691
	typedef integral_constant<bool, __stored_locally> _Local_storage;

	// Retrieve a pointer to the function object
	static _Functor*
	_M_get_pointer(const _Any_data& __source)
	{
	  const _Functor* __ptr =
1692
	    __stored_locally? std::__addressof(__source._M_access<_Functor>())
Paolo Carlini committed
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
	    /* have stored a pointer */ : __source._M_access<_Functor*>();
	  return const_cast<_Functor*>(__ptr);
	}

	// Clone a location-invariant function object that fits within
	// an _Any_data structure.
	static void
	_M_clone(_Any_data& __dest, const _Any_data& __source, true_type)
	{
	  new (__dest._M_access()) _Functor(__source._M_access<_Functor>());
	}

	// Clone a function object that is not location-invariant or
	// that cannot fit into an _Any_data structure.
	static void
	_M_clone(_Any_data& __dest, const _Any_data& __source, false_type)
	{
	  __dest._M_access<_Functor*>() =
	    new _Functor(*__source._M_access<_Functor*>());
	}

	// Destroying a location-invariant object may still require
	// destruction.
	static void
	_M_destroy(_Any_data& __victim, true_type)
	{
	  __victim._M_access<_Functor>().~_Functor();
	}
1721

Paolo Carlini committed
1722 1723 1724 1725 1726 1727
	// Destroying an object located on the heap.
	static void
	_M_destroy(_Any_data& __victim, false_type)
	{
	  delete __victim._M_access<_Functor*>();
	}
1728

Paolo Carlini committed
1729 1730 1731 1732 1733 1734 1735
      public:
	static bool
	_M_manager(_Any_data& __dest, const _Any_data& __source,
		   _Manager_operation __op)
	{
	  switch (__op)
	    {
1736
#if __cpp_rtti
Paolo Carlini committed
1737 1738 1739 1740 1741 1742 1743
	    case __get_type_info:
	      __dest._M_access<const type_info*>() = &typeid(_Functor);
	      break;
#endif
	    case __get_functor_ptr:
	      __dest._M_access<_Functor*>() = _M_get_pointer(__source);
	      break;
1744

Paolo Carlini committed
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
	    case __clone_functor:
	      _M_clone(__dest, __source, _Local_storage());
	      break;

	    case __destroy_functor:
	      _M_destroy(__dest, _Local_storage());
	      break;
	    }
	  return false;
	}

	static void
1757 1758
	_M_init_functor(_Any_data& __functor, _Functor&& __f)
	{ _M_init_functor(__functor, std::move(__f), _Local_storage()); }
1759

Paolo Carlini committed
1760 1761 1762
	template<typename _Signature>
	  static bool
	  _M_not_empty_function(const function<_Signature>& __f)
1763
	  { return static_cast<bool>(__f); }
Paolo Carlini committed
1764 1765 1766

	template<typename _Tp>
	  static bool
1767
	  _M_not_empty_function(_Tp* const& __fp)
Paolo Carlini committed
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
	  { return __fp; }

	template<typename _Class, typename _Tp>
	  static bool
	  _M_not_empty_function(_Tp _Class::* const& __mp)
	  { return __mp; }

	template<typename _Tp>
	  static bool
	  _M_not_empty_function(const _Tp&)
	  { return true; }

      private:
1781
	static void
1782 1783
	_M_init_functor(_Any_data& __functor, _Functor&& __f, true_type)
	{ new (__functor._M_access()) _Functor(std::move(__f)); }
Paolo Carlini committed
1784 1785

	static void
1786 1787
	_M_init_functor(_Any_data& __functor, _Functor&& __f, false_type)
	{ __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); }
Paolo Carlini committed
1788 1789 1790 1791 1792 1793 1794
      };

    template<typename _Functor>
      class _Ref_manager : public _Base_manager<_Functor*>
      {
	typedef _Function_base::_Base_manager<_Functor*> _Base;

1795
      public:
Paolo Carlini committed
1796 1797 1798 1799 1800 1801
	static bool
	_M_manager(_Any_data& __dest, const _Any_data& __source,
		   _Manager_operation __op)
	{
	  switch (__op)
	    {
1802
#if __cpp_rtti
Paolo Carlini committed
1803 1804 1805
	    case __get_type_info:
	      __dest._M_access<const type_info*>() = &typeid(_Functor);
	      break;
1806
#endif
Paolo Carlini committed
1807 1808 1809 1810
	    case __get_functor_ptr:
	      __dest._M_access<_Functor*>() = *_Base::_M_get_pointer(__source);
	      return is_const<_Functor>::value;
	      break;
1811

Paolo Carlini committed
1812 1813 1814 1815 1816 1817 1818 1819 1820
	    default:
	      _Base::_M_manager(__dest, __source, __op);
	    }
	  return false;
	}

	static void
	_M_init_functor(_Any_data& __functor, reference_wrapper<_Functor> __f)
	{
1821
	  _Base::_M_init_functor(__functor, std::__addressof(__f.get()));
Paolo Carlini committed
1822 1823 1824
	}
      };

1825
    _Function_base() : _M_manager(nullptr) { }
1826

Paolo Carlini committed
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
    ~_Function_base()
    {
      if (_M_manager)
	_M_manager(_M_functor, _M_functor, __destroy_functor);
    }


    bool _M_empty() const { return !_M_manager; }

    typedef bool (*_Manager_type)(_Any_data&, const _Any_data&,
1837
				  _Manager_operation);
Paolo Carlini committed
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853

    _Any_data     _M_functor;
    _Manager_type _M_manager;
  };

  template<typename _Signature, typename _Functor>
    class _Function_handler;

  template<typename _Res, typename _Functor, typename... _ArgTypes>
    class _Function_handler<_Res(_ArgTypes...), _Functor>
    : public _Function_base::_Base_manager<_Functor>
    {
      typedef _Function_base::_Base_manager<_Functor> _Base;

    public:
      static _Res
1854
      _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
Paolo Carlini committed
1855
      {
1856 1857
	return (*_Base::_M_get_pointer(__functor))(
	    std::forward<_ArgTypes>(__args)...);
Paolo Carlini committed
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
      }
    };

  template<typename _Functor, typename... _ArgTypes>
    class _Function_handler<void(_ArgTypes...), _Functor>
    : public _Function_base::_Base_manager<_Functor>
    {
      typedef _Function_base::_Base_manager<_Functor> _Base;

     public:
      static void
1869
      _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
Paolo Carlini committed
1870
      {
1871 1872
	(*_Base::_M_get_pointer(__functor))(
	    std::forward<_ArgTypes>(__args)...);
Paolo Carlini committed
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
      }
    };

  template<typename _Res, typename _Functor, typename... _ArgTypes>
    class _Function_handler<_Res(_ArgTypes...), reference_wrapper<_Functor> >
    : public _Function_base::_Ref_manager<_Functor>
    {
      typedef _Function_base::_Ref_manager<_Functor> _Base;

     public:
      static _Res
1884
      _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
Paolo Carlini committed
1885
      {
1886 1887
	return __callable_functor(**_Base::_M_get_pointer(__functor))(
	      std::forward<_ArgTypes>(__args)...);
Paolo Carlini committed
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
      }
    };

  template<typename _Functor, typename... _ArgTypes>
    class _Function_handler<void(_ArgTypes...), reference_wrapper<_Functor> >
    : public _Function_base::_Ref_manager<_Functor>
    {
      typedef _Function_base::_Ref_manager<_Functor> _Base;

     public:
      static void
1899
      _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
Paolo Carlini committed
1900
      {
1901 1902
	__callable_functor(**_Base::_M_get_pointer(__functor))(
	    std::forward<_ArgTypes>(__args)...);
Paolo Carlini committed
1903 1904 1905
      }
    };

1906 1907
  template<typename _Class, typename _Member, typename _Res,
	   typename... _ArgTypes>
Paolo Carlini committed
1908 1909 1910 1911
    class _Function_handler<_Res(_ArgTypes...), _Member _Class::*>
    : public _Function_handler<void(_ArgTypes...), _Member _Class::*>
    {
      typedef _Function_handler<void(_ArgTypes...), _Member _Class::*>
1912
	_Base;
Paolo Carlini committed
1913 1914 1915

     public:
      static _Res
1916
      _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
Paolo Carlini committed
1917
      {
1918
	return std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1919
	    std::forward<_ArgTypes>(__args)...);
Paolo Carlini committed
1920 1921 1922 1923 1924 1925
      }
    };

  template<typename _Class, typename _Member, typename... _ArgTypes>
    class _Function_handler<void(_ArgTypes...), _Member _Class::*>
    : public _Function_base::_Base_manager<
1926
		 _Simple_type_wrapper< _Member _Class::* > >
Paolo Carlini committed
1927 1928 1929 1930 1931
    {
      typedef _Member _Class::* _Functor;
      typedef _Simple_type_wrapper<_Functor> _Wrapper;
      typedef _Function_base::_Base_manager<_Wrapper> _Base;

1932
    public:
Paolo Carlini committed
1933 1934
      static bool
      _M_manager(_Any_data& __dest, const _Any_data& __source,
1935
		 _Manager_operation __op)
Paolo Carlini committed
1936
      {
1937
	switch (__op)
Paolo Carlini committed
1938
	  {
1939
#if __cpp_rtti
Paolo Carlini committed
1940 1941 1942
	  case __get_type_info:
	    __dest._M_access<const type_info*>() = &typeid(_Functor);
	    break;
1943
#endif
Paolo Carlini committed
1944 1945 1946 1947
	  case __get_functor_ptr:
	    __dest._M_access<_Functor*>() =
	      &_Base::_M_get_pointer(__source)->__value;
	    break;
1948

Paolo Carlini committed
1949 1950 1951
	  default:
	    _Base::_M_manager(__dest, __source, __op);
	  }
1952
	return false;
Paolo Carlini committed
1953 1954 1955
      }

      static void
1956
      _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args)
Paolo Carlini committed
1957
      {
1958
	std::mem_fn(_Base::_M_get_pointer(__functor)->__value)(
1959
	    std::forward<_ArgTypes>(__args)...);
Paolo Carlini committed
1960 1961 1962
      }
    };

1963 1964 1965 1966
  template<typename _From, typename _To>
    using __check_func_return_type
      = __or_<is_void<_To>, is_convertible<_From, _To>>;

1967 1968 1969 1970 1971 1972
  /**
   *  @brief Primary class template for std::function.
   *  @ingroup functors
   *
   *  Polymorphic function wrapper.
   */
Paolo Carlini committed
1973 1974 1975 1976 1977 1978
  template<typename _Res, typename... _ArgTypes>
    class function<_Res(_ArgTypes...)>
    : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
      private _Function_base
    {
      typedef _Res _Signature_type(_ArgTypes...);
1979

1980 1981 1982 1983
      template<typename _Functor>
	using _Invoke = decltype(__callable_functor(std::declval<_Functor&>())
				 (std::declval<_ArgTypes>()...) );

1984 1985 1986 1987 1988
      // Used so the return type convertibility checks aren't done when
      // performing overload resolution for copy construction/assignment.
      template<typename _Tp>
	using _NotSelf = __not_<is_same<_Tp, function>>;

1989
      template<typename _Functor>
1990 1991 1992
	using _Callable
	  = __and_<_NotSelf<_Functor>,
		   __check_func_return_type<_Invoke<_Functor>, _Res>>;
1993 1994 1995

      template<typename _Cond, typename _Tp>
	using _Requires = typename enable_if<_Cond::value, _Tp>::type;
1996

Paolo Carlini committed
1997 1998
    public:
      typedef _Res result_type;
1999

Paolo Carlini committed
2000
      // [3.7.2.1] construct/copy/destroy
2001

Paolo Carlini committed
2002 2003 2004 2005
      /**
       *  @brief Default construct creates an empty function call wrapper.
       *  @post @c !(bool)*this
       */
2006 2007
      function() noexcept
      : _Function_base() { }
2008

Paolo Carlini committed
2009
      /**
Jonathan Wakely committed
2010
       *  @brief Creates an empty function call wrapper.
Paolo Carlini committed
2011 2012
       *  @post @c !(bool)*this
       */
2013 2014
      function(nullptr_t) noexcept
      : _Function_base() { }
2015

Paolo Carlini committed
2016 2017
      /**
       *  @brief %Function copy constructor.
2018 2019
       *  @param __x A %function object with identical call signature.
       *  @post @c bool(*this) == bool(__x)
Paolo Carlini committed
2020 2021
       *
       *  The newly-created %function contains a copy of the target of @a
2022
       *  __x (if it has one).
Paolo Carlini committed
2023 2024 2025 2026 2027
       */
      function(const function& __x);

      /**
       *  @brief %Function move constructor.
2028
       *  @param __x A %function object rvalue with identical call signature.
Paolo Carlini committed
2029
       *
2030
       *  The newly-created %function contains the target of @a __x
Paolo Carlini committed
2031 2032 2033 2034
       *  (if it has one).
       */
      function(function&& __x) : _Function_base()
      {
2035
	__x.swap(*this);
Paolo Carlini committed
2036 2037 2038
      }

      // TODO: needs allocator_arg_t
2039

Paolo Carlini committed
2040 2041 2042
      /**
       *  @brief Builds a %function that targets a copy of the incoming
       *  function object.
2043
       *  @param __f A %function object that is callable with parameters of
Paolo Carlini committed
2044 2045 2046
       *  type @c T1, @c T2, ..., @c TN and returns a value convertible
       *  to @c Res.
       *
2047 2048
       *  The newly-created %function object will target a copy of 
       *  @a __f. If @a __f is @c reference_wrapper<F>, then this function
Paolo Carlini committed
2049
       *  object will contain a reference to the function object @c
2050
       *  __f.get(). If @a __f is a NULL function pointer or NULL
Paolo Carlini committed
2051 2052
       *  pointer-to-member, the newly-created object will be empty.
       *
2053
       *  If @a __f is a non-NULL function pointer or an object of type @c
Paolo Carlini committed
2054 2055
       *  reference_wrapper<F>, this function will not throw.
       */
2056 2057 2058
      template<typename _Functor,
	       typename = _Requires<_Callable<_Functor>, void>>
	function(_Functor);
Paolo Carlini committed
2059 2060 2061

      /**
       *  @brief %Function assignment operator.
2062
       *  @param __x A %function with identical call signature.
Paolo Carlini committed
2063 2064 2065
       *  @post @c (bool)*this == (bool)x
       *  @returns @c *this
       *
2066
       *  The target of @a __x is copied to @c *this. If @a __x has no
Paolo Carlini committed
2067 2068
       *  target, then @c *this will be empty.
       *
2069
       *  If @a __x targets a function pointer or a reference to a function
Paolo Carlini committed
2070 2071 2072 2073 2074
       *  object, then this operation will not throw an %exception.
       */
      function&
      operator=(const function& __x)
      {
2075 2076
	function(__x).swap(*this);
	return *this;
Paolo Carlini committed
2077 2078 2079 2080
      }

      /**
       *  @brief %Function move-assignment operator.
2081
       *  @param __x A %function rvalue with identical call signature.
Paolo Carlini committed
2082 2083
       *  @returns @c *this
       *
2084
       *  The target of @a __x is moved to @c *this. If @a __x has no
Paolo Carlini committed
2085 2086
       *  target, then @c *this will be empty.
       *
2087
       *  If @a __x targets a function pointer or a reference to a function
Paolo Carlini committed
2088 2089 2090 2091 2092
       *  object, then this operation will not throw an %exception.
       */
      function&
      operator=(function&& __x)
      {
2093 2094
	function(std::move(__x)).swap(*this);
	return *this;
Paolo Carlini committed
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
      }

      /**
       *  @brief %Function assignment to zero.
       *  @post @c !(bool)*this
       *  @returns @c *this
       *
       *  The target of @c *this is deallocated, leaving it empty.
       */
      function&
2105
      operator=(nullptr_t) noexcept
Paolo Carlini committed
2106
      {
2107
	if (_M_manager)
Paolo Carlini committed
2108 2109
	  {
	    _M_manager(_M_functor, _M_functor, __destroy_functor);
2110 2111
	    _M_manager = nullptr;
	    _M_invoker = nullptr;
Paolo Carlini committed
2112
	  }
2113
	return *this;
Paolo Carlini committed
2114 2115 2116 2117
      }

      /**
       *  @brief %Function assignment to a new target.
2118
       *  @param __f A %function object that is callable with parameters of
Paolo Carlini committed
2119 2120 2121 2122 2123
       *  type @c T1, @c T2, ..., @c TN and returns a value convertible
       *  to @c Res.
       *  @return @c *this
       *
       *  This  %function object wrapper will target a copy of @a
2124
       *  __f. If @a __f is @c reference_wrapper<F>, then this function
Paolo Carlini committed
2125
       *  object will contain a reference to the function object @c
2126
       *  __f.get(). If @a __f is a NULL function pointer or NULL
Paolo Carlini committed
2127 2128
       *  pointer-to-member, @c this object will be empty.
       *
2129
       *  If @a __f is a non-NULL function pointer or an object of type @c
Paolo Carlini committed
2130 2131 2132
       *  reference_wrapper<F>, this function will not throw.
       */
      template<typename _Functor>
2133
	_Requires<_Callable<typename decay<_Functor>::type>, function&>
2134
	operator=(_Functor&& __f)
Paolo Carlini committed
2135
	{
2136
	  function(std::forward<_Functor>(__f)).swap(*this);
Paolo Carlini committed
2137 2138 2139 2140 2141
	  return *this;
	}

      /// @overload
      template<typename _Functor>
2142
	function&
2143
	operator=(reference_wrapper<_Functor> __f) noexcept
Paolo Carlini committed
2144 2145 2146 2147 2148 2149
	{
	  function(__f).swap(*this);
	  return *this;
	}

      // [3.7.2.2] function modifiers
2150

Paolo Carlini committed
2151 2152
      /**
       *  @brief Swap the targets of two %function objects.
2153
       *  @param __x A %function with identical call signature.
Paolo Carlini committed
2154
       *
2155
       *  Swap the targets of @c this function object and @a __f. This
Paolo Carlini committed
2156 2157 2158 2159
       *  function will not throw an %exception.
       */
      void swap(function& __x)
      {
2160 2161 2162
	std::swap(_M_functor, __x._M_functor);
	std::swap(_M_manager, __x._M_manager);
	std::swap(_M_invoker, __x._M_invoker);
Paolo Carlini committed
2163 2164 2165 2166 2167
      }

      // TODO: needs allocator_arg_t
      /*
      template<typename _Functor, typename _Alloc>
2168 2169 2170 2171 2172 2173
	void
	assign(_Functor&& __f, const _Alloc& __a)
	{
	  function(allocator_arg, __a,
		   std::forward<_Functor>(__f)).swap(*this);
	}
Paolo Carlini committed
2174
      */
2175

Paolo Carlini committed
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
      // [3.7.2.3] function capacity

      /**
       *  @brief Determine if the %function wrapper has a target.
       *
       *  @return @c true when this %function object contains a target,
       *  or @c false when it is empty.
       *
       *  This function will not throw an %exception.
       */
2186
      explicit operator bool() const noexcept
Paolo Carlini committed
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
      { return !_M_empty(); }

      // [3.7.2.4] function invocation

      /**
       *  @brief Invokes the function targeted by @c *this.
       *  @returns the result of the target.
       *  @throws bad_function_call when @c !(bool)*this
       *
       *  The function call operator invokes the target function object
       *  stored by @c this.
       */
      _Res operator()(_ArgTypes... __args) const;

2201
#if __cpp_rtti
Paolo Carlini committed
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
      // [3.7.2.5] function target access
      /**
       *  @brief Determine the type of the target of this function object
       *  wrapper.
       *
       *  @returns the type identifier of the target function object, or
       *  @c typeid(void) if @c !(bool)*this.
       *
       *  This function will not throw an %exception.
       */
2212
      const type_info& target_type() const noexcept;
2213

Paolo Carlini committed
2214 2215 2216 2217 2218 2219 2220 2221 2222
      /**
       *  @brief Access the stored target function object.
       *
       *  @return Returns a pointer to the stored target function object,
       *  if @c typeid(Functor).equals(target_type()); otherwise, a NULL
       *  pointer.
       *
       * This function will not throw an %exception.
       */
2223
      template<typename _Functor>       _Functor* target() noexcept;
2224

Paolo Carlini committed
2225
      /// @overload
2226
      template<typename _Functor> const _Functor* target() const noexcept;
Paolo Carlini committed
2227 2228 2229
#endif

    private:
2230
      using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
Paolo Carlini committed
2231 2232 2233
      _Invoker_type _M_invoker;
  };

2234
  // Out-of-line member definitions.
Paolo Carlini committed
2235 2236 2237 2238 2239 2240 2241
  template<typename _Res, typename... _ArgTypes>
    function<_Res(_ArgTypes...)>::
    function(const function& __x)
    : _Function_base()
    {
      if (static_cast<bool>(__x))
	{
2242
	  __x._M_manager(_M_functor, __x._M_functor, __clone_functor);
Paolo Carlini committed
2243 2244 2245 2246 2247 2248
	  _M_invoker = __x._M_invoker;
	  _M_manager = __x._M_manager;
	}
    }

  template<typename _Res, typename... _ArgTypes>
2249
    template<typename _Functor, typename>
Paolo Carlini committed
2250
      function<_Res(_ArgTypes...)>::
2251
      function(_Functor __f)
Paolo Carlini committed
2252 2253 2254 2255 2256 2257
      : _Function_base()
      {
	typedef _Function_handler<_Signature_type, _Functor> _My_handler;

	if (_My_handler::_M_not_empty_function(__f))
	  {
2258
	    _My_handler::_M_init_functor(_M_functor, std::move(__f));
Paolo Carlini committed
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
	    _M_invoker = &_My_handler::_M_invoke;
	    _M_manager = &_My_handler::_M_manager;
	  }
      }

  template<typename _Res, typename... _ArgTypes>
    _Res
    function<_Res(_ArgTypes...)>::
    operator()(_ArgTypes... __args) const
    {
      if (_M_empty())
2270
	__throw_bad_function_call();
Paolo Carlini committed
2271 2272 2273
      return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);
    }

2274
#if __cpp_rtti
Paolo Carlini committed
2275 2276 2277
  template<typename _Res, typename... _ArgTypes>
    const type_info&
    function<_Res(_ArgTypes...)>::
2278
    target_type() const noexcept
Paolo Carlini committed
2279 2280
    {
      if (_M_manager)
2281 2282 2283 2284 2285
	{
	  _Any_data __typeinfo_result;
	  _M_manager(__typeinfo_result, _M_functor, __get_type_info);
	  return *__typeinfo_result._M_access<const type_info*>();
	}
Paolo Carlini committed
2286 2287 2288 2289 2290 2291 2292 2293
      else
	return typeid(void);
    }

  template<typename _Res, typename... _ArgTypes>
    template<typename _Functor>
      _Functor*
      function<_Res(_ArgTypes...)>::
2294
      target() noexcept
Paolo Carlini committed
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
      {
	if (typeid(_Functor) == target_type() && _M_manager)
	  {
	    _Any_data __ptr;
	    if (_M_manager(__ptr, _M_functor, __get_functor_ptr)
		&& !is_const<_Functor>::value)
	      return 0;
	    else
	      return __ptr._M_access<_Functor*>();
	  }
	else
	  return 0;
      }

  template<typename _Res, typename... _ArgTypes>
    template<typename _Functor>
      const _Functor*
      function<_Res(_ArgTypes...)>::
2313
      target() const noexcept
Paolo Carlini committed
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
      {
	if (typeid(_Functor) == target_type() && _M_manager)
	  {
	    _Any_data __ptr;
	    _M_manager(__ptr, _M_functor, __get_functor_ptr);
	    return __ptr._M_access<const _Functor*>();
	  }
	else
	  return 0;
      }
#endif

  // [20.7.15.2.6] null pointer comparisons

  /**
   *  @brief Compares a polymorphic function object wrapper against 0
   *  (the NULL pointer).
   *  @returns @c true if the wrapper has no target, @c false otherwise
   *
   *  This function will not throw an %exception.
   */
  template<typename _Res, typename... _Args>
    inline bool
2337
    operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
Paolo Carlini committed
2338 2339 2340 2341 2342
    { return !static_cast<bool>(__f); }

  /// @overload
  template<typename _Res, typename... _Args>
    inline bool
2343
    operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
Paolo Carlini committed
2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
    { return !static_cast<bool>(__f); }

  /**
   *  @brief Compares a polymorphic function object wrapper against 0
   *  (the NULL pointer).
   *  @returns @c false if the wrapper has no target, @c true otherwise
   *
   *  This function will not throw an %exception.
   */
  template<typename _Res, typename... _Args>
    inline bool
2355
    operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept
Paolo Carlini committed
2356 2357 2358 2359 2360
    { return static_cast<bool>(__f); }

  /// @overload
  template<typename _Res, typename... _Args>
    inline bool
2361
    operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept
Paolo Carlini committed
2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
    { return static_cast<bool>(__f); }

  // [20.7.15.2.7] specialized algorithms

  /**
   *  @brief Swap the targets of two polymorphic function object wrappers.
   *
   *  This function will not throw an %exception.
   */
  template<typename _Res, typename... _Args>
    inline void
    swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y)
    { __x.swap(__y); }
2375

2376 2377
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
Paolo Carlini committed
2378

2379
#endif // C++11
2380

2381
#endif // _GLIBCXX_FUNCTIONAL