public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] libstdc++: Fix forwarding in __take/drop_of_repeat_view [PR112453]
Date: Thu,  9 Nov 2023 11:00:28 -0500	[thread overview]
Message-ID: <20231109160028.2829009-1-ppalka@redhat.com> (raw)

Tested on x86_64-pc-linux-gnu, does this look OK for trunk/13?  (The
&& overloads are also missing on earlier branches, but I don't think
it makes a difference there since all uses of that operator* are on
lvalues before this fix.)

-- >8 --

We need to respect the value category of the repeat_view passed to these
two functions when accessing its _M_value member.  This revealed that
the space-efficient partial specialization of __box lacks && overloads
of operator* to match std::optional's API.

	PR libstdc++/112453

libstdc++-v3/ChangeLog:

	* include/std/ranges (__detail::__box::operator*): Define &&
	overloads as well.
	(__detail::__take_of_repeat_view): Forward __r when accessing
	its _M_value member.
	(__detail::__drop_of_repeat_view): Likewise.
	* testsuite/std/ranges/repeat/1.cc (test07): New test.
---
 libstdc++-v3/include/std/ranges               | 20 ++++++++++++++-----
 libstdc++-v3/testsuite/std/ranges/repeat/1.cc | 13 ++++++++++++
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 7893e3a84c9..41f95dc8f78 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -250,13 +250,21 @@ namespace ranges
 	{ return true; };
 
 	constexpr _Tp&
-	operator*() noexcept
+	operator*() & noexcept
 	{ return _M_value; }
 
 	constexpr const _Tp&
-	operator*() const noexcept
+	operator*() const & noexcept
 	{ return _M_value; }
 
+	constexpr _Tp&&
+	operator*() && noexcept
+	{ return std::move(_M_value); }
+
+	constexpr const _Tp&&
+	operator*() const && noexcept
+	{ return std::move(_M_value); }
+
 	constexpr _Tp*
 	operator->() noexcept
 	{ return std::__addressof(_M_value); }
@@ -7799,9 +7807,10 @@ namespace views::__adaptor
 	  using _Tp = remove_cvref_t<_Range>;
 	  static_assert(__is_repeat_view<_Tp>);
 	  if constexpr (sized_range<_Tp>)
-	    return views::repeat(*__r._M_value, std::min(ranges::distance(__r), __n));
+	    return views::repeat(*std::forward<_Range>(__r)._M_value,
+				 std::min(ranges::distance(__r), __n));
 	  else
-	    return views::repeat(*__r._M_value, __n);
+	    return views::repeat(*std::forward<_Range>(__r)._M_value, __n);
 	}
 
       template<typename _Range>
@@ -7813,7 +7822,8 @@ namespace views::__adaptor
 	  if constexpr (sized_range<_Tp>)
 	    {
 	      auto __sz = ranges::distance(__r);
-	      return views::repeat(*__r._M_value, __sz - std::min(__sz, __n));
+	      return views::repeat(*std::forward<_Range>(__r)._M_value,
+				   __sz - std::min(__sz, __n));
 	    }
 	  else
 	    return __r;
diff --git a/libstdc++-v3/testsuite/std/ranges/repeat/1.cc b/libstdc++-v3/testsuite/std/ranges/repeat/1.cc
index 30636407ee2..9551414e2c8 100644
--- a/libstdc++-v3/testsuite/std/ranges/repeat/1.cc
+++ b/libstdc++-v3/testsuite/std/ranges/repeat/1.cc
@@ -2,6 +2,7 @@
 
 #include <ranges>
 #include <algorithm>
+#include <memory>
 #include <testsuite_hooks.h>
 
 #if __cpp_lib_ranges_repeat != 202207L
@@ -137,6 +138,17 @@ test06()
   static_assert( requires { views::repeat(move_only{}, 2); } );
 }
 
+void
+test07()
+{
+  // PR libstdc++/112453
+  auto t = std::views::repeat(std::make_unique<int>(5)) | std::views::take(2);
+  auto d = std::views::repeat(std::make_unique<int>(5)) | std::views::drop(2);
+
+  auto t2 = std::views::repeat(std::make_unique<int>(5), 4) | std::views::take(2);
+  auto d2 = std::views::repeat(std::make_unique<int>(5), 4) | std::views::drop(2);
+}
+
 int
 main()
 {
@@ -146,4 +158,5 @@ main()
   static_assert(test04());
   test05();
   test06();
+  test07();
 }
-- 
2.43.0.rc1


             reply	other threads:[~2023-11-09 16:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-09 16:00 Patrick Palka [this message]
2023-11-09 19:33 ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231109160028.2829009-1-ppalka@redhat.com \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).