[PATCH] Optimize inserting value_type into std::vector

François Dumont frs.dumont@gmail.com
Sun Jun 19 08:21:00 GMT 2016


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 ?

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 ? Maybe only in pedantic mode ?

François



More information about the Libstdc++ mailing list