|
1 | 1 | --- |
2 | 2 | title: "<functional> functions" |
3 | | -ms.date: "01/17/2019" |
4 | | -f1_keywords: ["functional/std::bind", "xfunctional/std::bind1st", "xfunctional/std::bind2nd", "xfunctional/std::bit_and", "xfunctional/std::bit_not", "xfunctional/std::bit_or", "xfunctional/std::bit_xor", "functional/std::cref", "type_traits/std::cref", "xfunctional/std::mem_fn", "xfunctional/std::mem_fun_ref", "xfunctional/std::not1", "xfunctional/std::not2", "functional/std::not_fn", "xfunctional/std::ptr_fun", "functional/std::ref", "functional/std::swap"] |
| 3 | +ms.date: "02/21/2019" |
| 4 | +f1_keywords: ["functional/std::bind", "functional/std::bind1st", "functional/std::bind2nd", "functional/std::bit_and", "functional/std::bit_not", "functional/std::bit_or", "functional/std::bit_xor", "functional/std::cref", "type_traits/std::cref", "functional/std::mem_fn", "functional/std::mem_fun_ref", "functional/std::not1", "functional/std::not2", "functional/std::not_fn", "functional/std::ptr_fun", "functional/std::ref", "functional/std::swap"] |
5 | 5 | helpviewer_keywords: ["std::bind [C++]", "std::bind1st", "std::bind2nd", "std::bit_and [C++]", "std::bit_not [C++]", "std::bit_or [C++]", "std::bit_xor [C++]", "std::cref [C++]"] |
6 | 6 | ms.assetid: c34d0b45-50a7-447a-9368-2210d06339a4 |
7 | 7 | --- |
@@ -531,69 +531,6 @@ cref(i) = 1 |
531 | 531 | cref(neg)(i) = -1 |
532 | 532 | ``` |
533 | 533 |
|
534 | | -## <a name="mem_fn"></a> mem_fn |
535 | | - |
536 | | -Generates a simple call wrapper. |
537 | | - |
538 | | -```cpp |
539 | | -template <class Ret, class Ty> |
540 | | -unspecified mem_fn(Ret Ty::*pm); |
541 | | -``` |
542 | | -
|
543 | | -### Parameters |
544 | | -
|
545 | | -*Ret*<br/> |
546 | | -The return type of the wrapped function. |
547 | | -
|
548 | | -*Ty*<br/> |
549 | | -The type of the member function pointer. |
550 | | -
|
551 | | -### Remarks |
552 | | -
|
553 | | -The template function returns a simple call wrapper `cw`, with a weak result type, such that the expression `cw(t, a2, ..., aN)` is equivalent to `INVOKE(pm, t, a2, ..., aN)`. It does not throw any exceptions. |
554 | | -
|
555 | | -The returned call wrapper is derived from `std::unary_function<cv Ty*, Ret>` (hence defining the nested type `result_type` as a synonym for *Ret* and the nested type `argument_type` as a synonym for `cv Ty*`) only if the type *Ty* is a pointer to member function with cv-qualifier `cv` that takes no arguments. |
556 | | -
|
557 | | -The returned call wrapper is derived from `std::binary_function<cv Ty*, T2, Ret>` (hence defining the nested type `result_type` as a synonym for *Ret*, the nested type `first argument_type` as a synonym for `cv Ty*`, and the nested type `second argument_type` as a synonym for `T2`) only if the type *Ty* is a pointer to member function with cv-qualifier `cv` that takes one argument, of type `T2`. |
558 | | -
|
559 | | -### Example |
560 | | -
|
561 | | -```cpp |
562 | | -// std__functional__mem_fn.cpp |
563 | | -// compile with: /EHsc |
564 | | -#include <functional> |
565 | | -#include <iostream> |
566 | | -
|
567 | | -class Funs |
568 | | - { |
569 | | -public: |
570 | | - void square(double x) |
571 | | - { |
572 | | - std::cout << x << "^2 == " << x * x << std::endl; |
573 | | - } |
574 | | -
|
575 | | - void product(double x, double y) |
576 | | - { |
577 | | - std::cout << x << "*" << y << " == " << x * y << std::endl; |
578 | | - } |
579 | | - }; |
580 | | -
|
581 | | -int main() |
582 | | - { |
583 | | - Funs funs; |
584 | | -
|
585 | | - std::mem_fn(&Funs::square)(funs, 3.0); |
586 | | - std::mem_fn(&Funs::product)(funs, 3.0, 2.0); |
587 | | -
|
588 | | - return (0); |
589 | | - } |
590 | | -``` |
591 | | - |
592 | | -```Output |
593 | | -3^2 == 9 |
594 | | -3*2 == 6 |
595 | | -``` |
596 | | - |
597 | 534 | ## <a name="invoke"></a> invoke |
598 | 535 |
|
599 | 536 | Invokes any callable object with the given arguments. Added in C++17. |
@@ -701,9 +638,72 @@ int main() |
701 | 638 | } |
702 | 639 | ``` |
703 | 640 |
|
| 641 | +## <a name="mem_fn"></a> mem_fn |
| 642 | + |
| 643 | +Generates a simple call wrapper. |
| 644 | + |
| 645 | +```cpp |
| 646 | +template <class Ret, class Ty> |
| 647 | +unspecified mem_fn(Ret Ty::*pm); |
| 648 | +``` |
| 649 | +
|
| 650 | +### Parameters |
| 651 | +
|
| 652 | +*Ret*<br/> |
| 653 | +The return type of the wrapped function. |
| 654 | +
|
| 655 | +*Ty*<br/> |
| 656 | +The type of the member function pointer. |
| 657 | +
|
| 658 | +### Remarks |
| 659 | +
|
| 660 | +The template function returns a simple call wrapper `cw`, with a weak result type, such that the expression `cw(t, a2, ..., aN)` is equivalent to `INVOKE(pm, t, a2, ..., aN)`. It does not throw any exceptions. |
| 661 | +
|
| 662 | +The returned call wrapper is derived from `std::unary_function<cv Ty*, Ret>` (hence defining the nested type `result_type` as a synonym for *Ret* and the nested type `argument_type` as a synonym for `cv Ty*`) only if the type *Ty* is a pointer to member function with cv-qualifier `cv` that takes no arguments. |
| 663 | +
|
| 664 | +The returned call wrapper is derived from `std::binary_function<cv Ty*, T2, Ret>` (hence defining the nested type `result_type` as a synonym for *Ret*, the nested type `first argument_type` as a synonym for `cv Ty*`, and the nested type `second argument_type` as a synonym for `T2`) only if the type *Ty* is a pointer to member function with cv-qualifier `cv` that takes one argument, of type `T2`. |
| 665 | +
|
| 666 | +### Example |
| 667 | +
|
| 668 | +```cpp |
| 669 | +// std__functional__mem_fn.cpp |
| 670 | +// compile with: /EHsc |
| 671 | +#include <functional> |
| 672 | +#include <iostream> |
| 673 | +
|
| 674 | +class Funs |
| 675 | + { |
| 676 | +public: |
| 677 | + void square(double x) |
| 678 | + { |
| 679 | + std::cout << x << "^2 == " << x * x << std::endl; |
| 680 | + } |
| 681 | +
|
| 682 | + void product(double x, double y) |
| 683 | + { |
| 684 | + std::cout << x << "*" << y << " == " << x * y << std::endl; |
| 685 | + } |
| 686 | + }; |
| 687 | +
|
| 688 | +int main() |
| 689 | + { |
| 690 | + Funs funs; |
| 691 | +
|
| 692 | + std::mem_fn(&Funs::square)(funs, 3.0); |
| 693 | + std::mem_fn(&Funs::product)(funs, 3.0, 2.0); |
| 694 | +
|
| 695 | + return (0); |
| 696 | + } |
| 697 | +``` |
| 698 | + |
| 699 | +```Output |
| 700 | +3^2 == 9 |
| 701 | +3*2 == 6 |
| 702 | +``` |
| 703 | + |
704 | 704 | ## <a name="mem_fun"></a> mem_fun |
705 | 705 |
|
706 | | -Helper template functions used to construct function object adaptors for member functions when initialized with pointer arguments. Deprecated in C++11, removed in C++17. |
| 706 | +Helper template functions used to construct function object adaptors for member functions when initialized with pointer arguments. Deprecated in C++11 in favor of [mem_fn](#mem_fn) and [bind](#bind), and removed in C++17. |
707 | 707 |
|
708 | 708 | ```cpp |
709 | 709 | template <class Result, class Type> |
|
0 commit comments