Skip to content

Commit d8d0a4f

Browse files
committed
[libcxx] Add LWG4135: The helper lambda of std::erase for list should specify return type as bool
1 parent 73ad78c commit d8d0a4f

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

libcxx/docs/Status/Cxx2cIssues.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"`LWG4124 <https://wg21.link/LWG4124>`__","Cannot format ``zoned_time`` with resolution coarser than ``seconds``","2024-11 (Wrocław)","","",""
9898
"`LWG4126 <https://wg21.link/LWG4126>`__","Some feature-test macros for fully freestanding features are not yet marked freestanding","2024-11 (Wrocław)","","",""
9999
"`LWG4134 <https://wg21.link/LWG4134>`__","Issue with Philox algorithm specification","2024-11 (Wrocław)","","",""
100-
"`LWG4135 <https://wg21.link/LWG4135>`__","The helper lambda of ``std::erase`` for list should specify return type as ``bool``","2024-11 (Wrocław)","","",""
100+
"`LWG4135 <https://wg21.link/LWG4135>`__","The helper lambda of ``std::erase`` for list should specify return type as ``bool``","2024-11 (Wrocław)","|Complete|","21",""
101101
"`LWG4140 <https://wg21.link/LWG4140>`__","Useless default constructors for bit reference types","2024-11 (Wrocław)","","",""
102102
"`LWG4141 <https://wg21.link/LWG4141>`__","Improve prohibitions on ""additional storage""","2024-11 (Wrocław)","","",""
103103
"`LWG4142 <https://wg21.link/LWG4142>`__","``format_parse_context::check_dynamic_spec`` should require at least one type","2024-11 (Wrocław)","","",""

libcxx/include/forward_list

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ erase_if(forward_list<_Tp, _Allocator>& __c, _Predicate __pred) {
15571557
template <class _Tp, class _Allocator, class _Up>
15581558
inline _LIBCPP_HIDE_FROM_ABI typename forward_list<_Tp, _Allocator>::size_type
15591559
erase(forward_list<_Tp, _Allocator>& __c, const _Up& __v) {
1560-
return std::erase_if(__c, [&](auto& __elem) { return __elem == __v; });
1560+
return std::erase_if(__c, [&](const auto& __elem) -> bool { return __elem == __v; });
15611561
}
15621562
# endif
15631563

libcxx/include/list

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,7 @@ erase_if(list<_Tp, _Allocator>& __c, _Predicate __pred) {
17031703
template <class _Tp, class _Allocator, class _Up>
17041704
inline _LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Allocator>::size_type
17051705
erase(list<_Tp, _Allocator>& __c, const _Up& __v) {
1706-
return std::erase_if(__c, [&](auto& __elem) { return __elem == __v; });
1706+
return std::erase_if(__c, [&](const auto& __elem) -> bool { return __elem == __v; });
17071707
}
17081708

17091709
template <>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: std-at-least-c++20
10+
11+
// <forward_list>
12+
13+
// This test shows the effect of implementing `LWG4135`, before it this code
14+
// shouldn't compile with a long error message, as the predicate is not bool.
15+
// `LWG4135` suggests that std::erase explicitly specifying the lambda's
16+
// return type as bool.
17+
18+
#include <forward_list>
19+
20+
struct Bool {
21+
Bool() = default;
22+
Bool(const Bool&) = delete;
23+
operator bool() const { return true; }
24+
};
25+
26+
struct Int {
27+
Bool& operator==(Int) const {
28+
static Bool b;
29+
return b;
30+
}
31+
};
32+
33+
int main(int, char**) {
34+
std::forward_list<Int> l;
35+
std::erase(l, Int{});
36+
37+
return 0;
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: std-at-least-c++20
10+
11+
// <list>
12+
13+
// This test shows the effect of implementing `LWG4135`, before it this code
14+
// shouldn't compile with a long error message, as the predicate is not bool.
15+
// `LWG4135` suggests that std::erase explicitly specifying the lambda's
16+
// return type as bool.
17+
18+
#include <list>
19+
20+
struct Bool {
21+
Bool() = default;
22+
Bool(const Bool&) = delete;
23+
operator bool() const { return true; }
24+
};
25+
26+
struct Int {
27+
Bool& operator==(Int) const {
28+
static Bool b;
29+
return b;
30+
}
31+
};
32+
33+
int main(int, char**) {
34+
std::list<Int> l;
35+
std::erase(l, Int{});
36+
37+
return 0;
38+
}

0 commit comments

Comments
 (0)