[cfe-dev] C++11 reverse iterators (was C++11 is here)

Duncan P. N. Exon Smith dexonsmith at apple.com
Tue Mar 4 21:26:18 PST 2014


On 2014 Mar 4, at 20:23, Chandler Carruth <chandlerc at google.com> wrote:

> On Tue, Mar 4, 2014 at 8:07 PM, Duncan P. N. Exon Smith <dexonsmith at apple.com> wrote:
> There’s a decent selection of range adaptors in Boost.Range [1].  I’m not sure the license [2] allows copying the source (IANAL), but any reason not use the same names?  I don’t see any reason to reinvent the wheel here.
> 
> Because I think that algorithms (and functions more generally) should be verbs. The names will be very similar all the same.

I agree.  Algorithms should be verbs.

However, I disagree that range adaptors are algorithms.

“reverse” sounds like an algorithm.  Consider, from the STL:

    template <class Iterator> void reverse(Iterator F, Iterator L);

I’d expect a range version of the algorithm to look like this:

    template <class Range> void reverse(Range &R) {
      reverse(std::begin(R), std::end(R));
    }

On the other hand, “reversed” sounds like an accessor, and I’d expect it to look something like this:

    template <class Range> reversed_range<Range::iterator>
        reversed(Range &R) {
      return make_range(make_reverse_iterator(std::end(R)),
                        make_reverse_iterator(std::begin(R)));
    }
    template <class Range> reversed_range<Range::const_iterator>
        reversed(const Range &R) {
      return make_range(make_reverse_iterator(std::end(R)),
                        make_reverse_iterator(std::begin(R)));
    }
    template <class Iterator> iterator_range<Iterator>
        reversed(const reversed_range<Iterator> &R) {
      return make_range(R.end().base(), R.begin().base());
    }

IMO, Adaptors are more akin to accessors than they are to algorithms.  If it has to be a verb, then I think getReversed() is better than reverse().



More information about the cfe-dev mailing list