gpt4 book ai didi

c++ - 在容器 : state of previously contained objects 上 move 分配

转载 作者:行者123 更新时间:2023-11-28 04:31:46 24 4
gpt4 key购买 nike

来自another question (实际上,而不是 this one ,但前者是更好的引用),除了 20.5.5.15 之外,我在标准中找不到合适的引用:

Objects of types defined in the C++ standard library may be moved from (15.8). Move operations may be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state.

是否对目标容器中先前包含的元素有任何要求,例如。 G。在分配前被销毁?

例子:

std::list<SomeClass> v1({10, 12});
std::list<SomeClass> v2({7});
v1 = std::move(v2);
for(auto sc : v2)
{
std::cout << sc << ' ';
}

虽然 GCC 根本没有输出任何东西(std::vectorstd::list 类似),但会收到 10 12作为输出合法(适当的 operator<< 提供)(例如,通过交换内容接收,特别是删除以前包含的对象)?

到现在为止,我会说"is",但还不够确定可以依赖并且太好奇而不是发布问题...

如果合法,如果元素没有被立即销毁(例如,结果是一些资源仍然保持打开状态,而开发人员希望它们被关闭),对于任何开发人员来说是否会出乎意料?

最佳答案

[container.requirements.general] ,我们看到

All existing elements of a are either move assigned to or destroyed. Ensures: a shall be equal to the value that rv had before this assignment.

a 是目的地,rv 是右值。这可以通过将1 与源目的地的元素交换来实现,但最有可能通过调整大小然后 move 来完成。

  1. 它必须通过非特殊化的 __swap 来确保 move 分配确实发生。

关于c++ - 在容器 : state of previously contained objects 上 move 分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52698679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com