Overview

Function objects (also called functors) are an STL feature that you may not employ immediately when you start using the STL. They are, however, very useful in many situations and an STL facility with which you should become acquainted. They give the STL a flexibility that it would not otherwise have, and also contribute to STL efficiency. The most common uses for function objects are for generating data, for testing data, and for applying operations to data.

A function object (or functor) is simply any object of a class that provides at least one definition for operator() What this means is that if you then declare an object f of the class in which this operator() is defined you can subsequently use that object f just like you would use an "ordinary" function. For example, you could have an assignment statement like

someValue = f(arg1, arg2);

which is the same as

someValue = f.operator()(arg1, arg2);

so long as operator() for the given class had been defined to do the following two things:

  1. It must take two parameters (par1 and par2, say, of whatever type(s)), with arg1 and arg2 in the above assignment statement being actual parameters of the corresponding type(s).
  2. It must return a value of the type of the variable someValue

If you were only going to use a functor as illustrated in the assignment statement example or the sample program given above, you might as well use an ordinary function. In the STL, algorithms often take a parameter which is a function (or functor) telling the algorithm how to perform some part of its task, and functors are generally more versatile and hence more useful for this purpose. In particular, the built-in functors are objects of template classes, and so the same computations performed by a given functor can be applied to different types.

Binary functors (or ordinary binary functions) that return a boolean value are called binary predicates or comparitors or comparison functions. Unary functors (or ordinary unary functions) that return a boolean value are called unary predicates. Either variety of functor that returns a boolean value may be referred to simply as a predicate function.

Programmer-defined functors of binary or unary type

Clearly any C++ programmer may define a class which includes an overloaded operator(). Once this is done, that C++ programmer just as clearly has a programmer-defined functor at his or her disposal, a binary functor if it has two parameters and a unary functor if it has a single paramter (for example).

Built-in functors of binary type (available from <functional>)

In each of the functors of this section arg1 and arg2 both have type T.


Arithmetic binary functors

plus<T> f;
f(arg1, arg2) returns the value arg1 + arg2.
minus<T> f;
f(arg1, arg2) returns the value arg1 - arg2.
multiplies<T> f;
f(arg1, arg2) returns the value arg1 * arg2.
divides<T> f;
f(arg1, arg2) returns the value arg1 / arg2.
modulus<T> f;
f(arg1, arg2) returns the value arg1 % arg2.

Relational binary functors

equal_to<T> f;
f(arg1, arg2) returns the value arg1 == arg2.
not_equal_to<T> f;
f(arg1, arg2) returns the value arg1 != arg2.
greater<T> f;
f(arg1, arg2) returns the value arg1 > arg2.
greater_equal<T> f;
f(arg1, arg2) returns the value arg1 >= arg2.
less<T> f;
f(arg1, arg2) returns the value arg1 < arg2.
less_equal<T> f;
f(arg1, arg2) returns the value arg1 <= arg2.

Logical binary functors

logical_and<T> f;
f(arg1, arg2) returns the value arg1 && arg2.
logical_or<T> f;
f(arg1, arg2) returns the value arg1 || arg2.

Built-in functors of unary type (available from <functional>)

In each of the functors of this section arg has type T.


Arithmetic unary functor

negate<T> f;
f(arg) returns the value -arg.

Logical unary functor

logical_not<T> f;
f(arg) returns the value !arg.

Sample Programs

function_object.cpp | Windows_executable | program_output (text)
Illustrates a very simple programmer-defined function object.
function_objects_stl.cpp | Windows_executable | program_output (text)
Illustrates several STL built-in function objects.