Function objects, also known as functors, are a powerful feature in C++ Standard Template Library (STL). They are objects that can be called like functions, providing a flexible alternative to traditional function pointers.
Function objects are instances of classes that overload the function call operator (). This allows them to be used as if they were functions, while retaining the ability to maintain state between calls.
To create a function object, define a class with the operator() member function:
class MyFunctor {
public:
int operator()(int x, int y) {
return x + y;
}
};
You can then use the functor like this:
MyFunctor addObj;
int result = addObj(5, 3); // result = 8
The STL provides several predefined function objects in the <functional> header:
plus<T>minus<T>multiplies<T>divides<T>modulus<T>negate<T>
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
int sum = 0;
std::for_each(numbers.begin(), numbers.end(), [&sum](int n) {
sum += n;
});
std::cout << "Sum: " << sum << std::endl;
return 0;
}
You can create custom function objects to encapsulate complex operations or maintain state. Here's an example of a custom functor that counts how many times it's been called:
class Counter {
private:
int count = 0;
public:
int operator()() {
return ++count;
}
};
int main() {
Counter c;
std::cout << c() << std::endl; // Outputs: 1
std::cout << c() << std::endl; // Outputs: 2
std::cout << c() << std::endl; // Outputs: 3
return 0;
}
While Lambda Expressions provide a concise way to create inline function objects, traditional functors still have their place:
operator() functionsFunction objects are a cornerstone of generic programming in C++. They provide a flexible and efficient way to customize the behavior of STL algorithms and containers. By mastering functors, you'll be able to write more expressive and performant C++ code.