The book is right, I just misread a line.
As the answer by @uneven_mark clearly points out, the following question hinges upon a misreading of mine.
While reading The C++ Standard Library (2nd edition) by Josuttis, I got somehow convinced that coll
at page 457 is declared as a std::deque
(on the contrary, it is declared as std::list
!), hence I asked this question.
I hope it can serve as food for thought for the readers.
ORIGINAL QUESTION:
At page 456 of "The C++ Standard Library (2nd edition)", Josuttis remarks that before you call
copy(coll.begin(), coll.end(), back_inserter(coll));
on a coll
of class std::vector
, you must ensure that coll
has enough space (in this case, that it has a capacity
at least twice its size
), otherwise
the algorithm invalidates the passed source iterators while running.
Contrariwise, at page 458, he doesn't say anything similar for the case of
copy(coll.begin(), coll.end(), front_inserter(coll));
as applied to a coll
of class std::deque
, even though, at page 286, the following is specified about the std::deque
container:
[...] when elements are inserted at the front or the back. In this case, references and pointers to elements stay valid, but iterators don’t.
hence my doubt. (Yes, I know std::deque
doesn't even offer a reserve
-like member function.)
As long as I have understood this answer, my understanding is that the front_inserter(coll)
iterator can cause a reallocation of the array of pointers (which is a legal way of implementing std::deque
), and cannot cause reallocation of the arrays in which the actual elements of coll
are stored, thus leaving references/pointers to the elements valid, while invalidating the iterator
s, whose correct behavior (I'm thinking of how operator++
could be implmented) relies on both the array of pointers and the pointed-to arrays.
If this is true, then I guess that the parameter corresponding to copy
's argument coll.begin()
gets invalidated at the moment the assignment to it causes a reallocation of the array of pointers.