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
source share