[C++-sig] Re: Support for Indexing and Iteration Safety

David Abrahams dave at boost-consulting.com
Fri May 30 00:00:30 CEST 2003

"Mike Rovner" <mike at bindkey.com> writes:

>>            >>> x = foo[10].bar
>>            >>> del foo[10]  # erase the 10th item
>>            >>> print x
> What about having x a weak reference which del will reset to None.

I considered that, but is that really Pythonic and unsurprising?
Anyway, it's not much easier to make work than the one which copies
the referenced object when its position is invalidated.

>>            >>> x = foo[10]
>>            >>> foo[10] = y
>>            >>> print x, y
>>         Now you'll see that x and y are always identical, because x
>>         refers to the place in the array where you've written the
>>         value of y.
> It's ok, because Python have explicit copy semantics
> The list isn't the only model.

Huh?  How does "explicit copy semantics" change anything?
What does this have to do with "the list"?

>>      4. Similar problem with exposed iterators:
>>          change out from under you.  The above could lead to a crash
>>          or bogus results.
> bogus results are ok because it's the same as in Python - changing a list
> under loop can lead to skiping an element or visiting it twice.

Yes, but it will never crash.

>> know how many people care.
> I think if it will put some restriction on usage it is ok with me.

Do you mean to say that it's OK to expose the user to crashes as long
as we tell him what he's allowed to do, and only crash if he disobeys?

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list