Difference between input range and forward range
Jesse Phillips via Digitalmars-d
digitalmars-d at puremagic.com
Wed Nov 11 14:34:30 PST 2015
On Tuesday, 10 November 2015 at 16:07:01 UTC, Jonathan M Davis
wrote:
> generic code, you have to consider it to be consumed, because
> the state of range you passed to foreach is now undefined,
> since what happens when copying the range is undefined. This is
> true even if you put a break out of the loop, because the range
> was copied, and you simply cannot rely on the state of the
> range you passed to foreach after that copy.
The problem I find with generic code is when the desire is to
consume the data. Take this example of parsing a data stream.
auto osmData = datastream.take(size).array;
datastream.popFrontN(size);
auto header = BlobHeader(osmData);
http://he-the-great.livejournal.com/49636.html
How do I know if popFrontN is needed? If I was given a value base
range then it is. If I was given a reference range (in its many
forms) the extra call to popFrontN will result in an unneeded
data jump. I could require that a forward range is passed in,
then I can save() before calling .array and thus always require
popFrontN.
The best option is probably to use the RefRange wrapper, but it
does create an annoying element of surprise.
More information about the Digitalmars-d
mailing list