简体   繁体   English

为什么vector的移动ctor没有推导出noexcept()?

[英]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.

相关问题 如果没有 noexcept 移动构造函数,为什么带有 std::vector 的代码不能编译,但带有 std::unique_ptr 的代码却可以编译? - Why does code with std::vector not compile but with std::unique_ptr it does, if there is no noexcept move constructor? 为什么 std::map 的移动构造函数不是 noexcept? - Why is std::map's move constructor not noexcept? 为什么std :: vector使用移动构造函数,尽管声明为noexcept(false) - Why does std::vector use the move constructor although declared as noexcept(false) 为什么defaulted-move-ctor禁止隐式copy-ctor而不抑制defaulted-copy-ctor? - Why does defaulted-move-ctor inhibit implicit-copy-ctor but not defaulted-copy-ctor? 为什么移动ctor比复制ctor慢? - Why is move ctor slower than copy ctor? 为什么编译器在调用move之后选择复制ctor - Why does the compiler choose the copy ctor after calling move 为什么在这种情况下不调用 move ctor? - Why move ctor is not called in this case? 为什么当我不移动任何东西时,clang会抱怨删除的移动? - Why does clang complain about a deleted move ctor when I don't move anything? 为什么矢量访问运算符未指定为noexcept? - Why vector access operators are not specified as noexcept? 为什么我默认的移动构造函数不是 noexcept? - Why is my defaulted move constructor not noexcept?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM