Issue 4135: The helper lambda of std::erase for list should specify return type as
This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of WP status.
4135. The helper lambda of std::erase
for list
should specify return type asbool
Section: 23.3.7.7 [forward.list.erasure], 23.3.11.6 [list.erasure] Status: WP Submitter: Hewill Kang Opened: 2024-08-07 Last modified: 2024-11-28
Priority: Not Prioritized
View all issues with WP status.
Discussion:
std::erase
for list
is specified to returnerase_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
struct Bool { Bool(const Bool&) = delete; operator bool() const; };
struct Int { Bool& operator==(Int) const; };
int main() { std::list 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.
[Wrocław 2024-11-23; Status changed: Voting → WP.]
Proposed resolution:
This wording is relative to N4988.
- 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; });
- Modify 23.3.11.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; });