This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of Voting status.

4135. The helper lambda of std::erase for list should specify return type as bool

Section: 23.3.7.7 [forward.list.erasure], 23.3.9.6 [list.erasure] Status: Voting Submitter: Hewill Kang Opened: 2024-08-07 Last modified: 2024-11-19

Priority: Not Prioritized

View all issues with Voting status.

Discussion:

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
}

[2024-08-21; Reflector poll]

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

Proposed resolution:

This wording is relative to N4988.

  1. Modify 23.3.7.7 [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 23.3.9.6 [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; });