I have the following case:
T* get_somthing(){
std::vector vec; //T is trivally-copyable
//fill vec
T* temp = new T[vec.size()];
memc
Ok, don't try this at home, it's not nice. It's more an experiment than an actual answer.
(Well, the requirements/design is not nice either: "Play stupid games, win stupid prizes")
in your cpp:
#define private public // good luck for the code review
#define protected public
#include // (must be the first occurence in the TU)
#undef private // do not abuse good things...
#undef protected
template
T* my_release(std::vector& v){
std::vector x; // x: the local vector with which we mess around
std::swap(x, v); // the given vector is in an OK, empty state now.
T* out = x._M_impl._M_start; // first, get the pointer you want
// x will be destructed at the next '}'.
// The dtr only use _M_start and _M_finish, make sure it won't do anything.
x._M_impl._M_start = nullptr;
x._M_impl._M_finish = nullptr;
// no need to say, the internal state of 'x' is bad, like really bad...
// also we loose the capacity information, the actual allocator...
// -> good luck with memory leaks...
return out;
}
// usage example
int main(){
std::vector vi{1,2,3,4,5,6,7,8,9};
auto n = vi.size();
int* pi = release(vi);
for(size_t i=0; i
prints 1, 2, 3, 4, 5, 6, 7, 8, 9,