简体   繁体   中英

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.:

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? It seems like there would be no space in the actual class to store the state, if EBCO is used.

Secondly, how does the copying of the allocator take place? Is 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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