diff mbox series

libstdc++: Fix forwarding in __take/drop_of_repeat_view [PR112453]

Message ID 20231109160028.2829009-1-ppalka@redhat.com
State New
Headers show
Series libstdc++: Fix forwarding in __take/drop_of_repeat_view [PR112453] | expand

Commit Message

Patrick Palka Nov. 9, 2023, 4 p.m. UTC
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(-)

Comments

Jonathan Wakely Nov. 9, 2023, 7:33 p.m. UTC | #1
On Thu, 9 Nov 2023 at 16:01, Patrick Palka <ppalka@redhat.com> wrote:
>
> 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.)

OK for trunk and gcc-13, thanks.


>
> -- >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
>
diff mbox series

Patch

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();
 }