New linked list

Derek Parnell derek at psych.ward
Wed May 10 04:11:41 PDT 2006


On Fri, 12 May 2006 02:52:10 +1000, Walter Bright  
<newshound at digitalmars.com> wrote:

> Sean Kelly wrote:
>> Very cool.  One thing... does this work:
>>      foreach( Person p; per.each )
>>     {
>>         if( p.age > 50 )
>>             p.listRemove();
>>     }
>>  ie. can you remove elements within a foreach?
>
> It's undefined behavior. foreach is entitled to assume that the  
> aggregate is a loop invariant, although the contents of the aggregate's  
> elements can change. This precludes things like resizing an array inside  
> a foreach, etc.

I fell for this one just yesterday. I had ...

      foreach( Person p; People)
      {
          Foo();
          SomeFunc(); // Potentially adds an item to the People list.
      }

but the new items were never processed by Foo() !

-- 
Derek Parnell
Melbourne, Australia



More information about the Digitalmars-d-announce mailing list