More range woes: std.array.save is invalid

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Dec 19 23:36:18 PST 2012


On 12/20/12 2:03 AM, H. S. Teoh wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=8061
>
> After being sidetracked by the stack-allocated buffer fiasco, I finally
> found the root problem in the above bug.
>
> The problem is std.array.save:
>
> @property T[] save(T)(T[] a) @safe pure nothrow
> {
>      return a;
> }
>
> This implementation is only correct for certain values of T. It is wrong
> when T is a forward range, for example, because if you save a T[] and
> iterate over its elements, it will consume the T's in the array, so that
> the original range has elements that are now consumed.
>
> The correct implementation when T is a forward range is to return a copy
> of the array where the elements are obtained by calling T.save.

I think you're overthinking this. Save saves the position in the current 
range without any promise about its contents.

Andrei



More information about the Digitalmars-d mailing list