何时为 std::deque emplace() 调用移动赋值?
根据emplace()
方法的描述,
如果所需的位置已被现有元素占据,则插入的元素首先在另一个位置构造,然后移动到所需的位置。
NOTE: This is true, only when std::vector reallocation doesn't happen on emplace.
当我移动构造通过 emplacing emplace()示例的另一个元素,我看到emplace()的描述是真实的(当emplace上不进行重新分配时)。即容器中的最后一个元素是移动构造的,静止所有移动分配。
NOTE: create a new object A four{"four"}, then move construct by emplace
https://en.cppreference.com/w/cpp/cpp/container/container/vector/ emplace
对于std :: Deque
在此链接中,O/P有所不同 https:// https:// en.cppreference.com/w/cpp/cppp/container/deque/ emplace
在std :: Deque
中,哪些条件会生成移动分配 o/p,就像std :: vector
中一样在emplace()?
As per the description of emplace()
method in both std::vector and std::deque -
if the required location has been occupied by an existing element, the inserted element is constructed at another location at first, and then move assigned into the required location.
NOTE: This is true, only when std::vector reallocation doesn't happen on emplace.
When I move construct by emplacing one more element to the vector emplace() example, I see the description of emplace() stands true (when reallocation doesn't happen on emplace). i.e. The last element in the container is move constructed, rest all are move assigned.
NOTE: create a new object A four{"four"}, then move construct by emplace
https://en.cppreference.com/w/cpp/container/vector/emplace
Understandbly the o/p is different for std::deque
in this link
https://en.cppreference.com/w/cpp/container/deque/emplace
What conditions would generate the move assignment o/p in std::deque
, as it does in std::vector
on emplace()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
需要明确的是,我认为这里的行为不能由标准保证。这只是实施的实际行为。
您也许可以通过将Deque IE的中间放置在不是Deque末端的地方来复制任务。在您的简短示例中,您正在将其置于前端,在这种情况下,恰好有可能在无需额外分配的情况下构建该元素。
To be clear, I don't think that the behaviour here is is guaranteed by the standard. It's just a practical behaviour that arises from the implementation.
You may be able to reproduce the assignment by emplacing to the middle of the deque i.e. somewhere that isn't either of the ends of the deque. In your short example, you are emplacing to the front end, in which case it happens to be possible to construct the element in-place, without needing an extra assignment.