std.collection - changing the collection while iterating

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Sun Jun 21 23:29:25 PDT 2015


On 6/21/15 10:03 PM, Steven Schveighoffer wrote:
> On 6/21/15 10:55 PM, Andrei Alexandrescu wrote:
>> On 6/21/15 7:31 PM, Steven Schveighoffer wrote:
>>> On 6/21/15 7:02 PM, Andrei Alexandrescu wrote:
>>>> While I work on making std.allocator better, here's some food for
>>>> thought regarding std.collection.
>>>>
>>>> Consider a traditional container with reference semantics, Java-style.
>>>> Regarding changing the collection (e.g. adding/removing elements) while
>>>> iterating, we have the following possibilities:
>>>>
>>>> 1. Leave it undefined, like the STL does. Probably this is too extreme.
>>>
>>> I don't think it's undefined, it depends on the container:
>>>
>>> http://www.cplusplus.com/reference/set/set/erase/
>>>
>>> "Iterators, pointers and references referring to elements removed by the
>>> function are invalidated. All other iterators, pointers and references
>>> keep their validity."
>>
>> "Invalidated" = undefined. The point is to make it a hard error when
>> trying to use an invalidated range. -- Andrei
>
> No, that's not why I quoted it.
>
> An iterator remains valid as long as its target hasn't been removed.

The matter is very well understood. My point here is that leaving it to 
the user to make sure which ranges are still valid vs. not is not 
appropriate for D's container. -- Andrei


More information about the Digitalmars-d mailing list