Moving data from shared_ptr to vector constructor

  c++, shared-ptr, std, vector

I have a library which contains a function that returns the following:

std::shared_ptr<const uint8_t[]>
and
uint32_t size

I need it to pass it to another API that takes:

std::vector<uint8_t> data

The data contained in the shared_ptr is the exact same data that needs to go into the vector, so a move operation would be ideal.

So my question is this: Is there any way to obtain this without just copying the data? I realize the issue hinges on the fact that shared_ptr will delete the data once it goes out of scope, so I’d like to know if there’s a way to circumvent this.

(And if copy is necessary, what is the most efficient method to do so? I’m thinking std::copy or good’ol’ memcpy.)

Source: Windows Questions C++

LEAVE A COMMENT