Function Objects
Collaboration diagram for Function Objects:


 Adaptors for pointers to functions
 Adaptors for pointers to members
 Arithmetic Classes
 Binder Classes
 Boolean Operations Classes
 Comparison Classes


struct  std::binary_function< _Arg1, _Arg2, _Result >
class  std::function< _Res(_ArgTypes...)>
class  std::reference_wrapper< _Tp >
struct  std::unary_function< _Arg, _Result >


template<typename _Tp , typename _Class >
_Mem_fn< _Tp _Class::* > std::mem_fn (_Tp _Class::*__pm) noexcept

Detailed Description

Function objects, or functors, are objects with an operator() defined and accessible. They can be passed as arguments to algorithm templates and used in place of a function pointer. Not only is the resulting expressiveness of the library increased, but the generated code can be more efficient than what you might write by hand. When we refer to functors, then, generally we include function pointers in the description as well.

Often, functors are only created as temporaries passed to algorithm calls, rather than being created as named variables.

Two examples taken from the standard itself follow. To perform a by-element addition of two vectors a and b containing double, and put the result in a, use

transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());

To negate every element in a, use

transform(a.begin(), a.end(), a.begin(), negate<double>());

The addition and negation functions will be inlined directly.

The standard functors are derived from structs named unary_function and binary_function. These two classes contain nothing but typedefs, to aid in generic (template) programming. If you write your own functors, you might consider doing the same.

Function Documentation

◆ mem_fn()

template<typename _Tp , typename _Class >
_Mem_fn<_Tp _Class::*> std::mem_fn ( _Tp _Class::*  __pm)

Returns a function object that forwards to the member pointer pm.

Definition at line 160 of file functional.