The advantage of functional objects?

I knew that function objects used in STL are just a simple object that we can use as a function. I can say that the work of function objects and functions is the same. If this is true, then why should we use a functional object, not a function?

+3
source share
4 answers

The main advantage is that calls to functional objects (functors) are usually built-in, where, as a rule, calls to function pointers are not (an example of a simple comparison of C qsort with C ++ std::sort ). For non-trivial objects / comparators, C ++ should kill C performance for sorting.

There are other advantages, for example, you could associate or save a state in a functor with which you cannot make an unprocessed function.

Edit Apologies for the lack of direct links, but Scott Meyers claims that under certain circumstances a 670% improvement. Performance qsort vs std :: sort?

Edit 2 Walkthrough with performance record:

The fact that function pointer options prohibit nesting explains that long-time C programmers often find it hard to believe: C ++ sort almost always confuses Cs qsort when it comes to speed. Of course, C ++ has templates for functions and classes for instantiating and fun operator () functions for calling, while C makes a simple function call, but all that C ++ "overhead" is absorbed during the compilation. At run time, sorting makes built-in calls to compare the function (assuming the comparison function is declared built-in and its body is available at compile time), and qsort calls its comparison function via a pointer. The end result is that sorting is faster. In my tests for a million paired vector, it ran 670% faster, but don’t take my word for it, try it yourself. it is easy to verify that when comparing function objects and real functions as parameters of an algorithm, theres a bonus to abstraction.

 -Scott Meyers "Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library" - Item 46 
+6
source

The advantage of a functional object over a function is that it can contain state (from wikipedia ):

 #include <iostream> #include <iterator> #include <algorithm> class CountFrom { private: int &count; public: CountFrom(int &n) : count(n) {} int operator()() { return count++; } }; int main() { int state(10); std::generate_n(std::ostream_iterator<int>(std::cout, "\n"), 11, CountFrom(state)); return 0; } 

A regular function cannot contain state as an object of a function. If I remember correctly, this was a way to get around without having a lambda and no closing (before C ++ 11 the wikipedia section ) ...

+2
source

I think the best thing about functors is that they can store information internally. In those days without std::bind , you would have to write many unary comparison functions so that they can be passed to some routines, such as remove_if .

+1
source

See http://cs.stmarys.ca/~porter/csc/ref/stl/function_objects.html .

STL uses function objects (functors) as a callback to sort / search containers. Functors are templates, and therefore it is easier to implement them as classes. Try saying greater<T> with a function pointer ... given that containers in STL are also templates.

0
source

All Articles