# Functors in C++

A functor in C++ is something that can be called like a function – a function pointer or object of a class with an overloaded operator(). It is the second type that is most commonly being referred to when people talk about functors.

The advantages of having an object of a class with an overloaded operator() over a function pointer are:

1. The object can maintain state between calls
2. Calling the overloaded operator() can be more efficient than dereferencing a function pointer

## Maintaining state

A class with an overloaded operator() can have member variables, and these allow it to maintain state through function calls. For example, a functor to calculate the Fibonacci numbers could maintain a cache of the ones seen so far to avoid recalculating them.

#include <vector>
#include <iostream>

class Fibonacci
{
public:
Fibonacci()
{
results_.push_back(0);
results_.push_back(1);
}
unsigned int operator()(unsigned int n)
{
while (results_.size() < n + 1) {
results_.push_back(results_[results_.size() - 2] + results_[results_.size() - 1]);
}
return results_[n];
}
private:
std::vector<unsigned int> results_;
};

int main()
{
Fibonacci fib;
for (unsigned int n = 0; n < 10; n++) {
std::cout << fib(n) << "\n";
}
}


## Efficiency

Functors are commonly used with standard library algorithms like std::for_each() and std::transform(), as in the example below, which fills a vector with the first 100 Fibonaci numbers:

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>

int main()
{
Fibonacci fib;
std::vector<unsigned int> results(100);
std::iota(results.begin(), results.end(), 0);
std::transform(results.begin(), results.end(), results.begin(), fib);
std::copy(results.begin(), results.end(),
std::ostream_iterator<unsigned int>(std::cout, " "));
}


When an ordinary function pointer is passed to one of these algorithms, it’s difficult for the compiler to optimise the function call because a function pointer is a variable and so could change to point to a different function. When the pointer is to a virtual member function there is an extra level of indirection to get to the version in the actual class. With an overloaded operator() however, there is no indirection and the function to be called is fixed, so the compiler can optimise the call by inlining the function’s code at the call site. This can make the call much more efficient.