Using STL algorithms, is it better to pass a function pointer or a functor?
c++, functor, stl
Solution
Functors may (and will) be trivially inlined – this isn't done for regular function pointers.
Thus, functors have a real performance benefit which may be huge in tight loops. Furthermore, functors are generally more easily composable and in particuler play nicer with the STL: `std::bind``x` doesn't work on function pointers, for instance.
I hate how they clutter the code but given all the advantages, I'd prefer them over function pointers any time.
Problem
Which of these 2 methods is better and why? Method 1: ``` void fun(int i) { //do stuff } ... for_each(a.begin(), a.end(), fun); ``` Method 2: ``` class functor { public: void operator()(int i); }; ... for_each(a.begin(), a.end(), functor()); ``` Edit: Should have formulated it this way, in what situation is one of the above method preferable to the other? Thanks a lot!