Where Online Learning is simpler!
The C and C++ Include Header Files
/usr/include/c++/11/tr1/functional
$ cat -n /usr/include/c++/11/tr1/functional 1 // TR1 functional header -*- C++ -*- 2 3 // Copyright (C) 2004-2021 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 //
. 24 25 /** @file tr1/functional 26 * This is a TR1 C++ Library header. 27 */ 28 29 #ifndef _GLIBCXX_TR1_FUNCTIONAL 30 #define _GLIBCXX_TR1_FUNCTIONAL 1 31 32 #pragma GCC system_header 33 34 #include
// for std::_Placeholder, std::_Bind, std::_Bind_result 35 36 #include
37 #include
38 #include
39 #include
40 #include
41 #include
42 #include
43 #include
// for std::__addressof 44 45 namespace std _GLIBCXX_VISIBILITY(default) 46 { 47 _GLIBCXX_BEGIN_NAMESPACE_VERSION 48 49 #if __cplusplus < 201103L 50 // In C++98 mode,
doesn't declare std::placeholders::_1 etc. 51 // because they are not reserved names in C++98. However, they are reserved 52 // by
so we can declare them here, in order to redeclare 53 // them in the std::tr1::placeholders namespace below. 54 namespace placeholders 55 { 56 extern const _Placeholder<1> _1; 57 extern const _Placeholder<2> _2; 58 extern const _Placeholder<3> _3; 59 extern const _Placeholder<4> _4; 60 extern const _Placeholder<5> _5; 61 extern const _Placeholder<6> _6; 62 extern const _Placeholder<7> _7; 63 extern const _Placeholder<8> _8; 64 extern const _Placeholder<9> _9; 65 extern const _Placeholder<10> _10; 66 extern const _Placeholder<11> _11; 67 extern const _Placeholder<12> _12; 68 extern const _Placeholder<13> _13; 69 extern const _Placeholder<14> _14; 70 extern const _Placeholder<15> _15; 71 extern const _Placeholder<16> _16; 72 extern const _Placeholder<17> _17; 73 extern const _Placeholder<18> _18; 74 extern const _Placeholder<19> _19; 75 extern const _Placeholder<20> _20; 76 extern const _Placeholder<21> _21; 77 extern const _Placeholder<22> _22; 78 extern const _Placeholder<23> _23; 79 extern const _Placeholder<24> _24; 80 extern const _Placeholder<25> _25; 81 extern const _Placeholder<26> _26; 82 extern const _Placeholder<27> _27; 83 extern const _Placeholder<28> _28; 84 extern const _Placeholder<29> _29; 85 } 86 #endif // C++98 87 88 namespace tr1 89 { 90 template
91 class _Mem_fn; 92 template
93 _Mem_fn<_Tp _Class::*> 94 mem_fn(_Tp _Class::*); 95 96 /** 97 * Actual implementation of _Has_result_type, which uses SFINAE to 98 * determine if the type _Tp has a publicly-accessible member type 99 * result_type. 100 */ 101 template
102 class _Has_result_type_helper : __sfinae_types 103 { 104 template
105 struct _Wrap_type 106 { }; 107 108 template
109 static __one __test(_Wrap_type
*); 110 111 template
112 static __two __test(...); 113 114 public: 115 static const bool value = sizeof(__test<_Tp>(0)) == 1; 116 }; 117 118 template
119 struct _Has_result_type 120 : integral_constant
::type>::value> 122 { }; 123 124 /** 125 * 126 */ 127 /// If we have found a result_type, extract it. 128 template
129 struct _Maybe_get_result_type 130 { }; 131 132 template
133 struct _Maybe_get_result_type
134 { 135 typedef typename _Functor::result_type result_type; 136 }; 137 138 /** 139 * Base class for any function object that has a weak result type, as 140 * defined in 3.3/3 of TR1. 141 */ 142 template
143 struct _Weak_result_type_impl 144 : _Maybe_get_result_type<_Has_result_type<_Functor>::value, _Functor> 145 { 146 }; 147 148 /// Retrieve the result type for a function type. 149 template
150 struct _Weak_result_type_impl<_Res(_ArgTypes...)> 151 { 152 typedef _Res result_type; 153 }; 154 155 /// Retrieve the result type for a function reference. 156 template
157 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)> 158 { 159 typedef _Res result_type; 160 }; 161 162 /// Retrieve the result type for a function pointer. 163 template
164 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)> 165 { 166 typedef _Res result_type; 167 }; 168 169 /// Retrieve result type for a member function pointer. 170 template
171 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)> 172 { 173 typedef _Res result_type; 174 }; 175 176 /// Retrieve result type for a const member function pointer. 177 template
178 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const> 179 { 180 typedef _Res result_type; 181 }; 182 183 /// Retrieve result type for a volatile member function pointer. 184 template
185 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile> 186 { 187 typedef _Res result_type; 188 }; 189 190 /// Retrieve result type for a const volatile member function pointer. 191 template
192 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)const volatile> 193 { 194 typedef _Res result_type; 195 }; 196 197 /** 198 * Strip top-level cv-qualifiers from the function object and let 199 * _Weak_result_type_impl perform the real work. 200 */ 201 template
202 struct _Weak_result_type 203 : _Weak_result_type_impl
::type> 204 { 205 }; 206 207 template
208 class result_of; 209 210 /** 211 * Actual implementation of result_of. When _Has_result_type is 212 * true, gets its result from _Weak_result_type. Otherwise, uses 213 * the function object's member template result to extract the 214 * result type. 215 */ 216 template
217 struct _Result_of_impl; 218 219 // Handle member data pointers using _Mem_fn's logic 220 template
221 struct _Result_of_impl
222 { 223 typedef typename _Mem_fn<_Res _Class::*> 224 ::template _Result_type<_T1>::type type; 225 }; 226 227 /** 228 * Determine whether we can determine a result type from @c Functor 229 * alone. 230 */ 231 template
232 class result_of<_Functor(_ArgTypes...)> 233 : public _Result_of_impl< 234 _Has_result_type<_Weak_result_type<_Functor> >::value, 235 _Functor(_ArgTypes...)> 236 { 237 }; 238 239 /// We already know the result type for @c Functor; use it. 240 template
241 struct _Result_of_impl
242 { 243 typedef typename _Weak_result_type<_Functor>::result_type type; 244 }; 245 246 /** 247 * We need to compute the result type for this invocation the hard 248 * way. 249 */ 250 template
251 struct _Result_of_impl
252 { 253 typedef typename _Functor 254 ::template result<_Functor(_ArgTypes...)>::type type; 255 }; 256 257 /** 258 * It is unsafe to access ::result when there are zero arguments, so we 259 * return @c void instead. 260 */ 261 template
262 struct _Result_of_impl
263 { 264 typedef void type; 265 }; 266 267 /// Determines if the type _Tp derives from unary_function. 268 template
269 struct _Derives_from_unary_function : __sfinae_types 270 { 271 private: 272 template
273 static __one __test(const volatile unary_function<_T1, _Res>*); 274 275 // It's tempting to change "..." to const volatile void*, but 276 // that fails when _Tp is a function type. 277 static __two __test(...); 278 279 public: 280 static const bool value = sizeof(__test((_Tp*)0)) == 1; 281 }; 282 283 /// Determines if the type _Tp derives from binary_function. 284 template
285 struct _Derives_from_binary_function : __sfinae_types 286 { 287 private: 288 template
289 static __one __test(const volatile binary_function<_T1, _T2, _Res>*); 290 291 // It's tempting to change "..." to const volatile void*, but 292 // that fails when _Tp is a function type. 293 static __two __test(...); 294 295 public: 296 static const bool value = sizeof(__test((_Tp*)0)) == 1; 297 }; 298 299 /// Turns a function type into a function pointer type 300 template
::value> 301 struct _Function_to_function_pointer 302 { 303 typedef _Tp type; 304 }; 305 306 template
307 struct _Function_to_function_pointer<_Tp, true> 308 { 309 typedef _Tp* type; 310 }; 311 312 /** 313 * Invoke a function object, which may be either a member pointer or a 314 * function object. The first parameter will tell which. 315 */ 316 template
317 inline 318 typename __gnu_cxx::__enable_if< 319 (!is_member_pointer<_Functor>::value 320 && !is_function<_Functor>::value 321 && !is_function
::type>::value), 322 typename result_of<_Functor(_Args...)>::type 323 >::__type 324 __invoke(_Functor& __f, _Args&... __args) 325 { 326 return __f(__args...); 327 } 328 329 template
330 inline 331 typename __gnu_cxx::__enable_if< 332 (is_member_pointer<_Functor>::value 333 && !is_function<_Functor>::value 334 && !is_function
::type>::value), 335 typename result_of<_Functor(_Args...)>::type 336 >::__type 337 __invoke(_Functor& __f, _Args&... __args) 338 { 339 return mem_fn(__f)(__args...); 340 } 341 342 // To pick up function references (that will become function pointers) 343 template
344 inline 345 typename __gnu_cxx::__enable_if< 346 (is_pointer<_Functor>::value 347 && is_function
::type>::value), 348 typename result_of<_Functor(_Args...)>::type 349 >::__type 350 __invoke(_Functor __f, _Args&... __args) 351 { 352 return __f(__args...); 353 } 354 355 /** 356 * Knowing which of unary_function and binary_function _Tp derives 357 * from, derives from the same and ensures that reference_wrapper 358 * will have a weak result type. See cases below. 359 */ 360 template
361 struct _Reference_wrapper_base_impl; 362 363 // Not a unary_function or binary_function, so try a weak result type. 364 template
365 struct _Reference_wrapper_base_impl
366 : _Weak_result_type<_Tp> 367 { }; 368 369 // unary_function but not binary_function 370 template
371 struct _Reference_wrapper_base_impl
372 : unary_function
374 { }; 375 376 // binary_function but not unary_function 377 template
378 struct _Reference_wrapper_base_impl
379 : binary_function
382 { }; 383 384 // Both unary_function and binary_function. Import result_type to 385 // avoid conflicts. 386 template
387 struct _Reference_wrapper_base_impl
388 : unary_function
, 390 binary_function
393 { 394 typedef typename _Tp::result_type result_type; 395 }; 396 397 /** 398 * Derives from unary_function or binary_function when it 399 * can. Specializations handle all of the easy cases. The primary 400 * template determines what to do with a class type, which may 401 * derive from both unary_function and binary_function. 402 */ 403 template
404 struct _Reference_wrapper_base 405 : _Reference_wrapper_base_impl< 406 _Derives_from_unary_function<_Tp>::value, 407 _Derives_from_binary_function<_Tp>::value, 408 _Tp> 409 { }; 410 411 // - a function type (unary) 412 template
413 struct _Reference_wrapper_base<_Res(_T1)> 414 : unary_function<_T1, _Res> 415 { }; 416 417 // - a function type (binary) 418 template
419 struct _Reference_wrapper_base<_Res(_T1, _T2)> 420 : binary_function<_T1, _T2, _Res> 421 { }; 422 423 // - a function pointer type (unary) 424 template
425 struct _Reference_wrapper_base<_Res(*)(_T1)> 426 : unary_function<_T1, _Res> 427 { }; 428 429 // - a function pointer type (binary) 430 template
431 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)> 432 : binary_function<_T1, _T2, _Res> 433 { }; 434 435 // - a pointer to member function type (unary, no qualifiers) 436 template
437 struct _Reference_wrapper_base<_Res (_T1::*)()> 438 : unary_function<_T1*, _Res> 439 { }; 440 441 // - a pointer to member function type (binary, no qualifiers) 442 template
443 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)> 444 : binary_function<_T1*, _T2, _Res> 445 { }; 446 447 // - a pointer to member function type (unary, const) 448 template
449 struct _Reference_wrapper_base<_Res (_T1::*)() const> 450 : unary_function
451 { }; 452 453 // - a pointer to member function type (binary, const) 454 template
455 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const> 456 : binary_function
457 { }; 458 459 // - a pointer to member function type (unary, volatile) 460 template
461 struct _Reference_wrapper_base<_Res (_T1::*)() volatile> 462 : unary_function
463 { }; 464 465 // - a pointer to member function type (binary, volatile) 466 template
467 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile> 468 : binary_function
469 { }; 470 471 // - a pointer to member function type (unary, const volatile) 472 template
473 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile> 474 : unary_function
475 { }; 476 477 // - a pointer to member function type (binary, const volatile) 478 template
479 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile> 480 : binary_function
481 { }; 482 483 /// reference_wrapper 484 template
485 class reference_wrapper 486 : public _Reference_wrapper_base
::type> 487 { 488 // If _Tp is a function type, we can't form result_of<_Tp(...)>, 489 // so turn it into a function pointer type. 490 typedef typename _Function_to_function_pointer<_Tp>::type 491 _M_func_type; 492 493 _Tp* _M_data; 494 public: 495 typedef _Tp type; 496 497 explicit 498 reference_wrapper(_Tp& __indata) 499 : _M_data(std::__addressof(__indata)) 500 { } 501 502 reference_wrapper(const reference_wrapper<_Tp>& __inref): 503 _M_data(__inref._M_data) 504 { } 505 506 reference_wrapper& 507 operator=(const reference_wrapper<_Tp>& __inref) 508 { 509 _M_data = __inref._M_data; 510 return *this; 511 } 512 513 operator _Tp&() const 514 { return this->get(); } 515 516 _Tp& 517 get() const 518 { return *_M_data; } 519 520 template
521 typename result_of<_M_func_type(_Args...)>::type 522 operator()(_Args&... __args) const 523 { 524 return __invoke(get(), __args...); 525 } 526 }; 527 528 529 // Denotes a reference should be taken to a variable. 530 template
531 inline reference_wrapper<_Tp> 532 ref(_Tp& __t) 533 { return reference_wrapper<_Tp>(__t); } 534 535 // Denotes a const reference should be taken to a variable. 536 template
537 inline reference_wrapper
538 cref(const _Tp& __t) 539 { return reference_wrapper
(__t); } 540 541 template
542 inline reference_wrapper<_Tp> 543 ref(reference_wrapper<_Tp> __t) 544 { return ref(__t.get()); } 545 546 template
547 inline reference_wrapper
548 cref(reference_wrapper<_Tp> __t) 549 { return cref(__t.get()); } 550 551 template
552 struct _Mem_fn_const_or_non 553 { 554 typedef const _Tp& type; 555 }; 556 557 template
558 struct _Mem_fn_const_or_non<_Tp, false> 559 { 560 typedef _Tp& type; 561 }; 562 563 /** 564 * Derives from @c unary_function or @c binary_function, or perhaps 565 * nothing, depending on the number of arguments provided. The 566 * primary template is the basis case, which derives nothing. 567 */ 568 template
569 struct _Maybe_unary_or_binary_function { }; 570 571 /// Derives from @c unary_function, as appropriate. 572 template
573 struct _Maybe_unary_or_binary_function<_Res, _T1> 574 : std::unary_function<_T1, _Res> { }; 575 576 /// Derives from @c binary_function, as appropriate. 577 template
578 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2> 579 : std::binary_function<_T1, _T2, _Res> { }; 580 581 /// Implementation of @c mem_fn for member function pointers. 582 template
583 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)> 584 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...> 585 { 586 typedef _Res (_Class::*_Functor)(_ArgTypes...); 587 588 template
589 _Res 590 _M_call(_Tp& __object, const volatile _Class *, 591 _ArgTypes... __args) const 592 { return (__object.*__pmf)(__args...); } 593 594 template
595 _Res 596 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const 597 { return ((*__ptr).*__pmf)(__args...); } 598 599 public: 600 typedef _Res result_type; 601 602 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 603 604 // Handle objects 605 _Res 606 operator()(_Class& __object, _ArgTypes... __args) const 607 { return (__object.*__pmf)(__args...); } 608 609 // Handle pointers 610 _Res 611 operator()(_Class* __object, _ArgTypes... __args) const 612 { return (__object->*__pmf)(__args...); } 613 614 // Handle smart pointers, references and pointers to derived 615 template
616 _Res 617 operator()(_Tp& __object, _ArgTypes... __args) const 618 { return _M_call(__object, &__object, __args...); } 619 620 private: 621 _Functor __pmf; 622 }; 623 624 /// Implementation of @c mem_fn for const member function pointers. 625 template
626 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const> 627 : public _Maybe_unary_or_binary_function<_Res, const _Class*, 628 _ArgTypes...> 629 { 630 typedef _Res (_Class::*_Functor)(_ArgTypes...) const; 631 632 template
633 _Res 634 _M_call(_Tp& __object, const volatile _Class *, 635 _ArgTypes... __args) const 636 { return (__object.*__pmf)(__args...); } 637 638 template
639 _Res 640 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const 641 { return ((*__ptr).*__pmf)(__args...); } 642 643 public: 644 typedef _Res result_type; 645 646 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 647 648 // Handle objects 649 _Res 650 operator()(const _Class& __object, _ArgTypes... __args) const 651 { return (__object.*__pmf)(__args...); } 652 653 // Handle pointers 654 _Res 655 operator()(const _Class* __object, _ArgTypes... __args) const 656 { return (__object->*__pmf)(__args...); } 657 658 // Handle smart pointers, references and pointers to derived 659 template
660 _Res operator()(_Tp& __object, _ArgTypes... __args) const 661 { return _M_call(__object, &__object, __args...); } 662 663 private: 664 _Functor __pmf; 665 }; 666 667 /// Implementation of @c mem_fn for volatile member function pointers. 668 template
669 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile> 670 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*, 671 _ArgTypes...> 672 { 673 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile; 674 675 template
676 _Res 677 _M_call(_Tp& __object, const volatile _Class *, 678 _ArgTypes... __args) const 679 { return (__object.*__pmf)(__args...); } 680 681 template
682 _Res 683 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const 684 { return ((*__ptr).*__pmf)(__args...); } 685 686 public: 687 typedef _Res result_type; 688 689 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 690 691 // Handle objects 692 _Res 693 operator()(volatile _Class& __object, _ArgTypes... __args) const 694 { return (__object.*__pmf)(__args...); } 695 696 // Handle pointers 697 _Res 698 operator()(volatile _Class* __object, _ArgTypes... __args) const 699 { return (__object->*__pmf)(__args...); } 700 701 // Handle smart pointers, references and pointers to derived 702 template
703 _Res 704 operator()(_Tp& __object, _ArgTypes... __args) const 705 { return _M_call(__object, &__object, __args...); } 706 707 private: 708 _Functor __pmf; 709 }; 710 711 /// Implementation of @c mem_fn for const volatile member function pointers. 712 template
713 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile> 714 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*, 715 _ArgTypes...> 716 { 717 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile; 718 719 template
720 _Res 721 _M_call(_Tp& __object, const volatile _Class *, 722 _ArgTypes... __args) const 723 { return (__object.*__pmf)(__args...); } 724 725 template
726 _Res 727 _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const 728 { return ((*__ptr).*__pmf)(__args...); } 729 730 public: 731 typedef _Res result_type; 732 733 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 734 735 // Handle objects 736 _Res 737 operator()(const volatile _Class& __object, _ArgTypes... __args) const 738 { return (__object.*__pmf)(__args...); } 739 740 // Handle pointers 741 _Res 742 operator()(const volatile _Class* __object, _ArgTypes... __args) const 743 { return (__object->*__pmf)(__args...); } 744 745 // Handle smart pointers, references and pointers to derived 746 template
747 _Res operator()(_Tp& __object, _ArgTypes... __args) const 748 { return _M_call(__object, &__object, __args...); } 749 750 private: 751 _Functor __pmf; 752 }; 753 754 755 template
756 class _Mem_fn<_Res _Class::*> 757 { 758 // This bit of genius is due to Peter Dimov, improved slightly by 759 // Douglas Gregor. 760 template
761 _Res& 762 _M_call(_Tp& __object, _Class *) const 763 { return __object.*__pm; } 764 765 template
766 _Res& 767 _M_call(_Tp& __object, _Up * const *) const 768 { return (*__object).*__pm; } 769 770 template
771 const _Res& 772 _M_call(_Tp& __object, const _Up * const *) const 773 { return (*__object).*__pm; } 774 775 template
776 const _Res& 777 _M_call(_Tp& __object, const _Class *) const 778 { return __object.*__pm; } 779 780 template
781 const _Res& 782 _M_call(_Tp& __ptr, const volatile void*) const 783 { return (*__ptr).*__pm; } 784 785 template
static _Tp& __get_ref(); 786 787 template
788 static __sfinae_types::__one __check_const(_Tp&, _Class*); 789 template
790 static __sfinae_types::__one __check_const(_Tp&, _Up * const *); 791 template
792 static __sfinae_types::__two __check_const(_Tp&, const _Up * const *); 793 template
794 static __sfinae_types::__two __check_const(_Tp&, const _Class*); 795 template
796 static __sfinae_types::__two __check_const(_Tp&, const volatile void*); 797 798 public: 799 template
800 struct _Result_type 801 : _Mem_fn_const_or_non<_Res, 802 (sizeof(__sfinae_types::__two) 803 == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))> 804 { }; 805 806 template
807 struct result; 808 809 template
810 struct result<_CVMem(_Tp)> 811 : public _Result_type<_Tp> { }; 812 813 template
814 struct result<_CVMem(_Tp&)> 815 : public _Result_type<_Tp> { }; 816 817 explicit 818 _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { } 819 820 // Handle objects 821 _Res& 822 operator()(_Class& __object) const 823 { return __object.*__pm; } 824 825 const _Res& 826 operator()(const _Class& __object) const 827 { return __object.*__pm; } 828 829 // Handle pointers 830 _Res& 831 operator()(_Class* __object) const 832 { return __object->*__pm; } 833 834 const _Res& 835 operator()(const _Class* __object) const 836 { return __object->*__pm; } 837 838 // Handle smart pointers and derived 839 template
840 typename _Result_type<_Tp>::type 841 operator()(_Tp& __unknown) const 842 { return _M_call(__unknown, &__unknown); } 843 844 private: 845 _Res _Class::*__pm; 846 }; 847 848 /** 849 * @brief Returns a function object that forwards to the member 850 * pointer @a pm. 851 */ 852 template
853 inline _Mem_fn<_Tp _Class::*> 854 mem_fn(_Tp _Class::* __pm) 855 { 856 return _Mem_fn<_Tp _Class::*>(__pm); 857 } 858 859 /** 860 * @brief Determines if the given type _Tp is a function object 861 * should be treated as a subexpression when evaluating calls to 862 * function objects returned by bind(). [TR1 3.6.1] 863 */ 864 template
865 struct is_bind_expression 866 { static const bool value = false; }; 867 868 template
869 const bool is_bind_expression<_Tp>::value; 870 871 /** 872 * @brief Determines if the given type _Tp is a placeholder in a 873 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2] 874 */ 875 template
876 struct is_placeholder 877 { static const int value = 0; }; 878 879 template
880 const int is_placeholder<_Tp>::value; 881 882 /// The type of placeholder objects defined by libstdc++. 883 using ::std::_Placeholder; 884 885 /** @namespace std::tr1::placeholders 886 * @brief Sub-namespace for tr1/functional. 887 */ 888 namespace placeholders 889 { 890 // The C++11 std::placeholders are already exported from the library. 891 // Reusing them here avoids needing to export additional symbols for 892 // the TR1 placeholders, and avoids ODR violations due to defining 893 // them with internal linkage (as we used to do). 894 using namespace ::std::placeholders; 895 } 896 897 /** 898 * Partial specialization of is_placeholder that provides the placeholder 899 * number for the placeholder objects defined by libstdc++. 900 */ 901 template
902 struct is_placeholder<_Placeholder<_Num> > 903 : integral_constant
904 { }; 905 906 template
907 struct is_placeholder
> 908 : integral_constant
909 { }; 910 911 /** 912 * Stores a tuple of indices. Used by bind() to extract the elements 913 * in a tuple. 914 */ 915 template
916 struct _Index_tuple { }; 917 918 /// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>. 919 template
> 920 struct _Build_index_tuple; 921 922 template
923 struct _Build_index_tuple<_Num, _Index_tuple<_Indexes...> > 924 : _Build_index_tuple<_Num - 1, 925 _Index_tuple<_Indexes..., sizeof...(_Indexes)> > 926 { 927 }; 928 929 template
930 struct _Build_index_tuple<0, _Index_tuple<_Indexes...> > 931 { 932 typedef _Index_tuple<_Indexes...> __type; 933 }; 934 935 /** 936 * Used by _Safe_tuple_element to indicate that there is no tuple 937 * element at this position. 938 */ 939 struct _No_tuple_element; 940 941 /** 942 * Implementation helper for _Safe_tuple_element. This primary 943 * template handles the case where it is safe to use @c 944 * tuple_element. 945 */ 946 template
947 struct _Safe_tuple_element_impl 948 : tuple_element<__i, _Tuple> { }; 949 950 /** 951 * Implementation helper for _Safe_tuple_element. This partial 952 * specialization handles the case where it is not safe to use @c 953 * tuple_element. We just return @c _No_tuple_element. 954 */ 955 template
956 struct _Safe_tuple_element_impl<__i, _Tuple, false> 957 { 958 typedef _No_tuple_element type; 959 }; 960 961 /** 962 * Like tuple_element, but returns @c _No_tuple_element when 963 * tuple_element would return an error. 964 */ 965 template
966 struct _Safe_tuple_element 967 : _Safe_tuple_element_impl<__i, _Tuple, 968 (__i >= 0 && __i < tuple_size<_Tuple>::value)> 969 { 970 }; 971 972 /** 973 * Maps an argument to bind() into an actual argument to the bound 974 * function object [TR1 3.6.3/5]. Only the first parameter should 975 * be specified: the rest are used to determine among the various 976 * implementations. Note that, although this class is a function 977 * object, it isn't entirely normal because it takes only two 978 * parameters regardless of the number of parameters passed to the 979 * bind expression. The first parameter is the bound argument and 980 * the second parameter is a tuple containing references to the 981 * rest of the arguments. 982 */ 983 template
::value, 985 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)> 986 class _Mu; 987 988 /** 989 * If the argument is reference_wrapper<_Tp>, returns the 990 * underlying reference. [TR1 3.6.3/5 bullet 1] 991 */ 992 template
993 class _Mu
, false, false> 994 { 995 public: 996 typedef _Tp& result_type; 997 998 /* Note: This won't actually work for const volatile 999 * reference_wrappers, because reference_wrapper::get() is const 1000 * but not volatile-qualified. This might be a defect in the TR. 1001 */ 1002 template
1003 result_type 1004 operator()(_CVRef& __arg, const _Tuple&) const volatile 1005 { return __arg.get(); } 1006 }; 1007 1008 /** 1009 * If the argument is a bind expression, we invoke the underlying 1010 * function object with the same cv-qualifiers as we are given and 1011 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2] 1012 */ 1013 template
1014 class _Mu<_Arg, true, false> 1015 { 1016 public: 1017 template
class result; 1018 1019 // Determine the result type when we pass the arguments along. This 1020 // involves passing along the cv-qualifiers placed on _Mu and 1021 // unwrapping the argument bundle. 1022 template
1023 class result<_CVMu(_CVArg, tuple<_Args...>)> 1024 : public result_of<_CVArg(_Args...)> { }; 1025 1026 template
1027 typename result_of<_CVArg(_Args...)>::type 1028 operator()(_CVArg& __arg, 1029 const tuple<_Args...>& __tuple) const volatile 1030 { 1031 // Construct an index tuple and forward to __call 1032 typedef typename _Build_index_tuple
::__type 1033 _Indexes; 1034 return this->__call(__arg, __tuple, _Indexes()); 1035 } 1036 1037 private: 1038 // Invokes the underlying function object __arg by unpacking all 1039 // of the arguments in the tuple. 1040 template
1041 typename result_of<_CVArg(_Args...)>::type 1042 __call(_CVArg& __arg, const tuple<_Args...>& __tuple, 1043 const _Index_tuple<_Indexes...>&) const volatile 1044 { 1045 return __arg(tr1::get<_Indexes>(__tuple)...); 1046 } 1047 }; 1048 1049 /** 1050 * If the argument is a placeholder for the Nth argument, returns 1051 * a reference to the Nth argument to the bind function object. 1052 * [TR1 3.6.3/5 bullet 3] 1053 */ 1054 template
1055 class _Mu<_Arg, false, true> 1056 { 1057 public: 1058 template
class result; 1059 1060 template
1061 class result<_CVMu(_CVArg, _Tuple)> 1062 { 1063 // Add a reference, if it hasn't already been done for us. 1064 // This allows us to be a little bit sloppy in constructing 1065 // the tuple that we pass to result_of<...>. 1066 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value 1067 - 1), _Tuple>::type 1068 __base_type; 1069 1070 public: 1071 typedef typename add_reference<__base_type>::type type; 1072 }; 1073 1074 template
1075 typename result<_Mu(_Arg, _Tuple)>::type 1076 operator()(const volatile _Arg&, const _Tuple& __tuple) const volatile 1077 { 1078 return ::std::tr1::get<(is_placeholder<_Arg>::value - 1)>(__tuple); 1079 } 1080 }; 1081 1082 /** 1083 * If the argument is just a value, returns a reference to that 1084 * value. The cv-qualifiers on the reference are the same as the 1085 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4] 1086 */ 1087 template
1088 class _Mu<_Arg, false, false> 1089 { 1090 public: 1091 template
struct result; 1092 1093 template
1094 struct result<_CVMu(_CVArg, _Tuple)> 1095 { 1096 typedef typename add_reference<_CVArg>::type type; 1097 }; 1098 1099 // Pick up the cv-qualifiers of the argument 1100 template
1101 _CVArg& 1102 operator()(_CVArg& __arg, const _Tuple&) const volatile 1103 { return __arg; } 1104 }; 1105 1106 /** 1107 * Maps member pointers into instances of _Mem_fn but leaves all 1108 * other function objects untouched. Used by tr1::bind(). The 1109 * primary template handles the non--member-pointer case. 1110 */ 1111 template
1112 struct _Maybe_wrap_member_pointer 1113 { 1114 typedef _Tp type; 1115 1116 static const _Tp& 1117 __do_wrap(const _Tp& __x) 1118 { return __x; } 1119 }; 1120 1121 /** 1122 * Maps member pointers into instances of _Mem_fn but leaves all 1123 * other function objects untouched. Used by tr1::bind(). This 1124 * partial specialization handles the member pointer case. 1125 */ 1126 template
1127 struct _Maybe_wrap_member_pointer<_Tp _Class::*> 1128 { 1129 typedef _Mem_fn<_Tp _Class::*> type; 1130 1131 static type 1132 __do_wrap(_Tp _Class::* __pm) 1133 { return type(__pm); } 1134 }; 1135 1136 /// Type of the function object returned from bind(). 1137 template
1138 struct _Bind; 1139 1140 template
1141 class _Bind<_Functor(_Bound_args...)> 1142 : public _Weak_result_type<_Functor> 1143 { 1144 typedef _Bind __self_type; 1145 typedef typename _Build_index_tuple
::__type 1146 _Bound_indexes; 1147 1148 _Functor _M_f; 1149 tuple<_Bound_args...> _M_bound_args; 1150 1151 // Call unqualified 1152 template
1153 typename result_of< 1154 _Functor(typename result_of<_Mu<_Bound_args> 1155 (_Bound_args, tuple<_Args...>)>::type...) 1156 >::type 1157 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) 1158 { 1159 return _M_f(_Mu<_Bound_args>() 1160 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1161 } 1162 1163 // Call as const 1164 template
1165 typename result_of< 1166 const _Functor(typename result_of<_Mu<_Bound_args> 1167 (const _Bound_args, tuple<_Args...>) 1168 >::type...)>::type 1169 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const 1170 { 1171 return _M_f(_Mu<_Bound_args>() 1172 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1173 } 1174 1175 // Call as volatile 1176 template
1177 typename result_of< 1178 volatile _Functor(typename result_of<_Mu<_Bound_args> 1179 (volatile _Bound_args, tuple<_Args...>) 1180 >::type...)>::type 1181 __call(const tuple<_Args...>& __args, 1182 _Index_tuple<_Indexes...>) volatile 1183 { 1184 return _M_f(_Mu<_Bound_args>() 1185 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1186 } 1187 1188 // Call as const volatile 1189 template
1190 typename result_of< 1191 const volatile _Functor(typename result_of<_Mu<_Bound_args> 1192 (const volatile _Bound_args, 1193 tuple<_Args...>) 1194 >::type...)>::type 1195 __call(const tuple<_Args...>& __args, 1196 _Index_tuple<_Indexes...>) const volatile 1197 { 1198 return _M_f(_Mu<_Bound_args>() 1199 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1200 } 1201 1202 public: 1203 explicit _Bind(_Functor __f, _Bound_args... __bound_args) 1204 : _M_f(__f), _M_bound_args(__bound_args...) { } 1205 1206 // Call unqualified 1207 template
1208 typename result_of< 1209 _Functor(typename result_of<_Mu<_Bound_args> 1210 (_Bound_args, tuple<_Args...>)>::type...) 1211 >::type 1212 operator()(_Args&... __args) 1213 { 1214 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1215 } 1216 1217 // Call as const 1218 template
1219 typename result_of< 1220 const _Functor(typename result_of<_Mu<_Bound_args> 1221 (const _Bound_args, tuple<_Args...>)>::type...) 1222 >::type 1223 operator()(_Args&... __args) const 1224 { 1225 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1226 } 1227 1228 1229 // Call as volatile 1230 template
1231 typename result_of< 1232 volatile _Functor(typename result_of<_Mu<_Bound_args> 1233 (volatile _Bound_args, tuple<_Args...>)>::type...) 1234 >::type 1235 operator()(_Args&... __args) volatile 1236 { 1237 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1238 } 1239 1240 1241 // Call as const volatile 1242 template
1243 typename result_of< 1244 const volatile _Functor(typename result_of<_Mu<_Bound_args> 1245 (const volatile _Bound_args, 1246 tuple<_Args...>)>::type...) 1247 >::type 1248 operator()(_Args&... __args) const volatile 1249 { 1250 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1251 } 1252 }; 1253 1254 /// Type of the function object returned from bind
(). 1255 template
1256 struct _Bind_result; 1257 1258 template
1259 class _Bind_result<_Result, _Functor(_Bound_args...)> 1260 { 1261 typedef _Bind_result __self_type; 1262 typedef typename _Build_index_tuple
::__type 1263 _Bound_indexes; 1264 1265 _Functor _M_f; 1266 tuple<_Bound_args...> _M_bound_args; 1267 1268 // Call unqualified 1269 template
1270 _Result 1271 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) 1272 { 1273 return _M_f(_Mu<_Bound_args>() 1274 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1275 } 1276 1277 // Call as const 1278 template
1279 _Result 1280 __call(const tuple<_Args...>& __args, _Index_tuple<_Indexes...>) const 1281 { 1282 return _M_f(_Mu<_Bound_args>() 1283 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1284 } 1285 1286 // Call as volatile 1287 template
1288 _Result 1289 __call(const tuple<_Args...>& __args, 1290 _Index_tuple<_Indexes...>) volatile 1291 { 1292 return _M_f(_Mu<_Bound_args>() 1293 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1294 } 1295 1296 // Call as const volatile 1297 template
1298 _Result 1299 __call(const tuple<_Args...>& __args, 1300 _Index_tuple<_Indexes...>) const volatile 1301 { 1302 return _M_f(_Mu<_Bound_args>() 1303 (tr1::get<_Indexes>(_M_bound_args), __args)...); 1304 } 1305 1306 public: 1307 typedef _Result result_type; 1308 1309 explicit 1310 _Bind_result(_Functor __f, _Bound_args... __bound_args) 1311 : _M_f(__f), _M_bound_args(__bound_args...) { } 1312 1313 // Call unqualified 1314 template
1315 result_type 1316 operator()(_Args&... __args) 1317 { 1318 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1319 } 1320 1321 // Call as const 1322 template
1323 result_type 1324 operator()(_Args&... __args) const 1325 { 1326 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1327 } 1328 1329 // Call as volatile 1330 template
1331 result_type 1332 operator()(_Args&... __args) volatile 1333 { 1334 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1335 } 1336 1337 // Call as const volatile 1338 template
1339 result_type 1340 operator()(_Args&... __args) const volatile 1341 { 1342 return this->__call(tr1::tie(__args...), _Bound_indexes()); 1343 } 1344 }; 1345 1346 /// Class template _Bind is always a bind expression. 1347 template
1348 struct is_bind_expression<_Bind<_Signature> > 1349 { static const bool value = true; }; 1350 1351 template
1352 const bool is_bind_expression<_Bind<_Signature> >::value; 1353 1354 /// Class template _Bind is always a bind expression. 1355 template
1356 struct is_bind_expression
> 1357 { static const bool value = true; }; 1358 1359 template
1360 const bool is_bind_expression
>::value; 1361 1362 /// Class template _Bind is always a bind expression. 1363 template
1364 struct is_bind_expression
> 1365 { static const bool value = true; }; 1366 1367 template
1368 const bool is_bind_expression
>::value; 1369 1370 /// Class template _Bind is always a bind expression. 1371 template
1372 struct is_bind_expression
> 1373 { static const bool value = true; }; 1374 1375 template
1376 const bool is_bind_expression
>::value; 1377 1378 /// Class template _Bind_result is always a bind expression. 1379 template
1380 struct is_bind_expression<_Bind_result<_Result, _Signature> > 1381 { static const bool value = true; }; 1382 1383 template
1384 const bool is_bind_expression<_Bind_result<_Result, _Signature> >::value; 1385 1386 /// Class template _Bind_result is always a bind expression. 1387 template
1388 struct is_bind_expression
> 1389 { static const bool value = true; }; 1390 1391 template
1392 const bool 1393 is_bind_expression
>::value; 1394 1395 /// Class template _Bind_result is always a bind expression. 1396 template
1397 struct is_bind_expression
> 1398 { static const bool value = true; }; 1399 1400 template
1401 const bool 1402 is_bind_expression
>::value; 1403 1404 /// Class template _Bind_result is always a bind expression. 1405 template
1406 struct 1407 is_bind_expression
> 1408 { static const bool value = true; }; 1409 1410 template
1411 const bool 1412 is_bind_expression
>::value; 1414 1415 #if __cplusplus >= 201103L 1416 // Specialize tr1::is_bind_expression for std::bind closure types, 1417 // so that they can also work with tr1::bind. 1418 1419 template
1420 struct is_bind_expression
> 1421 : true_type { }; 1422 1423 template
1424 struct is_bind_expression
> 1425 : true_type { }; 1426 1427 template
1428 struct is_bind_expression
> 1429 : true_type { }; 1430 1431 template
1432 struct is_bind_expression
> 1433 : true_type { }; 1434 1435 template
1436 struct is_bind_expression
> 1437 : true_type { }; 1438 1439 template
1440 struct is_bind_expression
> 1441 : true_type { }; 1442 1443 template
1444 struct is_bind_expression
> 1445 : true_type { }; 1446 1447 template
1448 struct is_bind_expression
> 1450 : true_type { }; 1451 #endif 1452 1453 /// bind 1454 template
1455 inline 1456 _Bind
::type(_ArgTypes...)> 1457 bind(_Functor __f, _ArgTypes... __args) 1458 { 1459 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type; 1460 typedef typename __maybe_type::type __functor_type; 1461 typedef _Bind<__functor_type(_ArgTypes...)> __result_type; 1462 return __result_type(__maybe_type::__do_wrap(__f), __args...); 1463 } 1464 1465 template
1466 inline 1467 _Bind_result<_Result, 1468 typename _Maybe_wrap_member_pointer<_Functor>::type 1469 (_ArgTypes...)> 1470 bind(_Functor __f, _ArgTypes... __args) 1471 { 1472 typedef _Maybe_wrap_member_pointer<_Functor> __maybe_type; 1473 typedef typename __maybe_type::type __functor_type; 1474 typedef _Bind_result<_Result, __functor_type(_ArgTypes...)> 1475 __result_type; 1476 return __result_type(__maybe_type::__do_wrap(__f), __args...); 1477 } 1478 1479 /** 1480 * @brief Exception class thrown when class template function's 1481 * operator() is called with an empty target. 1482 * @ingroup exceptions 1483 */ 1484 class bad_function_call : public std::exception { }; 1485 1486 /** 1487 * The integral constant expression 0 can be converted into a 1488 * pointer to this type. It is used by the function template to 1489 * accept NULL pointers. 1490 */ 1491 struct _M_clear_type; 1492 1493 /** 1494 * Trait identifying @a location-invariant types, meaning that the 1495 * address of the object (or any of its members) will not escape. 1496 * Also implies a trivial copy constructor and assignment operator. 1497 */ 1498 template
1499 struct __is_location_invariant 1500 : integral_constant
::value 1502 || is_member_pointer<_Tp>::value)> 1503 { 1504 }; 1505 1506 class _Undefined_class; 1507 1508 union _Nocopy_types 1509 { 1510 void* _M_object; 1511 const void* _M_const_object; 1512 void (*_M_function_pointer)(); 1513 void (_Undefined_class::*_M_member_pointer)(); 1514 }; 1515 1516 union _Any_data 1517 { 1518 void* _M_access() { return &_M_pod_data[0]; } 1519 const void* _M_access() const { return &_M_pod_data[0]; } 1520 1521 template
1522 _Tp& 1523 _M_access() 1524 { return *static_cast<_Tp*>(_M_access()); } 1525 1526 template
1527 const _Tp& 1528 _M_access() const 1529 { return *static_cast
(_M_access()); } 1530 1531 _Nocopy_types _M_unused; 1532 char _M_pod_data[sizeof(_Nocopy_types)]; 1533 }; 1534 1535 enum _Manager_operation 1536 { 1537 __get_type_info, 1538 __get_functor_ptr, 1539 __clone_functor, 1540 __destroy_functor 1541 }; 1542 1543 // Simple type wrapper that helps avoid annoying const problems 1544 // when casting between void pointers and pointers-to-pointers. 1545 template
1546 struct _Simple_type_wrapper 1547 { 1548 _Simple_type_wrapper(_Tp __value) : __value(__value) { } 1549 1550 _Tp __value; 1551 }; 1552 1553 template
1554 struct __is_location_invariant<_Simple_type_wrapper<_Tp> > 1555 : __is_location_invariant<_Tp> 1556 { 1557 }; 1558 1559 // Converts a reference to a function object into a callable 1560 // function object. 1561 template
1562 inline _Functor& 1563 __callable_functor(_Functor& __f) 1564 { return __f; } 1565 1566 template
1567 inline _Mem_fn<_Member _Class::*> 1568 __callable_functor(_Member _Class::* &__p) 1569 { return mem_fn(__p); } 1570 1571 template
1572 inline _Mem_fn<_Member _Class::*> 1573 __callable_functor(_Member _Class::* const &__p) 1574 { return mem_fn(__p); } 1575 1576 template
1577 class function; 1578 1579 /// Base class of all polymorphic function object wrappers. 1580 class _Function_base 1581 { 1582 public: 1583 static const std::size_t _M_max_size = sizeof(_Nocopy_types); 1584 static const std::size_t _M_max_align = __alignof__(_Nocopy_types); 1585 1586 template