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