Function objects are objects specifically designed to be used with a syntax similar to that of functions. In C++, this is achieved by defining member function operator() in their class, like for example:
1
2
3
4
struct myclass {
intoperator()(int a) {return a;}
} myobject;
int x = myobject (0); // function-like syntax with object myobject
They are especially useful as predicates or comparison functions to be used with standard algorithms. The standard library provides standard definitions for several function objects and some ways to modify and adapt their behavior in header <functional>: