Compose Function Object Adapters

The C++ Standard Template Library STL as part of the C++ Standard Library provides several function objects. Unfortunately, it doesn't provide the ability to compose these function objects. For example, it is not possible to combine the result of two unary operations to formulate a criterion such as "this and that". But this would be important for building software components out of other components, which leads to the concept of functional composition. So, here are some adapters that close this gap.

In fact, these adapters provide to compose function objects as follows:
 
Functionality Boost Name SGI STL's Name
f(g(value)) compose_f_gx compose1
f(g(value),h(value)) compose_f_gx_hx compose2
f(g(value1),h(value2)) compose_f_gx_hy
f(g(value1,value2)) compose_f_gxy
f(g()) compose_f_g

As you can see, two of these adapters are part of the SGI STL already. However, we changed the names according to a consistent naming scheme for all adapters.

In addition, this code also defines a type nullary_function for function objects that have no argument (as supplement to unary_function and binary_function) and overloads ptr_fun for functions that take no arguments.

The code is provided "as is" without expressed or implied warranty.

compose.hpp:

  • as HTML file
  • as plain file

    comptest1.cpp, an example for using compose_f_gx
  • as HTML file
  • as plain file

  • comptest2.cpp, an example for using compose_f_gx_hx
  • as HTML file
  • as plain file

  • comptest3.cpp, an example for using compose_f_gx_hy
  • as HTML file
  • as plain file

  • comptest4.cpp, an example for using compose_f_g and ptr_fun for functions without arguments
  • as HTML file
  • as plain file

    All files
  • as ZIP file (22KB)
  • as TGZ file (13KB)

    To find more details about function objects in general and about these compose functions, see
         The C++ Standard Library - A Tutorial and Reference
         by Nicolai M. Josuttis
         Addison Wesley Longman, 1999
         ISBN 0-201-37926-0
    and the SGI STL documentation
  • Note that this library was part of Boost, but is deprecated right now. You might consider to use Boost.Bind, instead.

    Home Page