Must ranges support `r1 = r2;`?

ag0aep6g anonymous at example.com
Sat Mar 24 23:28:32 UTC 2018


On 03/25/2018 12:02 AM, Jonathan M Davis wrote:
> auto range2 = range1; // now, range1 can't be used until it's assigned to
> range2.popFront();
> 
> range1 = range2; // now, range2 can't be used until it's assigned to
> range1.popFront();
> 
> And I don't think that RefRange violates that.

What RefRange violates is the assumption that range1 gets discarded, and 
that it simply gets overwritten by range2.

Note that `auto range2 = range1;` does not have that problem, because 
it's initialization, not assignment. It doesn't call RefRange's funky 
opAssign.

The various misbehaving Phobos functions assume that assignment works 
the same as initialization. But it doesn't with RefRange.

[...]
> As far as save goes, save is supposed to create an independent copy to
> iterate, and RefRange's documentation says that that's what it does, meaning
> that passing a RefRange to a function that uses the forward range API
> probably isn't very useful, but it would do what a forward range is supposed
> to do. Whether the actual implementation does that properly on not, I don't
> know - I'd have to study it - but per the documentation, it's at least
> designed to do the right thing with save.

RefRange.save works correctly. opAssign is the problem.


More information about the Digitalmars-d mailing list