Title
The helper lambda of std::erase for list should specify return type as bool
Status
wp
Section
[forward.list.erasure][list.erasure]
Submitter
Hewill Kang

Created on 2024-08-07.00:00:00 last changed 3 weeks ago

Messages

Date: 2024-11-28.21:40:31

Proposed resolution:

This wording is relative to N4988.

  1. Modify [forward.list.erasure] as indicated:

    template<class T, class Allocator, class U = T>
      typename forward_list<T, Allocator>::size_type
        erase(forward_list<T, Allocator>& c, const U& value);
    

    -1- Effects: Equivalent to: return erase_if(c, [&](const auto& elem) -> bool { return elem == value; });

  2. Modify [list.erasure] as indicated:

    template<class T, class Allocator, class U = T>
      typename list<T, Allocator>::size_type
        erase(list<T, Allocator>& c, const U& value);
    

    -1- Effects: Equivalent to: return erase_if(c, [&](const auto& elem) -> bool { return elem == value; });

Date: 2024-11-28.21:40:31

[ Wrocław 2024-11-23; Status changed: Voting → WP. ]

Date: 2024-08-15.00:00:00

[ 2024-08-21; Reflector poll ]

Set status to Tentatively Ready after nine votes in favour during reflector poll.

Date: 2024-08-07.00:00:00

std::erase for list is specified to return erase_if(c, [&](auto& elem) { return elem == value; }). However, the template parameter Predicate of erase_if only requires that the type of decltype(pred(...)) satisfies boolean-testable, i.e., the return type of elem == value is not necessarily bool.

This means it's worth explicitly specifying the lambda's return type as bool to avoid some pedantic cases (demo):

#include <list>

struct Bool {
  Bool(const Bool&) = delete;
  operator bool() const;
};

struct Int {
  Bool& operator==(Int) const;
};

int main() {
  std::list<Int> l;
  std::erase(l, Int{}); // unnecessary hard error
}
History
Date User Action Args
2024-11-28 21:40:31adminsetmessages: + msg14488
2024-11-28 21:40:31adminsetstatus: voting -> wp
2024-11-19 16:09:07adminsetstatus: ready -> voting
2024-08-21 14:46:30adminsetmessages: + msg14326
2024-08-21 14:46:30adminsetstatus: new -> ready
2024-08-10 15:41:37adminsetmessages: + msg14314
2024-08-07 00:00:00admincreate