range.save

Joseph Rushton Wakeling via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 27 02:17:44 PST 2015


On Friday, 27 November 2015 at 09:20:23 UTC, Jonathan M Davis 
wrote:
> Obviously, Andrei will have to answer to know what he meant, 
> but with regards to ranges, I would consider a reference type 
> to be one where in
>
> auto copy = range;
>
> doing anything to copy or range does the exact same thing to 
> the other, because they refer to the exact same state. 
> Something like save is required to get a separate range where 
> popping elements from one will not affect the other.

Unfortunately it's a bit more complicated than that, because it's 
readily possible to have ranges where

     auto copy = range;

... will copy _some_ of the internals by value, and some by 
reference -- e.g. a range whose private data includes some 
integer values and a dynamic array.

That's not necessarily a problem if the reference-type data does 
not influence the range's behaviour (e.g. you're doing forward 
iteration over a container accessed by ref), but it's readily 
possible to imagine a range design where

     auto copy = range;
     copy.popFront();

... will affect range's state without updating it to the _same_ 
state as copy.


More information about the Digitalmars-d mailing list