Skip to content

Commit

Permalink
[libcxx] Add LWG4135: The helper lambda of std::erase for list should…
Browse files Browse the repository at this point in the history
… specify return type as bool
  • Loading branch information
elhewaty committed Feb 22, 2025
1 parent 73ad78c commit d8d0a4f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
2 changes: 1 addition & 1 deletion libcxx/docs/Status/Cxx2cIssues.csv
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"`LWG4124 <https://wg21.link/LWG4124>`__","Cannot format ``zoned_time`` with resolution coarser than ``seconds``","2024-11 (Wrocław)","","",""
"`LWG4126 <https://wg21.link/LWG4126>`__","Some feature-test macros for fully freestanding features are not yet marked freestanding","2024-11 (Wrocław)","","",""
"`LWG4134 <https://wg21.link/LWG4134>`__","Issue with Philox algorithm specification","2024-11 (Wrocław)","","",""
"`LWG4135 <https://wg21.link/LWG4135>`__","The helper lambda of ``std::erase`` for list should specify return type as ``bool``","2024-11 (Wrocław)","","",""
"`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",""
"`LWG4140 <https://wg21.link/LWG4140>`__","Useless default constructors for bit reference types","2024-11 (Wrocław)","","",""
"`LWG4141 <https://wg21.link/LWG4141>`__","Improve prohibitions on ""additional storage""","2024-11 (Wrocław)","","",""
"`LWG4142 <https://wg21.link/LWG4142>`__","``format_parse_context::check_dynamic_spec`` should require at least one type","2024-11 (Wrocław)","","",""
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/forward_list
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,7 @@ erase_if(forward_list<_Tp, _Allocator>& __c, _Predicate __pred) {
template <class _Tp, class _Allocator, class _Up>
inline _LIBCPP_HIDE_FROM_ABI typename forward_list<_Tp, _Allocator>::size_type
erase(forward_list<_Tp, _Allocator>& __c, const _Up& __v) {
return std::erase_if(__c, [&](auto& __elem) { return __elem == __v; });
return std::erase_if(__c, [&](const auto& __elem) -> bool { return __elem == __v; });
}
# endif

Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/list
Original file line number Diff line number Diff line change
Expand Up @@ -1703,7 +1703,7 @@ erase_if(list<_Tp, _Allocator>& __c, _Predicate __pred) {
template <class _Tp, class _Allocator, class _Up>
inline _LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Allocator>::size_type
erase(list<_Tp, _Allocator>& __c, const _Up& __v) {
return std::erase_if(__c, [&](auto& __elem) { return __elem == __v; });
return std::erase_if(__c, [&](const auto& __elem) -> bool { return __elem == __v; });
}

template <>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// REQUIRES: std-at-least-c++20

// <forward_list>

// This test shows the effect of implementing `LWG4135`, before it this code
// shouldn't compile with a long error message, as the predicate is not bool.
// `LWG4135` suggests that std::erase explicitly specifying the lambda's
// return type as bool.

#include <forward_list>

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

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

int main(int, char**) {
std::forward_list<Int> l;
std::erase(l, Int{});

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// REQUIRES: std-at-least-c++20

// <list>

// This test shows the effect of implementing `LWG4135`, before it this code
// shouldn't compile with a long error message, as the predicate is not bool.
// `LWG4135` suggests that std::erase explicitly specifying the lambda's
// return type as bool.

#include <list>

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

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

int main(int, char**) {
std::list<Int> l;
std::erase(l, Int{});

return 0;
}

0 comments on commit d8d0a4f

Please sign in to comment.