I am using an external library which provides a function with the following interface:
void foo(const std::vector<int>& data);
I am receiving a very large C-style array from another library which has already been allocated:
int* data = bar();
Is there any way for me to pass on data to foo without allocating and copying each element? data is very large and therefore I want to avoid a copy and allocation if possible.
I could have used allocators, but foo is not templated for an allocator, so I don't believe this is possible.
I understand I may be asking for magic, but if it is possible that would be great. Of course if foo rather took an std::span this would not be a problem.
std:vectorthat wraps yourint* data(then passing that vector) may be a solution to avoid memory copy/moves. This answer shows a way to do that.std::vector<int, my_allocator<int>>is different type? In general that looks like two incompatible library interfaces. C++ one doesn't have legacy interface (pointer + size), the C one doesn't have an interface that allows using pre-allocated storage.