6

What would happen if a container that BOOST_FOREACH is iterating through is changed inside the BOOST_FOREACH scope?

Does BOOST_FOREACH "freeze" the initial state?

Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359

2 Answers2

9

In this case behavior is undefined. Look at Hoisting and Iterator Invalidation in official documentation of BOOST_FOREACH.

Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111
2

I guess it depends on the container.

The real question is, whether the (remaining) iterator range for traversal is invalidated or not.

This depends on the change. std algorithms are specified with their behaviour: they mention what iterators are invalidated when.

Update

It can also depend on the type of iterator,

  • vector::erase invalidates all subsequent iterators
  • list::erase does not
sehe
  • 374,641
  • 47
  • 450
  • 633