DMD Bug or not? foreach over struct range

Jonathan M Davis jmdavisProg at gmx.com
Wed May 16 00:38:15 PDT 2012


On Wednesday, May 16, 2012 03:29:23 Nick Sabalausky wrote:
> "Jonathan M Davis" <jmdavisProg at gmx.com> wrote in message
> news:mailman.826.1337151940.24740.digitalmars-d-learn at puremagic.com...
> 
> > No. That's expected. Your range is a value type, so it got copied when you
> > used it with foreach.
> 
> But foreach isn't a function, it's a flow-control statement.

If it _wasn't_ copied, using foreach would consume your range. It doesn't, and 
it would really suck if it did. But

foreach(e; range) {}

pretty has to be translated to something similar to

for(auto r = range; !r.empty(); r.popFront())
{
    auto e = r.front;
}

And actually, looking at TDPL (p. 381), that's pretty much _exactly_ what it 
gets translated to (save for the exact variable names used).

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list