std::vector::resize(size_type) requires CopyInsertable?

215 Views Asked by At

This question is made while I answer this another question.

N3337 23.3.6.3 "vector capacity" says (it's in 770 page):

void resize(size_type sz);

Effects: If sz <= size(), equivalent to erase(begin() + sz, end());. If size() < sz, appends sz - size() value-initialized elements to the sequence.

Requires: T shall be CopyInsertable into *this.

However, clang++ says it's okay though T is not copyable. And I think it makes sense that resize(size_type) requires only destroyable/moveable/default constructable. It destroys if sz <= size, appends (which uses default constructing, and destroying and moving if the capacity is not enough) if size() < sz.

What is truth? Is it a standard defect? Or is it a mistake of both clang++ and me?

1

There are 1 best solutions below

3
On BEST ANSWER

You are correct. It was a defect in C++11 that was fixed for C++14 by http://cplusplus.github.io/LWG/lwg-defects.html#2033

The current wording says:

Effects: If sz < size(), erases the last size() - sz elements from the sequence. Otherwise, appends sz - size() default-inserted elements to the sequence.

Requires: T shall be MoveInsertable and DefaultInsertable into *this.

The requirement on Destructible is in Table 95 and applies to all operations on all containers, not just resize().