Does std::list::clear invalidate std::list::end iterator?

蹲街弑〆低调 提交于 2019-12-04 01:29:37

Other answers point out that, in general, you can't rely on a container's past-the-end iterator remaining valid when the container is cleared. However, the past-the-end iterator of a list should indeed remain valid:

C++11 23.3.5.4/3 Effects: Invalidates only the iterators and references to the erased elements.

The past-the-end iterator does not refer to any element, so should not be invalidated.

From C++11, Table 100 (Sequence container requirements):

clear() [...] may invalidate the past-the-end iterator.

And std::list is of course a sequence container template (23.3.5.1/2):

A list satisfies all of the requirements of a container, of a reversible container (given in two tables in 23.2), of a sequence container, including most of the optional sequence container requirements (23.2.3), and of an allocator-aware container (Table 99). The exceptions are the operator[] and at member functions, which are not provided. Descriptions are provided here only for operations on list that are not described in one of these tables or for operations where there is additional semantic information.

This is in fact invalid. Iterators are only valid on the current state container. Once you add or remove items, the iterator is no longer valid.

The article you link does not say what you are doing is valid. They get a new iterator after the clear.

The reason it doesn't show up in release code is because the debugging that picks up the issue is disabled.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!