[libcxx-commits] [libcxx] [libc++][ranges] use `static operator()` for C++23 ranges (PR #86052)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Wed Mar 20 22:18:13 PDT 2024


================
@@ -205,17 +205,19 @@ namespace views {
 namespace __chunk_by {
 struct __fn {
   template <class _Range, class _Pred>
-  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pred&& __pred) const
-      noexcept(noexcept(/**/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))))
-          -> decltype(/*--*/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))) {
-    return /*-------------*/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred));
+  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(
+      _Range&& __range,
+      _Pred&& __pred) noexcept(noexcept(chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))))
+      -> decltype(/*-----------------*/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))) {
+    return /*------------------------*/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred));
   }
 
   template <class _Pred>
     requires constructible_from<decay_t<_Pred>, _Pred>
-  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pred&& __pred) const
-      noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>) {
-    return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pred>(__pred)));
+  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto
+  operator()(_Pred&& __pred) noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>) {
+    constexpr auto __self = __fn{};
+    return __range_adaptor_closure_t(std::__bind_back(__self, std::forward<_Pred>(__pred)));
----------------
frederick-vs-ja wrote:

> The solution here is to initialize the function object `__fn` as a `constexpr` variable, which will be evaluated at compile time, and pass it to `std::__bind_back`. Please let me know if there's a better solution!

This looks good to me. [P2714R1](https://wg21.link/p2714r1) (adopted for C++26) exactly adds such things.

https://github.com/llvm/llvm-project/pull/86052


More information about the libcxx-commits mailing list