public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7150] libstdc++/ranges: Use C++23 deducing this in _Pipe and _Partial
@ 2024-01-11 18:15 Patrick Palka
  0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2024-01-11 18:15 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:d2cb4693a0b383e971f67feb96d3b48ff997d2d5

commit r14-7150-gd2cb4693a0b383e971f67feb96d3b48ff997d2d5
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu Jan 11 13:14:46 2024 -0500

    libstdc++/ranges: Use C++23 deducing this in _Pipe and _Partial
    
    This simplifies the operator() of the _Pipe and _Partial range adaptor
    closure objects using C++23 deducing this, allowing us to condense
    multiple operator() overloads into one.
    
    The new __like_t alias template is similar to the expositional one from
    P0847R6 except it's implemented in terms of forward_like instead of vice
    versa, and thus ours always yields a reference so e.g. __like_t<A, char>
    is char&& instead of char.  For our purposes (forwarding) this shouldn't
    make a difference, I think..
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/move.h (__like_t): Define in C++23 mode.
            * include/std/ranges (views::__adaptor::Partial::operator()):
            Implement using C++23 deducing this when available.
            (views::__adaptor::_Pipe::operator()): Likewise.
            * testsuite/std/ranges/adaptors/100577.cc: Adjust testcase to
            accept new "no match for call" errors issued in C++23 mode.
            * testsuite/std/ranges/adaptors/lazy_split_neg.cc: Likewise.

Diff:
---
 libstdc++-v3/include/bits/move.h                   |  3 ++
 libstdc++-v3/include/std/ranges                    | 36 ++++++++++++++++++++--
 .../testsuite/std/ranges/adaptors/100577.cc        | 18 +++++------
 .../std/ranges/adaptors/lazy_split_neg.cc          |  2 +-
 4 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/libstdc++-v3/include/bits/move.h b/libstdc++-v3/include/bits/move.h
index 4e741bcdeb0..bb200c95964 100644
--- a/libstdc++-v3/include/bits/move.h
+++ b/libstdc++-v3/include/bits/move.h
@@ -110,6 +110,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  return static_cast<_Up&>(__x);
       }
   }
+
+  template<typename _Tp, typename _Up>
+    using __like_t = decltype(std::forward_like<_Tp>(std::declval<_Up>()));
 #endif
 
   /**
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 81a857502e3..aa7b288ba4d 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -1011,7 +1011,19 @@ namespace views::__adaptor
 
       // Invoke _Adaptor with arguments __r, _M_args... according to the
       // value category of this _Partial object.
-      // TODO: use explicit object functions ("deducing this").
+#if __cpp_explicit_this_parameter
+      template<typename _Self, typename _Range>
+	requires __adaptor_invocable<_Adaptor, _Range, __like_t<_Self, _Args>...>
+	constexpr auto
+	operator()(this _Self&& __self, _Range&& __r)
+	{
+	  auto __forwarder = [&__r] (auto&&... __args) {
+	    return _Adaptor{}(std::forward<_Range>(__r),
+			      std::forward<decltype(__args)>(__args)...);
+	  };
+	  return std::apply(__forwarder, std::forward<_Self>(__self)._M_args);
+	}
+#else
       template<typename _Range>
 	requires __adaptor_invocable<_Adaptor, _Range, const _Args&...>
 	constexpr auto
@@ -1037,6 +1049,7 @@ namespace views::__adaptor
       template<typename _Range>
 	constexpr auto
 	operator()(_Range&& __r) const && = delete;
+#endif
     };
 
   // A lightweight specialization of the above primary template for
@@ -1051,6 +1064,13 @@ namespace views::__adaptor
 	: _M_arg(std::move(__arg))
       { }
 
+#if __cpp_explicit_this_parameter
+      template<typename _Self, typename _Range>
+	requires __adaptor_invocable<_Adaptor, _Range, __like_t<_Self, _Arg>>
+	constexpr auto
+	operator()(this _Self&& __self, _Range&& __r)
+	{ return _Adaptor{}(std::forward<_Range>(__r), std::forward<_Self>(__self)._M_arg); }
+#else
       template<typename _Range>
 	requires __adaptor_invocable<_Adaptor, _Range, const _Arg&>
 	constexpr auto
@@ -1066,6 +1086,7 @@ namespace views::__adaptor
       template<typename _Range>
 	constexpr auto
 	operator()(_Range&& __r) const && = delete;
+#endif
     };
 
   // Partial specialization of the primary template for the case where the extra
@@ -1142,7 +1163,17 @@ namespace views::__adaptor
 
       // Invoke _M_rhs(_M_lhs(__r)) according to the value category of this
       // range adaptor closure object.
-      // TODO: use explicit object functions ("deducing this").
+#if __cpp_explicit_this_parameter
+      template<typename _Self, typename _Range>
+	requires __pipe_invocable<__like_t<_Self, _Lhs>, __like_t<_Self, _Rhs>, _Range>
+	constexpr auto
+	operator()(this _Self&& __self, _Range&& __r)
+	{
+	  return (std::forward<_Self>(__self)._M_rhs
+		  (std::forward<_Self>(__self)._M_lhs
+		   (std::forward<_Range>(__r))));
+	}
+#else
       template<typename _Range>
 	requires __pipe_invocable<const _Lhs&, const _Rhs&, _Range>
 	constexpr auto
@@ -1158,6 +1189,7 @@ namespace views::__adaptor
       template<typename _Range>
 	constexpr auto
 	operator()(_Range&& __r) const && = delete;
+#endif
     };
 
   // A partial specialization of the above primary template for the case where
diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/100577.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/100577.cc
index 69072d69fec..3a52f5b4ce0 100644
--- a/libstdc++-v3/testsuite/std/ranges/adaptors/100577.cc
+++ b/libstdc++-v3/testsuite/std/ranges/adaptors/100577.cc
@@ -98,28 +98,28 @@ test02()
   (views::take_while(badarg) | views::all)(x); // { dg-error "no match" }
   (views::drop_while(badarg) | views::all)(x); // { dg-error "no match" }
 
-  // In practice, range adaptor closures with non-simple operator() are
+  // In C++20 mode, range adaptor closures with non-simple operator() are
   // implemented using a fallback deleted overload, so when a call is
   // ill-formed overload resolution succeeds but selects the deleted overload
   // (but only when the closure is invoked as an rvalue).
-  views::lazy_split(badarg)(x); // { dg-error "deleted function" }
-  (views::lazy_split(badarg) | views::all)(x); // { dg-error "deleted function" }
+  views::lazy_split(badarg)(x); // { dg-error "deleted function|no match" }
+  (views::lazy_split(badarg) | views::all)(x); // { dg-error "deleted function|no match" }
   auto a0 = views::lazy_split(badarg);
   a0(x); // { dg-error "no match" };
   auto a1 = a0 | views::all;
   a1(x); // { dg-error "no match" }
 
-  views::split(badarg)(x); // { dg-error "deleted function" }
-  (views::split(badarg) | views::all)(x); // { dg-error "deleted function" }
+  views::split(badarg)(x); // { dg-error "deleted function|no match" }
+  (views::split(badarg) | views::all)(x); // { dg-error "deleted function|no match" }
   auto a0a = views::split(badarg);
   a0a(x); // { dg-error "no match" };
   auto a1a = a0a | views::all;
   a1a(x); // { dg-error "no match" }
 
-  views::take(badarg)(x); // { dg-error "deleted" }
-  views::drop(badarg)(x); // { dg-error "deleted" }
-  (views::take(badarg) | views::all)(x); // { dg-error "deleted" }
-  (views::drop(badarg) | views::all)(x); // { dg-error "deleted" }
+  views::take(badarg)(x); // { dg-error "deleted|no match" }
+  views::drop(badarg)(x); // { dg-error "deleted|no match" }
+  (views::take(badarg) | views::all)(x); // { dg-error "deleted|no match" }
+  (views::drop(badarg) | views::all)(x); // { dg-error "deleted|no match" }
 }
 
 void
diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/lazy_split_neg.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/lazy_split_neg.cc
index 683ea76da07..85632759fcc 100644
--- a/libstdc++-v3/testsuite/std/ranges/adaptors/lazy_split_neg.cc
+++ b/libstdc++-v3/testsuite/std/ranges/adaptors/lazy_split_neg.cc
@@ -38,7 +38,7 @@ test02()
 {
   using namespace std::literals;
   auto x = "the  quick  brown  fox"sv;
-  auto v1 = views::lazy_split(std::initializer_list<char>{' ', ' '})(x); // { dg-error "deleted" }
+  auto v1 = views::lazy_split(std::initializer_list<char>{' ', ' '})(x); // { dg-error "deleted|no match" }
   auto v2 = x | views::lazy_split(std::initializer_list<char>{' ', ' '}); // { dg-error "no match" }
 }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-11 18:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-11 18:15 [gcc r14-7150] libstdc++/ranges: Use C++23 deducing this in _Pipe and _Partial Patrick Palka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).