[phobos] Would you find a ByRef range useful?

Lars Tandle Kyllingstad lars at kyllingen.net
Mon Jun 6 02:33:43 PDT 2011


Sorry for not following up on this earlier, I got busy with other things
and completely forgot about it.


On Thu, 2011-05-19 at 18:28 -0400, Robert Jacques wrote:
> On Thu, 19 May 2011 18:09:09 -0400, Lars Tandle Kyllingstad  
> <lars at kyllingen.net> wrote:
> 
> > I can't really see any elegant way to implement save() for such a range,
> > though, and without save() it doesn't qualify as a forward range, a
> > bidirectional range or a random-access range -- at least not as defined
> > by isForwardRange & co.  Is there then any point in implementing the
> > remaining primitives?
> >
> > -Lars
> 
> Well, any routine which uses byRef internally, like I do, won't  
> necessarily care about save, but might use indexing/slicing if available.
> 
> Besides, what's wrong with something like:
> 
> static if(/*...*/)
> ByRef save()
> {
> 	auto ptr = new Range;
> 	*ptr = (*_range).save;
> 	return byRef(ptr);
> }

A memory allocation hidden away in a function which is normally very
cheap.  I've somehow gotten the impression that algorithms can use
save() without worrying about its cost.  After all, in most cases it is
simply implemented as "return this" or similar.  If it turns out that
save() makes no such guarantee, I don't see a problem with the
implementation you suggest.

-Lars



More information about the phobos mailing list