Question

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:

  • Insert (at end)
  • Remove (at start)
  • Step forward (the "sent" pointer)

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top