简体   繁体   English

是std :: vector需要使用move而不是copy吗?

[英]are std::vector required to use move instead of copy?

Are std::vector required to use move instead of copy, at reallocation on grow/swap/erase by standard? std::vector是否需要使用move而不是copy,在标准的grow / swap / erase重新分配时? Or this is purely implementation optimization? 或者这是纯粹的实施优化?

If std::vector required to use move, at what conditions? 如果std::vector 需要使用move,在什么条件下? Where these rules/conditions can be read? 哪些规则/条件可以阅读?

I found this How to enforce move semantics when a vector grows? 我发现这个如何在向量增长时强制执行移动语义? . But there are no answers about guarantees. 但是没有关于担保的答案。

UPDATE UPDATE

When it required to use move constructor on reallocation? 什么时候需要在重新分配时使用移动构造函数? And where it says that it required to reallocate with move semantic? 它说它需要重新分配移动语义?

Are std::vector required to use move instead of copy, by standard? std::vector是否需要使用move而不是copy,按标准?

I presume that you refer to move and copy of the elements. 我认为你指的是元素的移动和复制。

Neither copy, nor move is necessarily required to be used at all, if you don't use the member functions that do those things. 如果您不使用执行这些操作的成员函数,则根本不需要使用复制或移动。 Some member functions are required to copy elements and other member functions are required to move elements. 复制元素需要一些成员函数,移动元素需要其他成员函数。

When it required to use move constructor on reallocation 当需要在重新分配时使用移动构造函数

It is required to use move if the element is not copy insertable (this requires no rule, you can't do what you can't do). 如果元素不是可复制插入的,则需要使用move(这不需要规则,你不能做你不能做的事情)。 Otherwise it is required to copy if the move constructor can throw (this is required by strong exception guarantee quoted below). 否则,如果移动构造函数可以抛出则需要复制(这是下面引用的强异常保证所要求的)。 If non-throw of move can be proven, then it seems to be up to implementation, as I don't find any further guarantees. 如果可以证明非投掷移动,那么它似乎取决于实施,因为我没有找到任何进一步的保证。 A good implementation would move when it is noexcept . 一个好的实现将在noexcept时移动。

The exception guarantee on relevant functions: 相关功能的例外保证:

If an exception is thrown other than by the move constructor of a non-CopyInsertable T there are no effects. 如果除非由非CopyInsertable T的移动构造函数抛出异常,则不会产生任何影响。

Answer of user2079303 is good, but I summarize, just for myself. user2079303的答案很好,但我总结一下,仅供我自己使用。

Standard C++17 does not require std::vector to move during reallocation, at all. 标准C ++ 17 根本不需要std::vector在重新分配期间移动。 It allowed to always use copy, if object is copyable. 如果对象是可复制的,它允许始终使用副本。

For example, std::vector<std::vector<std::array<char, 10000>>> is allowed to copy each time, when it need to reallocate its elements on grow. 例如, std::vector<std::vector<std::array<char, 10000>>>每次都需要复制,当需要在grow上重新分配其元素时。

Don't put your trust on library implementers, always check how YOUR copyable + movable type work with std::vector in YOUR environment. 不要相信库实现者,总是检查你的可复制+可移动类型如何在你的环境中使用std::vector It could be anything, because it allowed to be anything. 它可以是任何东西,因为它允许任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM