[patch] std::vector::assign should not call std::vector::swap

Jonathan Wakely jwakely@redhat.com
Wed Oct 1 12:37:00 GMT 2014


On 25/09/14 13:14 +0100, Jonathan Wakely wrote:
>With C++11 allocator semantics the swap() member might also replace
>the allocator, which is only allowed in specific circumstances.
>
>Even though the worst that could happen is we replace the allocator
>with an equal one, we should avoid using swap and use the internal
>_M_swap_data function instead.
>
>I'm not adding a test, as I don't think this is likely to regress and
>to do it properly we'd need to test every member function and ensure
>only the correct ones replace the allocator. I searched, and we don't
>use swap() inappropriately elsewhere in std::vector.
>
>Tested x86_64-linux, committed to trunk.

... and the 4.9 branch.

>    	* include/bits/vector.tcc (vector::_M_fill_assign): Use _M_swap_data.
>
>diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc
>index 5c3dfae..4eacec3 100644
>--- a/libstdc++-v3/include/bits/vector.tcc
>+++ b/libstdc++-v3/include/bits/vector.tcc
>@@ -228,7 +228,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
>       if (__n > capacity())
> 	{
> 	  vector __tmp(__n, __val, _M_get_Tp_allocator());
>-	  __tmp.swap(*this);
>+	  __tmp._M_impl._M_swap_data(this->_M_impl);
> 	}
>       else if (__n > size())
> 	{



More information about the Libstdc++ mailing list