[英]why vector's move ctor does not deduce a noexcept()?
Why move constructor for std::vector
with custom allocator does not deduce a noexcept()
from allocator's behaviours? 为什么用自定义分配器移动std::vector
构造函数不会从分配器的行为中推导出noexcept()
?
This leads to the class that encapsulates such vector cannot form the (other) vector that can be normally moved in some <algorithm>
s. 这导致封装这种向量的类不能形成可以在某些<algorithm>
正常移动的(其他)向量。 Even if the underlying type meets the nessesary requirements (MoveInsertable and DefaultInsertable). 即使基础类型满足nessesary要求(MoveInsertable和DefaultInsertable)。
I assume that by "move constructor for std::vector
with custom allocator" you mean the allocator-extended move constructor ie this constructor: 我假设通过“使用自定义分配器移动std::vector
构造函数”,你的意思是allocator-extended移动构造函数,即这个构造函数:
vector(vector&& v, const allocator_type& a);
The main reason is that if v.get_allocator() != a
then the constructor must allocate more memory, which could throw bad_alloc
. 主要原因是如果v.get_allocator() != a
那么构造函数必须分配更多的内存,这可能会抛出bad_alloc
。 There is no way to know at compile-time if two allocators of a given type will always compare equal or not (I have reported this as a defect, see LWG 2108 ). 如果给定类型的两个分配器总是比较相等或不同,则无法在编译时知道(我已将此报告为缺陷,请参阅LWG 2108 )。
NB the standard does not require this constructor or the vector(vector&&)
move constructor to be noexcept
. NB标准不要求此构造函数或 vector(vector&&)
移动构造函数为noexcept
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.