[libcxx-commits] [PATCH] D58332: Erase-Like Algorithms Should Return size_type (P0646R1)

Marshall Clow via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Apr 4 08:58:37 PDT 2019


mclow.lists added a comment.

In D58332#1455053 <https://reviews.llvm.org/D58332#1455053>, @mclow.lists wrote:

> A meta-question:
>  Why does `list<_Tp, _Alloc>::remove(const value_type& __x)` gather all the deleted nodes into a separate list, while
>  `list<_Tp, _Alloc>::remove_if(_Pred __pred)` and `list<_Tp, _Alloc>::unique()` just delete them as they go?
>
> they're all the same operation under the hood.
>  [ I know you didn't create this difference. ]


And the reason is:
The `value_type &x` passed into remove might refer to an item in the list itself, and if we destroy the elements as we go, we will destroy the value that we're comparing against. This is not the case for `unique`, but it could be the case for `remove_if` (imagine a list of predicates, and calling `remove_if` passing one of the prediccates in the list).  I'll fix this up later.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D58332/new/

https://reviews.llvm.org/D58332





More information about the libcxx-commits mailing list