An input range iteration question

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 8 05:52:52 PDT 2015


A struct is copied when you pass it to foreach, so it iterates 
over the copy leaving the original the same.

A class is referenced by foreach, so the original is affected by 
it.


This is one reason why separating containers from the 
ranges/iterators that go over them is helpful: the iterator looks 
at, but doesn't modify the container. When it is done as a 
separate struct, this typically works fairly easily (unless the 
act of iterating the container is destructive, like reading from 
a pipe...)


More information about the Digitalmars-d-learn mailing list