Click on the banner to return to the user guide home page.

3.4 Function Adaptors

A function adaptor is an instance of a class that adapts a global or member function so that the function can be used as a function object (a function adaptor may also be used to alter the behavior of a function or function object, as is the case in the next section). Each function adaptor provides a constructor that takes a global or member function. The adaptor also provides a parenthesis operator that forwards its call to that associated global or member function.

The pointer_to_unary_functoin and pointer_to_binary_function templates adapt global functions of one or two arguments. These adaptors can be applied directly, or you can use the ptr_fun function template to construct the appropriate adaptor automatically. For instance, I can adapt a simple times3 function and apply it to a vector of integers as follows:

int times3(int x) {
  return 3*x;
}

int a{} {1,2,3,4,5};
vector<int> v(a,a+5), v2;

transform(v.begin(),v.end(),v2.end(),ptr_fun(times3));

Alternatively, I could have applied the adaptor, and then passed the new, adapted, function object to my vector.

pointer_to_unary_function<int,int> pf(times3);
transform(v.begin(),v.end(),v2.end(),pf);

Here you can see the advantage of allowing the compiler to deduce the types needed by pointer_to_unary_function through the use of ptr_fun.

The mem_fun family of templates adapts member functions, rather than global functions. For instance, if I have a set of lists, and I want to sort each list in the set, I can use mem_fun_t (or more simply mem_fun) to apply the list sort member function to each element in the set.

set<list<int>* > s;

// Initialize the set with lists
...
// Sort each list in the set.
for_each(s.begin(),s.end(),mem_fun(&list<int>::sort));

// Now each list in the set is sorted

This is necessary because the generic sort algorithm cannot be used on a list. This is also the simplest way to access any polymorphic characteristics of an object held in a standard container. For instance I might invoke a virtual draw function on a collection of objects that are all part of the canonical 'shape' hierarchy like this:

// shape hierarchy
class shape {
   virtual void draw();
};

class circle : public shape {
   void draw();
};

class square : public shape {
  void draw();
};

//  Assemble a vector of shapes
circle c;
square s;
vector<shape*> v;
v.push_back(&s);
v.push_back(&c);

// Call draw on each one
for_each(v.begin(),v.end(), mem_fun(&shape::draw));

Similarly to the global function adaptors, each member function adaptor consists of a class template and an associated function template. The class is the actual adaptor, while the function simplifies the use of the class by constructing instances of that class on the fly. For instance, in the above example I could have constructed a mem_fun_t myself, and then passed that to the for_each algorithm:

mem_fun_t<shape> mf(&shape::draw);
for_each(v.begin(),v.end(),mf);

Again, you can see that the mem_fun function template simplifies the use of the mem_fun_t adaptor by allowing the compiler to deduce the type needed by mem_fun_t.

The library provides member function adaptors for functions with zero arguments (as above) and one argument. This can be easily extended to member functions with more arguments.




©Copyright 1996, Rogue Wave Software, Inc.