Introducing Functors
A Functor (function object) is similar to a class. The class that overloads the operator() function is also known as the function call operator.
The syntax that's used to define a functor is as follows:
class class_name { public: type operator()(type arg) {} };
The function call operator has a return type and takes any number of arguments of any type. To invoke the call operator of an object, we can write the name of the object, followed by parentheses containing the arguments to pass to the operator. You can imagine that an object that provides a call operator can be used in the same way as you would use a function. Here's an example of a functor:
class_name obj; type t; /* obj is an instance of a class with the call operator: it can be used as if it was a function */ obj(t);
They are particularly useful in places where you can pass a function object to an algorithmic template that accepts an object with operator() defined. This exploits code reusability and testability...