gpt4 book ai didi

c++ - 分配器感知容器分配是如何实现的?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:17:51 31 4
gpt4 key购买 nike

例如,来自std::deque::operator = in C++ Reference:
(1) 复制赋值 (const std::deque &other)

Replaces the contents with a copy of the contents of other.
If std::allocator_traits::propagate_on_container_copy_assignment() is true, the target allocator is replaced by a copy of the source allocator. If the target and the source allocators do not compare equal, the target (*this) allocator is used to deallocate the memory, then other's allocator is used to allocate it before copying the elements.

如果 this->get_allocator() == other.get_allocator(),我可以根据需要简单地销毁和释放 this 元素,或者分配和构造元素需要,或者如果需要,将元素从 other 复制分配给 *this
但如果不是呢?上面的引用是否意味着我不能复制分配元素,所以我必须首先销毁和释放所有元素,使用 this->get_allocator(),然后分配和构造元素, 使用 other.get_allocator()?
但如果是这样,我为什么要使用 other.get_allocator() 进行分配?
它不会在以后导致一些运行时错误,因为 this 不会正确释放内存吗?

(2) 移动赋值 (std::deque &&other)

Replaces the contents with those of other using move semantics (i.e. the data in other is moved from other into this container). other is in a valid but unspecified state afterward. If std::allocator_traits::propagate_on_container_move_assignment() is true, the target allocator is replaced by a copy of the source allocator. If it is false and the source and the target allocators do not compare equal, the target cannot take ownership of the source memory and must move-assign each element individually, allocating additional memory using its own allocator as needed. In any case, all element originally present in *this are either destroyed or replaced by elementwise move-assignment.

如果 this->get_allocator() == other.get_allocator(),这是一个简单的任务。
但如果不是,则会出现上述相同的问题,除了在这种情况下使用移动分配。

在这两种情况下,我还有一个问题。
如果元素既不能复制分配也不能移动分配,是否可以销毁它并从其他元素构造?如果是,我应该使用谁的分配器?

最佳答案

POCCA(传播容器复制分配)分配器作为容器复制分配的一部分进行复制分配。同样,当容器的移动被分配时,POCMA 分配器被移动分配。

Does the quote above mean that I can't copy-assign the elements, so I have to destroy and deallocate ALL the elements first, using this->get_allocator(), and then allocate and construct the elements, using other.get_allocator()?

正确。

But if that is the case, why should I use other.get_allocator for the allocation? Won't it cause some runtime error later, as this->get_allocator() won't deallocate the memory properly?

因为赋值传播了分配器:在赋值之后,this->get_allocator()other.get_allocator() 的拷贝,所以它可以安全地释放内存由它分配。

If this->get_allocator() == other.get_allocator(), this is an easy task. But if not, the same questions above follow, except in this case move-assignment is used.

实际上,这是完全不同的。使用 POCMA 分配器进行移动分配很简单:您销毁 *this 中的所有元素,释放内存,并掠夺 other 的内存和分配器。

容器移动分配必须诉诸元素移动分配/构造的唯一情况是当您有一个非 POCMA 分配器并且分配器比较不相等时。在这种情况下,所有分配和构建都是使用 this->get_allocator() 完成的,因为您不会传播任何内容。

In both cases, I have an additional question. If the elements can neither be copy-assigned or move-assigned, is it okay to destroy it and construct from other? If it is, whose allocator should I use?

使用最初构造它的分配器销毁它;使用它将被销毁的分配器构造它。换句话说,如果您正在传播分配器,则使用目标分配器销毁它并使用源分配器构造它。

关于c++ - 分配器感知容器分配是如何实现的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40801678/

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