Suppose we have a functor, which contains a couple different ref-qualified or simply const-qualified overloadings of operator ()
. I want to different ones to be called for different contexts of using with STL algorithms. Namely, I want to proper overloading of operator ()
to be called with dependence on constness or "lvalue/rvalue-ness" of functor value/reference, that I provide to STL algorithm. Say, there is hypotetic algorithm recurrent_all_of
. The form which I potentially expect to find in STL is:
template< typename Iterator, typename BinaryFunctor >
bool
recurrent_all_of(Iterator begin, Iterator end, BinaryFunctor functor)
{
if (begin == end) {
return false;
}
Iterator previous = begin;
while (++begin != end) {
if (!functor(*previous, *begin)) {
return false;
}
previous = begin;
}
return (++previous != end);
}
But to meet a claim to being referenceness friendly the form should be as follows:
template< typename Iterator, typename BinaryFunctor >
bool
recurrent_all_of(Iterator begin, Iterator end, BinaryFunctor && functor)
{
if (begin == end) {
return false;
}
Iterator previous = begin;
while (++begin != end) {
if (!std::forward< BinaryFunctor >(functor)(*previous, *begin)) {
return false;
}
previous = begin;
}
return (++previous != end);
}
Is there any misconception? Why STL algorithms not designed in such way?