1

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?

Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
  • Some [notes](http://stackoverflow.com/a/23614591/2567683) on this – Nikos Athanasiou May 29 '14 at 04:51
  • @NikosAthanasiou Once I've already thought about the `std:ref`/`std::cref` using, but what about the rvalue-references? There is some intuitions in `` header: we can construct an one-element tuple of single reference by means of `std::tie`, but there is absence of `template< typename ...Args > std::tuple::operator () (Args &&...) cv-ref-qualifier` to forward arguments and call one-element tuple's contained functor. – Tomilov Anatoliy May 29 '14 at 05:33

0 Answers0