何时为 std::deque emplace() 调用移动赋值?

发布于 2025-01-20 11:13:41 字数 1055 浏览 4 评论 0原文

根据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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄与心 2025-01-27 11:13:41

需要明确的是,我认为这里的行为不能由标准保证。这只是实施的实际行为。

您也许可以通过将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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文