Function Objects (STL)

A Function object is an object that in some way behaves like a function. Typically, that would mean an object of a class that defines the application operator operator.

A function object is a more general concept than a function because a function object can have state that persist across several calls (like a static local variable) and can be initialized and examined from outside the object (unlike a static local variable). For example:

Note that a function object with an inline application operator inlines beautifully because there are no pointers involved that might confuse optimizers. To contrast: current optimizers are rarely able to inline a call through a pointer to function.

Function objects are extensively used to provide flexibility in the Standard Library. There are many obvious uses for functions passed as arguments: logical predicates, arithmetic operations, operations for extracting information form elements, etc.

The STL's designers made the algorithms more flexible by allowing any algorithm that can receive a function pointer to receive an object of a class that overloads the parentheses operator with a function named operator, provided that the overloaded operator meets the requirements of the algorithm - in the case of the overloaded binary_search that takes a pointer to a function as the fourth argument, it must receive two arguments and return a bool. An object of such a class is known as a //**function object**// and can be used syntactically and semantically like a function or function pointer - the overloaded parentheses operator is invoked by using a function object's name followed by parantheses containing the arguments to the function. Together, function objects and functions are known as //**functors**//. Most algorithms can use function objects and functions interchangeably.

Function objects provide several advantages over function pointers. Since function objects are commonly implemented as class templates that are included into each source code file that uses them, the compiler can inline an overloaded operator to improve performance. Also, since they're objects of classes, function objects can have data members that operator can use to perform its task.

Example: Function Object and Function Pointer
The code demonstrates the accumulate numeric algorithm to calculate the sum of the squares of the elements in a vector. The fourth argument to accumulate is a binary function object (that is, a function object for which operator takes two arguments) or a function pointer to a binary function (that is, a function that takes two arguments).

Output:

Predefined Function Objects of the Standard Library
Many predefined function objects can be found in the header  . A list of the Standard Library function objects, which are all implemented as class templates:

^ Function Objects ^ Operator ^ Type ^ Function Objects ^ Operator ^ Type ^
 * plus | +  | arithmetic | greater |  >  | relational |
 * minus | -  | arithmetic | less |  <  | relational |
 * multiplies | *  | arithmetic | greater_equal |  >=  | relational |
 * divides | /  | arithmetic | less_equal |  <=  | relational |
 * modulus | %  | arithmetic | logical_and |  &&  | logical |
 * negate | -  | arithmetic | logical_or |  ||  | logical |
 * equal_to | ==  | relational | logical_not |  !  | logical |
 * not_equal_to | !=  | relational | | |