diff --git a/include/list b/include/list index 1d21fd6f..c36786dd 100644 --- a/include/list +++ b/include/list @@ -219,7 +219,7 @@ struct __list_node_base _LIBCPP_INLINE_VISIBILITY pointer __self() { - return static_cast(pointer_traits<__base_pointer>::pointer_to(*this)); + return static_cast(pointer_traits<__base_pointer>::pointer_to(*this)); } }; @@ -1086,10 +1086,10 @@ inline _LIBCPP_INLINE_VISIBILITY void list<_Tp, _Alloc>::__link_nodes_at_front(__node_pointer __f, __node_pointer __l) { - __f->__prev_ = base::__end_.__self(); - __l->__next_ = base::__end_.__next_; - __l->__next_->__prev_ = __l; - base::__end_.__next_ = __f; + __f->__prev_ = base::__end_.__self(); + __l->__next_ = base::__end_.__next_; + __l->__next_->__prev_ = __l; + base::__end_.__next_ = __f; } // Link in nodes [__f, __l] at the front of the list @@ -1098,10 +1098,10 @@ inline _LIBCPP_INLINE_VISIBILITY void list<_Tp, _Alloc>::__link_nodes_at_back(__node_pointer __f, __node_pointer __l) { - __l->__next_ = base::__end_.__self(); - __f->__prev_ = base::__end_.__prev_; - __f->__prev_->__next_ = __f; - base::__end_.__prev_ = __l; + __l->__next_ = base::__end_.__self(); + __f->__prev_ = base::__end_.__prev_; + __f->__prev_->__next_ = __f; + base::__end_.__prev_ = __l; } @@ -2058,14 +2058,16 @@ template void list<_Tp, _Alloc>::remove(const value_type& __x) { - for (iterator __i = begin(), __e = end(); __i != __e;) + list<_Tp, _Alloc> __deleted_nodes; // collect the nodes we're removing + for (const_iterator __i = begin(), __e = end(); __i != __e;) { if (*__i == __x) { - iterator __j = _VSTD::next(__i); + const_iterator __j = _VSTD::next(__i); for (; __j != __e && *__j == __x; ++__j) ; - __i = erase(__i, __j); + __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j); + __i = __j; if (__i != __e) ++__i; } diff --git a/test/containers/sequences/list/list.ops/remove.pass.cpp b/test/containers/sequences/list/list.ops/remove.pass.cpp index 76d878d0..106c0527 100644 --- a/test/containers/sequences/list/list.ops/remove.pass.cpp +++ b/test/containers/sequences/list/list.ops/remove.pass.cpp @@ -16,6 +16,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() { { @@ -25,6 +36,27 @@ int main() c.remove(3); assert(c == std::list(a2, a2+3)); } + { // LWG issue #526 + int a1[] = {1, 2, 1, 3, 5, 8, 11}; + int a2[] = { 2, 3, 5, 8, 11}; + std::list c(a1, a1+7); + c.remove(c.front()); + assert(c == std::list(a2, a2+5)); + } + { + int a1[] = {1, 2, 1, 3, 5, 8, 11, 1}; + int a2[] = { 2, 3, 5, 8, 11 }; + std::list c; + for(int *ip = a1; ip < a1+8; ++ip) + c.push_back(S(*ip)); + c.remove(c.front()); + std::list::const_iterator it = c.begin(); + for(int *ip = a2; ip < a2+5; ++ip, ++it) { + assert ( it != c.end()); + assert ( *ip == it->get()); + } + assert ( it == c.end ()); + } #if __cplusplus >= 201103L { int a1[] = {1, 2, 3, 4};