Does std :: move () cancel the iterators?

Consider the following program:

struct list_wrapper { std::vector<int> m_list; }; int main() { std::vector<int> myList { 1, 1, 2, 3, 5 }; const std::vector<int>::iterator iter = myList.begin(); list_wrapper wrappedList; wrappedList.m_list = std::move(myList); // Can I still dereference iter? return 0; } 

After calling std::move(myList) , now does iter point to a valid element inside wrappedList.m_list or wrappedList.m_list moving constructors / assignments invalidate all iterators?

+7
c ++ iterator undefined-behavior c ++ 11 move-semantics
source share
2 answers

After the http://en.cppreference.com notes (my highlight):

After assigning container movement (overload (2)), if only the movement element is assigned by forced incompatible allocators, references, pointers and iterators (except for the final iterator) to others remain valid , but refer to the elements that are now in * this. Electric current standard makes this guarantee by using the blanket application in ยง23.2.1 [container.requirements.general] / 12, and a more direct guarantee is pending LWG 2321

Notes

As hvd rightly pointed out, at least once the case where the designated move is forced to invalidate the iterators is when the new container has an incompatible allocator.

As Ben Voigt said, here is a broader discussion in this section and in fact it already covers aspects of the C ++ 11 issue ...

+4
source share

No, they should not become invalid after the move operation.

23.3.6.5/1

all iterators and links to the insertion point are not affected if the new container size is larger than the previous capacity (in this case, all iterators and links are invalid)

0
source share

All Articles