function template
<functional>
std::mem_fn
template <class Ret, class T>
/* unspecified */ mem_fn (Ret T::* pm);
Convert member function to function object
Returns a function object whose functional call invokes the member function pointed by pm.
The type of the returned object has the following properties:
- Its functional call takes as first argument an object of type T (or a reference or a pointer to it) and, as additional arguments, the arguments taken by pm (if any). The effect of such a call with fn as first argument are the same as calling
fn.*pm
(or (*fn).*pm
if fn is a pointer), forwarding any additional arguments.
- It has a member result_type, defined as an alias of Ret (which is the return type of pm).
- If the member pointed by pm takes no arguments, it has a member argument_type, defined as an alias of
T*
.
- If the member pointed by pm takes one argument, it has a member first_argument_type, defined as an alias of
T*
, and a member second_argument_type, defined as an alias of the argument taken by pm.
- It is nothrow move-constructible, nothrow copy-constructible and nothrow copy-assignable.
Parameters
- pm
- Pointer to a member function.
Return value
A function object that, when called, calls pm on the object passed as first argument.
The return type is unspecified, but the type returned is a function object class with the properties described above.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
// mem_fn example
#include <iostream> // std::cout
#include <functional> // std::mem_fn
struct int_holder {
int value;
int triple() {return value*3;}
};
int main () {
int_holder five {5};
// call member directly:
std::cout << five.triple() << '\n';
// same as above using a mem_fn:
auto triple = std::mem_fn (&int_holder::triple);
std::cout << triple(five) << '\n';
return 0;
}
| |
Output:
Exception safety
No-throw guarantee: never throws exceptions.
See also
- reference_wrapper
- Reference wrapper (class template
)
- bind
- Bind function arguments (function template
)