This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [Patch]New feature to all kind of stl container.vector::data_move() returns vector::data() that can be moved,just as unique_ptr::release() to unique_ptr::get()
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: éååå <805600352 at qq dot com>
- Cc: "libstdc++" <libstdc++ at gcc dot gnu dot org>, "marc.glisse" <marc dot glisse at inria dot fr>
- Date: Thu, 8 Jan 2015 13:36:05 +0000
- Subject: Re: [Patch]New feature to all kind of stl container.vector::data_move() returns vector::data() that can be moved,just as unique_ptr::release() to unique_ptr::get()
- Authentication-results: sourceware.org; auth=none
- References: <tencent_1737FA275BA843B20700F485 at qq dot com> <alpine dot DEB dot 2 dot 11 dot 1501080815100 dot 1564 at laptop-mg dot saclay dot inria dot fr> <CAH6eHdRJ7XGw1m6ntK+sBtyM70yvvqUmz-LxQdPrUH8bT4SY7g at mail dot gmail dot com> <alpine dot DEB dot 2 dot 11 dot 1501081246410 dot 31658 at stedding dot saclay dot inria dot fr> <tencent_215CF4C848ACCCEF53C7F318 at qq dot com>
On 8 January 2015 at 13:31, éååå <805600352@qq.com> wrote:
> Same as my original idea.
> my vector<T>::data_move() can be exactly in this function form
> T* data()&&;
That is a poor interface, which can cause currently working code to leak memory.
extern "C" void some_function(const int*, size_t);
std::vector<int> get_values(size_t n);
...
some_function(get_values(5).data(), 5);
Today this works correctly. With your suggestion the call to data()
would release ownership of the memory and it would never be freed.
> And thanks to Marc Glisse for works on std::string.
> Since it's a real nead,anyone consider my idea acceptable to libstdc++?
Definitely not in your suggested form, no.
The function must return some RAII type, which would probably make it
incompatible with existing uses of data(), so it should have a
different name.