The concept of Fail-Fast iterators

Good time of day.

If I understand correctly, Fail-Fast iterators immediately stop working (usually by generating exceptions) if they find that the data set they are iterating over has been changed after the iteration has started (or since the iterator was created).

Question: why in the case of iterators, the approach "I can read until no one has changed anything" is preferred to the approach " I can only make changes until nobody reads anything"?

Author: Bakuard, 2020-03-30

1 answers

This is done for one simple reason: it's cheaper this way. Otherwise, you'd have to keep track of all the live iterators - and that's expensive. Especially in old C++ without rvalue references or in languages with garbage collection.

 1
Author: Pavel Mayorov, 2020-03-30 07:02:15