[Ranges] Position-based ranges

James Touton bekenn at gmail.com
Sat Mar 8 07:02:28 CET 2014

Hello, James Touton here.

I've been following the ongoing discussions on the ranges mailing list 
with interest, and I think I have something to contribute. I've been 
working on my own range concepts for a while now; you can find the basic 
idea at https://github.com/Bekenn/range. Generally, I approach the 
problem from the Sean Parent school of iterator pairs, only I weaken 
that idea to pairs of positions.  A key trait of a position is that it 
is meaningless absent the range object that defines it: positions can't 
be incremented or dereferenced without help from the parent range.  By 
shifting that logic into the range object, positions can remain very 
lightweight, which is important when using lazy evaluation via range 
adapters.  I describe positions in more detail in the comments at the 
top of range.h; here's an excerpt:

> Positions are similar to (but weaker than) iterators, as understood by 
> the C++ standard library.
> Like iterators, positions can be used (in conjunction with a sequence) 
> to reference a particular
> element in a sequence.  Unlike iterators, positions do not generally 
> reference an element absent
> some form of reference to the sequence as a whole.  In operational 
> terms, here are some key
> differences between positions and iterators:
> Positions
> -----------------
> Cannot be directly dereferenced
> Cannot be directly incremented
> Can be compared for equality only
>  in the context of a given sequence
> Iterators
> -----------------
> Can be directly dereferenced (*i)
> Can be directly incremented (++i)
> Can always be compared for equality (i == j, i != j)
> Positions are DefaultConstructible, CopyConstructible, 
> MoveConstructible, and EqualityComparable.
> These additional constraints are placed on position operations:
> p == q:
>    If p and q are positions referencing elements of the same sequence, 
> then p == q iff p and q
>    reference the same element.  If p and q are positions referencing 
> elements of different
>    sequences, then p == q has no inherent meaning.
> p != q:
>    !(p == q)

Let me know what you think.

More information about the Ranges mailing list