【发布时间】:2019-07-09 06:23:36
【问题描述】:
将这些特征放在容器中的原因是什么 (https://en.cppreference.com/w/cpp/memory/allocator_traits)
propagate_on_container_copy_assignment Alloc::propagate_on_container_copy_assignment if present, otherwise std::false_type
propagate_on_container_move_assignment Alloc::propagate_on_container_move_assignment if present, otherwise std::false_type
propagate_on_container_swap Alloc::propagate_on_container_swap if present, otherwise std::false_type
is_always_equal(since C++17) Alloc::is_always_equal if present, otherwise std::is_empty<Alloc>::type
我了解容器实现在分配和交换的实现中会以一种或另一种方式表现。 (并且处理这些情况是可怕的代码。)
我也明白,有时可能需要将移动容器保持在resizeble 的状态,或者至少可以调用最后一次释放,因此分配器不能无效。 (我个人认为这是一个弱论点。)
但问题是,为什么这些信息不能成为自定义分配器类型本身的正常实现和语义的一部分?
我的意思是,容器复制分配可以尝试复制分配源分配器,如果该语法复制分配没有真正复制,那么,这就像说你的容器没有 em>propagate_on_container_copy_assignment.
以同样的方式,而不是使用is_always_equal,实际上可以使分配器分配什么也不做。
(此外,如果is_always_equal 为真,则可以使分配器的operator== 返回std::true_type 以发出信号。)
在我看来,这些特征似乎试图覆盖人们可以通过普通 C++ 方式赋予自定义分配器的语义。 这似乎与泛型编程和当前的 C++ 哲学背道而驰。
唯一的原因,我认为这对于实现与“旧”容器的某种向后兼容性很有用。
如果我今天要编写一个 new 容器和/或 new 非平凡的分配器,我是否可以依赖分配器的语义而忘记关于这些特征?
在我看来,只要移出的分配器可以“释放”空指针状态(这意味着在这种特殊情况下几乎什么都不做),那么它应该没问题,如果 resize 抛出,那也很好(有效),它只是意味着分配器不再有权访问它的堆。
编辑:实际上,我可以这样简单地编写容器吗?并将复杂性委托给自定义分配器的语义?:
templata<class Allocator>
struct my_container{
Allocator alloc_;
...
my_container& operator=(my_container const& other){
alloc_ = other.alloc_; // if allocator is_always_equal equal this is ok, if allocator shouldn't propagate on copy, Alloc::operator=(Alloc const&) simply shouldn't do anything in the first place
... handle copy...
return *this;
}
my_container& operator=(my_container&& other){
alloc_ = std::move(other.alloc_); // if allocator shouldn't propagate on move then Alloc::operator=(Alloc&&) simply shouldn't do anything.
... handle move...
return *this;
}
void swap(my_container& other){
using std::swap;
swap(alloc, other.alloc); //again, we assume that this does the correct thing (including not actually swapping anything if that is the desired criteria. (that would be the case equivalent to `propagate_on_container_swap==std::false_type`)
... handle swap...
}
}
我认为对分配器的唯一真正要求是,移出的分配器应该能够做到这一点。
my_allocator a2(std::move(a1));
a1.deallocate(nullptr, 0); // should ok, so moved-from container is destructed (without exception)
a1.allocate(n); // well defined behavior, (including possibly throwing bad_alloc).
而且,如果移动的容器由于移动的分配器失去对堆的访问权而无法调整大小(例如,因为没有特定资源的默认分配器),那么,太糟糕了,那么操作将抛出 (因为任何调整大小都可能抛出)。
【问题讨论】:
-
你有一个类的集合,比如 MyFoo、ThisFoo、ThatOtherFoo,每个类都实现了一种 Foo。每个类的对象的行为都被编码在类中。 作为一个整体的类的属性被编码在相关的特征类中,这是 FooTraits 模板的一个特化。您可以在整个标准库中看到这一点。
-
"这似乎违背了泛型编程和当前的 C++ 哲学。" 以何种方式? Traits 类在整个 C++ 中使用,并且在 C++98 出现之前就已经使用。
-
@NicolBolas,是的,但是特征通常不会告诉其他类如何复制元素以及复制或移动“真正”的含义。 “复制复制”给使用分配器的所有案例带来了很大的负担,因为它必须考虑所有案例。 (我不反对特质,我反对特质,这些特质充其量与语义是多余的,最坏的情况是与 C++ 可以允许您指定的语义相矛盾)。我认为这一定是一个历史故障。
-
@alfC: ""Propagate on copy" 给使用分配器的所有情况带来了很大的负担,因为它必须考虑所有情况。" 它只会让容器实现的负担,它是可能需要复制分配器的代码子集。即便如此,它也只适用于副本assignment。
-
@NicolBolas,是的,仅适用于我的意思的容器类(我碰巧正在实现)。它也适用于移动和交换。第二个问题,如果我忽略这些特征,我的容器会与近期预期的标准分配器一起使用吗? (参见最后一部分的代码,其中我假设分配器具有正常语义,例如它们本身是指针(句柄)到堆youtube.com/watch?v=0MdSJsCTRkY)
标签: c++11 move-semantics allocator copy-assignment move-assignment-operator