Making sense of ranges
Jonathan M Davis
jmdavisProg at gmx.com
Sat Mar 24 19:34:49 PDT 2012
On Saturday, March 24, 2012 18:14:46 Ali Çehreli wrote:
> I looked for rationale on Andrei's article. There is this bit about STL
> forward iterators:
>
> <quote>
> Input and forward iterators are syntactically identical but subtly
> different semantically—copying a forward iterator saves iteration state,
> but copying an input iterator just creates a new view of the same
> iteration state.
> </quote>
>
> I guess that's why 'save' is explicit on ForwardRange.
Yes. If a range is a reference type rather than a value type or a dynamic
array, then you _must_ have save in order to get a copy of its current
state. However, since most ranges are either dynamic arrays or value types,
functions are often written without using save like they should and would
end up consuming a forward range if it's a reference type (e.g. a class).
But save was created primarily to make it possible to have classes which are
ranges rather than relying on assignment making a copy (and not all structs
are value types, so save can be required for them as well).
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list