Previous fileTop of DocumentContentsIndex pageNext file
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:

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

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:

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:

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:

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.

Previous fileTop of DocumentContentsIndex pageNext file