std::vector::resize(size_type) 需要 CopyInsertable?

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

这个问题是在我回答的时候提出的。

N3337 23.3.6.3 "vector capacity" 说(在第 770 页):

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.

然而,clang++ says it's okay though T is not copyable. 而且我认为 resize(size_type) 只需要 destroyable/moveable/default 可构建是有道理的。 destroys if sz <= size, appends(默认构造,容量不够就销毁移动)if size() < sz.

什么是真理?是标准缺陷吗?还是我和clang++都错了?

你是对的。 http://cplusplus.github.io/LWG/lwg-defects.html#2033

为 C++14 修复了 C++11 中的一个缺陷

当前的措辞是:

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.

Destructible 的要求在 Table 95 中,适用于所有容器的所有操作,而不仅仅是 resize()