diff mbox

std::vector::assign should not call std::vector::swap

Message ID 20140925121420.GE2669@redhat.com
State New
Headers show

Commit Message

Jonathan Wakely Sept. 25, 2014, 12:14 p.m. UTC
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.

Comments

Jonathan Wakely Oct. 1, 2014, 12:37 p.m. UTC | #1
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())
> 	{
diff mbox

Patch

commit 6a3d7b12c8879a0431e8aa9ffb521f18063debc6
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Sep 25 13:03:40 2014 +0100

    	* 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())
 	{