I have the following case:
T* get_somthing(){
std::vector<T> vec; //T is trivally-copyable
//fill vec
T* temp = new T[vec.size()];
memcpy(temp, vec.data(), vec.size() * sizeof(T));
return temp;
}
I want to get rid of the copy process by returning the std::vector::data
directly like this:
T* get_somthing(){
std::vector<T> vec; //T is trivally-copyable
//fill vec
return temp.data();
}
However, that is wrong since the data is going to be deleted when vec
destructor is called.
So, how can I prevent vec from delete its data? In other word I want some kind of move-idiiom from std::vector
to C++ Raw Dynamic Array.
P.S. Changing the design is not an option. Using the std::vector
there is mandatory. Returning a pointer
to array
is also mandatory. Becauese It is a wrapper between two modules. One need vector the other need pointer.