Skip to content

<algorithm>: ranges::find_if_not's help lambda should return bool #69074

Closed
@hewillk

Description

@hewillk

template <input_range _Rp, class _Proj = identity, indirect_unary_predicate<projected<iterator_t<_Rp>, _Proj>> _Pred>
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr borrowed_iterator_t<_Rp>
operator()(_Rp&& __r, _Pred __pred, _Proj __proj = {}) const {
auto __pred2 = [&](auto&& __e) { return !std::invoke(__pred, std::forward<decltype(__e)>(__e)); };
return ranges::__find_if_impl(ranges::begin(__r), ranges::end(__r), __pred2, __proj);
}

The lambda here should specify the return type as bool, such as [&](auto&& __e) -> bool because the type that satisfies boolean-testable does not necessarily have to be bool:

#include <algorithm>

const struct Boolean {
  Boolean() = default;
  Boolean(Boolean&&) = delete;
  operator bool() const;
  const Boolean& operator!() const;
} boolean;

#ifdef __clang__
static_assert(std::__boolean_testable<Boolean>);
#endif

int main() {
  // libc++ rejects
  auto it = std::ranges::find_if_not(" ", [&](char) -> auto& { return boolean; });
}

https://godbolt.org/z/4M15cvarx

Metadata

Metadata

Assignees

Labels

libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.rangesIssues related to `<ranges>`

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions