From 99d2df956d4e7a643f2884b057178139e3a5da3c Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Fri, 8 Aug 2014 15:58:00 +0000 Subject: [PATCH] =?UTF-8?q?Apply=20a=20similar=20fix=20to=20=20as=20I=20did=20for=20=20in=20r215210.=20Again,=20tha?= =?UTF-8?q?nks=20to=20Ion=20Gazta=C3=B1aga=20for=20noticing=20this=20probl?= =?UTF-8?q?em=20w.r.t=20LWG#526?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit llvm-svn: 215213 --- libcxx/include/forward_list | 3 +- .../forwardlist.ops/remove.pass.cpp | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/libcxx/include/forward_list b/libcxx/include/forward_list index 72d31dc7e074..a83b19520e28 100644 --- a/libcxx/include/forward_list +++ b/libcxx/include/forward_list @@ -1421,6 +1421,7 @@ template void forward_list<_Tp, _Alloc>::remove(const value_type& __v) { + forward_list<_Tp, _Alloc> __deleted_nodes; // collect the nodes we're removing iterator __e = end(); for (iterator __i = before_begin(); __i.__ptr_->__next_ != nullptr;) { @@ -1429,7 +1430,7 @@ forward_list<_Tp, _Alloc>::remove(const value_type& __v) iterator __j = _VSTD::next(__i, 2); for (; __j != __e && *__j == __v; ++__j) ; - erase_after(__i, __j); + __deleted_nodes.splice_after(__deleted_nodes.before_begin(), *this, __i, __j); if (__j == __e) break; __i = __j; diff --git a/libcxx/test/containers/sequences/forwardlist/forwardlist.ops/remove.pass.cpp b/libcxx/test/containers/sequences/forwardlist/forwardlist.ops/remove.pass.cpp index a15e786cd283..ae381e65bd28 100644 --- a/libcxx/test/containers/sequences/forwardlist/forwardlist.ops/remove.pass.cpp +++ b/libcxx/test/containers/sequences/forwardlist/forwardlist.ops/remove.pass.cpp @@ -17,6 +17,17 @@ #include "min_allocator.h" +struct S { + S(int i) : i_(new int(i)) {} + S(const S &rhs) : i_(new int(*rhs.i_)) {} + S& operator = (const S &rhs) { *i_ = *rhs.i_; return *this; } + ~S () { delete i_; i_ = NULL; } + bool operator == (const S &rhs) const { return *i_ == *rhs.i_; } + int get () const { return *i_; } + int *i_; + }; + + int main() { { @@ -66,6 +77,32 @@ int main() c1.remove(0); assert(c1 == c2); } + { // LWG issue #526 + typedef int T; + typedef std::forward_list C; + int t1[] = {1, 2, 1, 3, 5, 8, 11}; + int t2[] = { 2, 3, 5, 8, 11}; + C c1(std::begin(t1), std::end(t1)); + C c2(std::begin(t2), std::end(t2)); + c1.remove(c1.front()); + assert(c1 == c2); + } + { + typedef S T; + typedef std::forward_list C; + int t1[] = {1, 2, 1, 3, 5, 8, 11, 1}; + int t2[] = { 2, 3, 5, 8, 11 }; + C c; + for(int *ip = std::end(t1); ip != std::begin(t1);) + c.push_front(S(*--ip)); + c.remove(c.front()); + C::const_iterator it = c.begin(); + for(int *ip = std::begin(t2); ip != std::end(t2); ++ip, ++it) { + assert ( it != c.end()); + assert ( *ip == it->get()); + } + assert ( it == c.end ()); + } #if __cplusplus >= 201103L { typedef int T;