Moving elements between two std::vectors which are using different allocators

74 views Asked by At

I have an object of type std::vector<a_trivially_copiable_type, my_custom_allocator>. At some point, I need to inject this into a 3rd-party-library which accepts only a std::vector<a_trivially_copiable_type>. For my use case it is fine that from that point on my_custom_allocator won't be used anymore. What is not fine, is that the std::vector content is at any moment duplicated in the process.

So I tried to do this:

auto tmp = std::vector<a_trivially_copiable_type>{};
tmp = std::move(the_vector_with_the_custom_allocator);
auto library_obj = ThirdPartyLibraryObject{std::move(tmp)};

But this does not work since the STL requires the two std::vector to have exactly the same type. Also, std::move in <algorithm> does not help since it will only move the objects of type a_trivially_copiable_type, which will result in nothing better than a std::memcpy since they are trivially copiable.

Is there a way to achieve this?

0

There are 0 answers