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: Anthony Shipman <als at iinet dot net dot au>
- Cc: "libstdc++" <libstdc++ at gcc dot gnu dot org>
- Date: Fri, 9 Jan 2015 00:41:18 +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> <201501090219 dot 30548 dot als at iinet dot net dot au> <CAH6eHdRkpHMn7onGc8LS2HzL9siW_w17uyf-tOQ3uqk=DenKug at mail dot gmail dot com> <201501090427 dot 31733 dot als at iinet dot net dot au>
On 8 January 2015 at 17:27, Anthony Shipman <als@iinet.net.au> wrote:
> On Fri, 9 Jan 2015 02:52:42 am Jonathan Wakely wrote:
>> >> 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.
>> >
>> > Why would this happen? Is there some sort of implied operator=(&&) for
>> > the int* type?
>>
>> I don't understand the question. Why would what happen?
>>
> That the vector loses ownership of its data. When the vector is destroyed it
> would release the memory with the data. What is the move method that would
> take away ownership of the data if data() returned T*&& to some_function()?
Read the rest of the thread you're replying to. In
https://gcc.gnu.org/ml/libstdc++/2015-01/msg00032.html it was
suggested to make calling data() on an rvalue vector release ownership
of the memory it manages. My example was showing how that causes
currently valid code to start leaking memory.