[PATCH] Optimize inserting value_type into std::vector

Jonathan Wakely jwakely@redhat.com
Mon Jun 20 07:42:00 GMT 2016


On 19/06/16 10:21 +0200, François Dumont wrote:
>On 16/06/2016 22:21, Jonathan Wakely wrote:
>>On 16/06/16 21:27 +0200, François Dumont wrote:
>>>Very nice improvement. Could also benefit to other containers, 
>>>especially std::deque. Do you plan to take care of it ?
>>
>>Good point - I'd only looked at it for std::vector, because that's
>>what Howard's experiment tested. I haven't looked at the other
>>containers at all, and wasn't planning to do so. If you have time to
>>look at them that would be great, otherwise I'll add it to my TODO
>>list for something to look at later.
>>
>>
>I started considering it and so came to the question of insert/emplace 
>of the container self values. Is the following program ill-formed ?
>
>int main()
>{
>  std::vector<std::vector<int>> vv =
>    {
>      { 2, 3 },
>      { 4, 5 },
>      { 0, 1 }
>    };
>
>  vv.reserve(4);
>  vv.emplace(vv.begin(), vv[0]);
>
>  assert( vv[0].size() == 2 );
>}
>
>The assert fails because we end-up assigning a moved vector instance 
>to vv first entry. This is not a regression of this patch, we were 
>already not creating any copy before moving all vector values. If this 
>program is ill-formed why does libc++ consider this kind of situation 
>in its insert implementation ?

I think this should work correctly, for both insert and emplace.

>Note that it gave me the idear of adding a DEBUG check detecting when 
>a moved instance is being used. A moved instance shall only be 
>destroyed or assigned, no ?

That's not true in general, but is true for these members of vector.




More information about the Libstdc++ mailing list