Functors are class objects which can be called like a normal function is called. An important example of functors are lambdas. Lambdas are syntactic sugar for functors.

Functors are created by overloading operator() function.

A simple example of functors:

#include <iostream>
 
class Counter
{
    int m_count{};
 
public:
    Counter() = default;
    Counter(int start)
        : m_count{start} {}
 
    int operator()()
    {
        return ++m_count;
    }
};
 
int main(int argc, char const *argv[])
{
    Counter count{};
 
    std::cout << count() << '\n';
    std::cout << count() << '\n';
    std::cout << count() << '\n';
    std::cout << count() << '\n';
    return 0;
}

We have a Counter class with overloaded operator(). When calling count, it increases the counter’s count.