使用移动而不是复制需要std :: vector吗? [英] are std::vector required to use move instead of copy?

查看:35
本文介绍了使用移动而不是复制需要std :: vector吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在按标准重新分配增长/交换/擦除操作时,是否需要 std :: vector 使用move而不是copy?还是纯粹是实现优化?

Are std::vector required to use move instead of copy, at reallocation on grow/swap/erase by standard? Or this is purely implementation optimization?

如果 std :: vector 需要以使用move,在什么条件下?这些规则/条件在哪里可以读取?

If std::vector required to use move, at what conditions? 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.

更新

何时何时需要在重新分配时使用move构造函数?在哪里说需要使用移动语义重新分配?

When it required to use move constructor on reallocation? And where it says that it required to reallocate with move semantic?

推荐答案

user2079303的答案是好的,但我总结一下,只是对我自己而言.

标准C ++ 17 完全不需要,它不需要 std :: vector 在重新分配期间移动.如果对象是可复制的,则允许始终使用复制.

Standard C++17 does not require std::vector to move during reallocation, at all. It allowed to always use copy, if object is copyable.

例如,每次需要重新分配其元素时,允许 std :: vector< std :: vector< std :: array< char,10000>>> 进行复制在成长.

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 如何使用可复制+可移动类型.可以是任何东西,因为它可以是任何东西.

Don't put your trust on library implementers, always check how YOUR copyable + movable type work with std::vector in YOUR environment. It could be anything, because it allowed to be anything.

这篇关于使用移动而不是复制需要std :: vector吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆