function template
<functional>

std::mem_fun_ref

template <class S, class T>
  mem_fun_ref_t<S,T> mem_fun_ref (S (T::*f)());

template <class S, class T, class A>
  mem_fun1_ref_t<S,T,A> mem_fun_ref (S (T::*f)(A));

template <class S, class T>
  const_mem_fun_ref_t<S,T> mem_fun_ref (S (T::*f)() const);

template <class S, class T, class A>
  const_mem_fun1_ref_t<S,T,A> mem_fun_ref (S (T::*f)(A) const);
Convert member function to function object (reference 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 reference to an object as its (first) argument for operator(). A similar function, mem_fun generates the same function but expecting a pointer 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
12
13
14
15
template <class S, class T>
  mem_fun_ref_t<S,T> mem_fun_ref (S (T::*f)())
  { return mem_fun_ref_t<S,T>(f); }

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

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

template <class S, class T, class A>
  const_mem_fun1_ref_t<S,T,A> mem_fun_ref (S (T::*f)(A) const)
  { return const_mem_fun1_ref_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 passed as its (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
// mem_fun_ref example
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

int main () {
  vector<string> numbers;

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

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

  transform (numbers.begin(), numbers.end(), lengths.begin(), mem_fun_ref(&string::length));
	
  for (int i=0; i<5; i++) {
	  cout << numbers[i] << " has " << lengths[i] << " letters.\n";
  }
  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