to invalidate a range

Ellery Newcomer ellery-newcomer at utulsa.edu
Fri Aug 12 15:29:00 PDT 2011


On 08/12/2011 03:54 PM, Jonathan M Davis wrote:
>
> In the case of container that uses nodes - such as a linked list - because you
> can add and remove elements without affecting other elements, iterators and
> ranges don't tend to get invalidated as easily. As long as you don't remove
> the element (or elements in the case of a range - assuming that it keeps track
> of its two end points, as is likely) that it points to, then adding or
> removing elements from the container shouldn't invalidate the iterator/range.

"shouldn't" isn't a guarantee. Where there is "shouldn't", there can't 
be stableRemove*, no?

>
> So, basically what it comes down to is the short answer. A range which has
> been invalidated doesn't point to what it's supposed to point to anymore, and
> using it results in undefined behavior. It's less likely to blow up in D,
> because it's generally memory-safe, but you're going to get incorrect
> behavior.
>
> - Jonathan M Davis

suppose your linked list range points to a node X. element in X is 
removed by the linked list, and the range automagically moves to X.next 
(or X.prev). Is the range invalid by this standard or not? (no way 'san 
ifrinn I'm going to implement that, though).


heh heh. most of this business has only convinced me I want immutable 
containers.


More information about the Digitalmars-d-learn mailing list