protocol for using InputRanges

Timon Gehr timon.gehr at gmx.ch
Sun Mar 23 04:52:52 PDT 2014


On 03/23/2014 01:50 AM, Walter Bright wrote:
>
> 2. Can r.front be called n times in a row? I.e. is calling front()
> destructive?
>
> If true, this means that r.front will have to cache a copy in many cases.

An analogous problem exists and is more severe for RandomAccessRanges.

import std.algorithm, std.range;
class Obj{ this(int){} }
void main(){
     auto x=[1,2,3].map!(a=>new Obj(a));
     auto a=x.front;
     auto b=x.front;
     auto c=x[0];
     auto d=x[0];
     assert(a is b); // fail
     assert(b is c); // fail
     assert(c is d); // fail
}



More information about the Digitalmars-d mailing list