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