Apache C++ Standard Library User's Guide

## 3.4 Function Adaptors

### 3.4.1 Definition

A function adaptor is an instance of a class that adapts a namespace-scope 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 shown in Section 3.5. Each function adaptor defined by the C++ Standard Library provides a constructor that takes a namespace-scope or member function. The adaptor also defines a function call operator that forwards its call to that associated global or member function.

### 3.4.2 Adapting Non-member or Static Member Functions

The pointer_to_unary_function and pointer_to_binary_function templates adapt namespace-scope or static member functions of one or two arguments. These adaptors can be applied directly, or the ptr_fun function template can be used to construct the appropriate adaptor automatically without explicitly specifying the template arguments. For instance, a simple `times3` function can be adapted and applied to a vector of integers as follows:

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

    int a[] = {1,2,3,4,5};
    std::vector<int> v(a,a+5), v2;

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

Alternatively, the adaptor could have been applied, and the new, adapted function object passed to the vector:

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

This example points out the advantage of allowing the compiler to deduce the types needed by pointer_to_unary_function through the use of `ptr_fun`.

### 3.4.3 Adapting Member Functions

The mem_fun family of templates adapts member functions. For instance, to sort each list in a given set of lists, mem_fun can be used to apply the list sort member function to each element in the set:

    std::set<list<int>* > s;

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

    // Now each list in the set is sorted

Using mem_fun is necessary because the generic sort algorithm requires random access iterators and thus cannot be used on a list. It is also the simplest way to access any polymorphic characteristics of an object held in a standard container. For instance, a virtual draw function might be invoked on a collection of objects that are all part of the canonical `shape` hierarchy like this:

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

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

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

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

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

Each member function adaptor comes with an associated function template for convenience. The class template is the actual adaptor, while the function simplifies the use of the template by constructing instances of that class on the fly. For instance, in the previous example, a user could construct a const _mem_fun_t, and pass that to the `std::for_each()` algorithm:

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

Here again the mem_fun function template simplifies the use of the const_mem_fun_t adaptor by allowing the compiler to deduce the type needed by const_mem_fun_t. The fact that a const version of the function adaptor is needed (since the shapes are const) is obvious when the convenience function template is used.

The C++ Standard Library provides member function adaptors for functions with zero arguments as above and with one argument. Adaptors for member functions that operate on object references as opposed to object pointers such as those shown in the examples above are provided (mem_fun_ref_t), as are those that operate on pointers and references to const objects, respectively (const_mem_fun_t, const_mem_fun_ref_t). All of these adaptors can be easily extended to member functions with more arguments.