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