function template
<functional>

std::mem_fun

template <class S, class T> mem_fun_t<S,T> mem_fun (S (T::*f)());template <class S, class T, class A> mem_fun1_t<S,T,A> mem_fun (S (T::*f)(A));template <class S, class T> const_mem_fun_t<S,T> mem_fun (S (T::*f)() const);template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun (S (T::*f)(A) const);
Convert member function to function object (pointer version)
Returns a function object that encapsulates member function f of type T. The member function returns a value of type S and, optionally, can take one parameter of type A.

The function object returned by this function expects a pointer to an object as its (first) argument for operator(). A similar function, mem_fun_ref generates the same function but expecting a reference to an object as (first) argument instead.

Function objects are objects whose class defines member function operator(). This member function allows the object to be used with the same syntax as a regular function call. Several standard algorithms and adaptors are designed to be used with function objects.

It is defined with the same behavior as:

1
2
3
4
5
6
7
8
9
10
11
template <class S, class T> mem_fun_t<S,T> mem_fun (S (T::*f)())
{ return mem_fun_t<S,T>(f); }

template <class S, class T, class A> mem_fun1_t<S,T,A> mem_fun (S (T::*f)(A))
{ return mem_fun1_t<S,T,A>(f); }

template <class S, class T> const_mem_fun_t<S,T> mem_fun (S (T::*f)() const)
{ return const_mem_fun_t<S,T>(f); }

template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun (S (T::*f)(A) const)
{ return const_mem_fun1_t<S,T,A>(f); }

Template parameters

S
Return type of member function.
T
Type (class) of which the member function is a member.
A
Type of the argument taken by the member function (if any).

Parameters

f
Pointer to a member function, taking either one argument (of type A) or no arguments, and returning a value of type S.

Return value

A function object which calls the member function f of the object whose pointer is passed as the (first) argument.
If the member function accepts one parameter, this is specified as the second argument in the function object.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// mem_fun example
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

int main () {
  vector <string*> numbers;

  // populate vector of pointers:
  numbers.push_back ( new string ("one") );
  numbers.push_back ( new string ("two") );
  numbers.push_back ( new string ("three") );
  numbers.push_back ( new string ("four") );
  numbers.push_back ( new string ("five") );

  vector <int> lengths ( numbers.size() );

  transform (numbers.begin(), numbers.end(), lengths.begin(), mem_fun(&string::length));
	
  for (int i=0; i<5; i++) {
    cout << *numbers[i] << " has " << lengths[i] << " letters.\n";
  }

  // deallocate strings:
  for (vector<string*>::iterator it = numbers.begin(); it!=numbers.end(); ++it)
    delete *it;

  return 0;
}

Output:

one has 3 letters.
two has 3 letters.
three has 5 letters.
four has 4 letters.
five has 4 letters.


See also