Stroustrup gives the example of using an overloaded operator()
to execute a function on vector elements during a transform operation:
class Example
{
public:
std::vector<int> Test1 {1,2,3,4,5};
std::vector<int> Test2;
int operator()(int el);
void MyFunction();
}
int Example::operator()(int el)
{
return el + 1;
}
void Example::MyFunction()
{
std::transform(Test1.begin(), Test1.end(), std::back_inserter(Test2), std::bind(Example(), std::placeholders::_1))
}
However, with the introduction of lambda expressions the above code seems very verbose, compared to:
std::transform(Test1.begin(), Test1.end(), std::back_inserter(Test2), [](int el){return el + 1;});
Am I right in saying there is little value in using the overloaded operator()
approach? Or are there still scenarios where they can be beneficial when working with STL algorithms?