Since BOOST_FOREACH is a macro, it must have exactly two
arguments, with exactly one comma separating them. That's not always convenient,
especially when the type of the loop variable is a template. Consider trying
to iterate over a std::map:

Under the covers, BOOST_FOREACH uses iterators to traverse
the element sequence. Before the loop is executed, the end iterator is cached
in a local variable. This is called hoisting, and it is
an important optimization. It assumes, however, that the end iterator of the
sequence is stable. It usually is, but if we modify the sequence by adding
or removing elements while we are iterating over it, we may end up hoisting
ourselves on our own petard.

The call to vect.push_back()
will cause all iterators into vect
to become invalid, including it1
and it2. The next iteration
through the loop will cause the invalid iterators to be used. That's bad news.

The moral of the story is to think twice before adding and removing elements
from the sequence over which you are iterating. If doing so could cause iterators
to become invalid, don't do it. Use a regular for
loop instead.