Unmanaged drop in replacemet for [] and length -= 1

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 19 12:27:42 PDT 2016


On Sunday, June 19, 2016 15:59:41 Joerg Joergonson via Digitalmars-d-learn 
wrote:
> If foreach removes all/any of the elements of a container then
> something is broke.

That's exactly what happens with a basic input range, and if it doesn't
happen with a forward range, it's just because copying that range implicitly
saves it, which doesn't work in generic code, because it's not the case with
all forward ranges. popFront consumes an element from a range, and foreach
is popping every element in the range. The same goes for any algorithm which
iterates over a range. So, naturally, any container that is treated as a
range is going to have its elements removed as it's iterated over.

You're certainly free to treat a container as a range if that's what you
want to do, but it shows a fundamental misunderstanding of what a range is
supposed to be and do, and there are plenty of algorithms that will behave
very badly for you if you do that.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list