Classes with operator Functors

suggest change

Every class which overloads the operator() can be used as a function object. These classes can be written by hand (often referred to as functors) or automatically generated by the compiler by writing Lambdas from C++11 on.

struct Person {
    std::string name;
    unsigned int age;
};

// Functor which find a person by name
struct FindPersonByName {
    FindPersonByName(const std::string &name) : _name(name) {}

    // Overloaded method which will get called
    bool operator()(const Person &person) const {
         return person.name == _name;
    }
private:
    std::string _name;
};

std::vector<Person> v; // Assume this contains data
std::vector<Person>::iterator iFind =
    std::find_if(v.begin(), v.end(), FindPersonByName("Foobar"));
// ...

As functors have their own identity, they cannot be put in a typedef and these have to be accepted via template argument. The definition of std::find_if can look like:

template<typename Iterator, typename Predicate>
Iterator find_if(Iterator begin, Iterator end, Predicate &predicate) {
     for (Iterator i = begin, i != end, ++i)
         if (predicate(*i))
             return i;
     return end;
}

From C++17 on, the calling of the predicate can be done with invoke: std::invoke(predicate, *i).

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Callable objects:
* Classes with operator Functors

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
108 Callable objects
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype