Per Johansson
Per Johansson

Reputation: 6887

Are forward_list iterators stable?

I need to implement a list of requests, send them off one at a time (throttled) and wait for the answer (always in order). Thus the operations are:

I just discovered std::forward_list, and am thinking to use it. But for this to work, I need to keep track of one iterator for the sent pointer and one iterator for insertion, and they can't break when I insert and remove objects.

Intuitively I'd say linked list iterators would be stable for insert and remove, but can someone confirm this. Also, do I need to make a special case if I empty the list, where the insertion iterator should reset to before_begin?

Upvotes: 2

Views: 461

Answers (1)

Dietmar Kühl
Dietmar Kühl

Reputation: 153955

The relevant quote from the standard is in 23.3.4.5 [forwardlist.modifiers] paragraph 1 (first sentence):

None of the overloads of insert_after shall affect the validity of iterators and references, and erase_after shall invalidate only iterators and references to the erased elements.

Upvotes: 2

Related Questions