Is there any performance advantage to be had when using template parameters with static member functions instead of functor-style predicates??
For instance, a functor-style sort interface is typically something like this:
template <typename _Type, typename _Pred>
void sort (
RandomAccessIterator first,
RandomAccessIterator last ,
_Pred less_than
)
{
// actual sorting code here, calling less_than()...
}
You could do something more like this, and require that _Pred contained a static member function _Pred::less_than:
template <typename _Type, typename _Pred>
void sort (
RandomAccessIterator first,
RandomAccessIterator last
)
{
// actual sorting code here, calling _Pred::less_than()...
}
In theory, the first case might dynamically create a temporary functor object on the heap, whereas I believe that the second case is fully evaluated at compile time. I understand that (say) gcc and/or msvc are good at optimising, but can this be done to the same degree in the first case??
Also, I’m not trying to rewrite the STL sort routines or anything like that, just an example for a more general functor question…
Normal use of
sortwon’t put anything on the heap, for the simple reason that nobody callsmallocornew. If your predicate causes a call tomallocornew, either in its constructor or in the comparison, then you only have yourself to blame…It’s plausible that some stack will be used for the parameter of type
_Pred(you must not call a template parameter_Predin your code, because_Predis a reserved symbol. It can be called that in the implementation ofstd::sort). But there won’t be any associated work to do, beyond what’s necessary for any data members that the predicate object might have. If the predicate has no data members then the optimizer will have a field day, and if it does have data members then a static member function wouldn’t support what the user wants to do.As long as
operator()in the predicate is non-virtual, the compiler can inline it into the instantiation ofsortif it can see the definition and if it feels that’s best. Of course there are no guarantees what’s faster, but there’s no reason to suppose that a call to a static member function is any faster or slower than a call to a non-virtual non-static member function, nor that it’s any easier or harder to inline.