简体   繁体   English

通过 EBCO 继承分配器时复制 C++ 个分配器

[英]Copying C++ allocators when allocator is inherited via EBCO

If a container obtains it's allocator's functions via EBCO ( empty base class optimisation ) eg.:如果容器通过 EBCO(空基 class 优化)获得它的分配器函数,例如:

template <class T, class allocator = std::allocator<T>>
class a_container : private allocator;

What happens when that container has another container copied into it, and the other container's allocator is not equivalent (or empty)?当该容器将另一个容器复制到其中,而另一个容器的分配器不等效(或为空)时会发生什么? If the new allocator has state, how is that state copied into the destination container, if the destination container's allocator was stateless?如果新分配器有 state,如果目标容器的分配器是无状态的,那么 state 是如何复制到目标容器中的? It seems like there would be no space in the actual class to store the state, if EBCO is used.如果使用 EBCO,实际的 class 似乎没有空间来存储 state。

Secondly, how does the copying of the allocator take place?其次,分配器的复制是如何进行的? Is static_cast<allocator &>(*this) = source.get_allocator();static_cast<allocator &>(*this) = source.get_allocator(); reasonable?合理的?

What happens when that container has another container copied into it, and the other container's allocator is not equivalent?当该容器将另一个容器复制到其中,而另一个容器的分配器不等效时会发生什么?

A compile time error.编译时错误。 std::vector<T, std::allocator<T>> is not assignable to std::vector<T, some_other_allocator<T>> , they are different classes. std::vector<T, std::allocator<T>>不可分配给std::vector<T, some_other_allocator<T>> ,它们是不同的类。

Secondly, how does the copying of the allocator take place?其次,分配器的复制是如何进行的?

Same as any other subobject that is trivially copyable.与任何其他可简单复制的子对象相同。 The 0 bytes of object representation are copied. object表示的0字节被复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM