During the proceedings of this question, it came to light that there appear to be no time complexity requirements placed on std::vector<T>::clear
by the C++ standard.
Table 100 under 23.2.3 says:
Destroys all elements in
a
. Invalidates all references, pointers, and iterators referring to the elements ofa
and may invalidate the past-the-end iterator. post:a.empty()
returnstrue
And... that's it. There's no entry for it specifically under 23.3.6, and no explicit indication that the following applies to clear
:
[C++11: 23.3.6.1/1]:
A vector is a sequence container that supports random access iterators. In addition, it supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficiency. [..]
So... is this really true? Or have I simply missed it?
This seems to be an unintended consequence of DR 704 (and the related DR 1301) which removed the wording saying clear()
was equivalent to erase(begin(), end())
because erase()
requires MoveAssignable, which isn't needed when erasing every element. Removing the definition in terms of erase()
also removes the complexity requirement. That can probably be handled editorially; I've raised it with the committee.
N.B. std::deque::clear()
and std::forward_list::clear()
are also affected. std::list::clear()
does have a complexity guarantee.
Edit: this is now http://cplusplus.github.com/LWG/lwg-active.html#2231
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With