diff mbox series

[committed,v4] libstdc++: Fix std::ranges::iota is not included in numeric [PR108760]

Message ID 20240608150555.457573-1-jwakely@redhat.com
State New
Headers show
Series [committed,v4] libstdc++: Fix std::ranges::iota is not included in numeric [PR108760] | expand

Commit Message

Jonathan Wakely June 8, 2024, 3:03 p.m. UTC
From: Michael Levine <mlevine55@bloomberg.net>

I committed the missing include separately, and pushed Michael's change
as attached (with some whitespace tweaks and a changelog entry).

Thanks for the patch, Michael!

Tested x86_64-linux. Pushed to trunk.

I'm in two minds about backporting this one. It would be good to fix the
non-conformance problem for the release branches, but it also
potentially breaks some code that uses ranges::iota without including
<numeric>. Ideally we'd make ranges::iota available in *both* <numeric>
and <algorithm> for gcc-13 and gcc-14, as a transition aid. I'm not sure
I can be bothered to move it to a separate header to make that work, nor
to include all of bits/ranges_algo.h in <numeric>.

-- >8 --

Before this patch, using std::ranges::iota required including
<algorithm> when it should have been sufficient to only include
<numeric>.

libstdc++-v3/ChangeLog:

	PR libstdc++/108760
	* include/bits/ranges_algo.h (ranges::out_value_result):
	Move to <bits/ranges_algobase.h>.
	(ranges::iota_result, ranges::__iota_fn, ranges::iota): Move to
	<numeric>.
	* include/bits/ranges_algobase.h (ranges::out_value_result):
	Move to here.
	* include/std/numeric (ranges::iota_result, ranges::__iota_fn)
	(ranges::iota): Move to here.
	* testsuite/25_algorithms/iota/1.cc: Renamed to ...
	* testsuite/26_numerics/iota/2.cc: ... here.

Signed-off-by: Michael Levine <mlevine55@bloomberg.net>
---
 libstdc++-v3/include/bits/ranges_algo.h       | 52 -------------------
 libstdc++-v3/include/bits/ranges_algobase.h   | 24 +++++++++
 libstdc++-v3/include/std/numeric              | 38 ++++++++++++++
 .../iota/1.cc => 26_numerics/iota/2.cc}       |  2 +-
 4 files changed, 63 insertions(+), 53 deletions(-)
 rename libstdc++-v3/testsuite/{25_algorithms/iota/1.cc => 26_numerics/iota/2.cc} (96%)

Comments

Ulrich Drepper June 8, 2024, 3:55 p.m. UTC | #1
On Sat, Jun 8, 2024 at 5:03 PM Jonathan Wakely <jwakely@redhat.com> wrote:
> I'm in two minds about backporting this one. It would be good to fix the
> non-conformance problem for the release branches, but it also
> potentially breaks some code that uses ranges::iota without including
> <numeric>.

I say add the change as soon as possible so that there is as little
code as possible relying on the non-standard header.
Jonathan Wakely June 8, 2024, 7:23 p.m. UTC | #2
On Sat, 8 Jun 2024 at 16:56, Ulrich Drepper <drepper.fsp@gmail.com> wrote:
>
> On Sat, Jun 8, 2024 at 5:03 PM Jonathan Wakely <jwakely@redhat.com> wrote:
> > I'm in two minds about backporting this one. It would be good to fix the
> > non-conformance problem for the release branches, but it also
> > potentially breaks some code that uses ranges::iota without including
> > <numeric>.
>
> I say add the change as soon as possible so that there is as little
> code as possible relying on the non-standard header.

Yes, that's good rationale for doing the backports.
diff mbox series

Patch

diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index 62faff173bd..d258be0b93f 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -3521,58 +3521,6 @@  namespace ranges
 
 #endif // __glibcxx_ranges_contains
 
-#if __glibcxx_ranges_iota >= 202202L // C++ >= 23
-
-  template<typename _Out, typename _Tp>
-    struct out_value_result
-    {
-      [[no_unique_address]] _Out out;
-      [[no_unique_address]] _Tp value;
-
-      template<typename _Out2, typename _Tp2>
-	requires convertible_to<const _Out&, _Out2>
-	  && convertible_to<const _Tp&, _Tp2>
-	constexpr
-	operator out_value_result<_Out2, _Tp2>() const &
-	{ return {out, value}; }
-
-      template<typename _Out2, typename _Tp2>
-	requires convertible_to<_Out, _Out2>
-	  && convertible_to<_Tp, _Tp2>
-	constexpr
-	operator out_value_result<_Out2, _Tp2>() &&
-	{ return {std::move(out), std::move(value)}; }
-    };
-
-  template<typename _Out, typename _Tp>
-    using iota_result = out_value_result<_Out, _Tp>;
-
-  struct __iota_fn
-  {
-    template<input_or_output_iterator _Out, sentinel_for<_Out> _Sent, weakly_incrementable _Tp>
-      requires indirectly_writable<_Out, const _Tp&>
-      constexpr iota_result<_Out, _Tp>
-      operator()(_Out __first, _Sent __last, _Tp __value) const
-      {
-	while (__first != __last)
-	  {
-	    *__first = static_cast<const _Tp&>(__value);
-	    ++__first;
-	    ++__value;
-	  }
-	return {std::move(__first), std::move(__value)};
-      }
-
-    template<weakly_incrementable _Tp, output_range<const _Tp&> _Range>
-      constexpr iota_result<borrowed_iterator_t<_Range>, _Tp>
-      operator()(_Range&& __r, _Tp __value) const
-      { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__value)); }
-  };
-
-  inline constexpr __iota_fn iota{};
-
-#endif // __glibcxx_ranges_iota
-
 #if __glibcxx_ranges_find_last >= 202207L // C++ >= 23
 
   struct __find_last_fn
diff --git a/libstdc++-v3/include/bits/ranges_algobase.h b/libstdc++-v3/include/bits/ranges_algobase.h
index e1f00838818..7ce5ac314f2 100644
--- a/libstdc++-v3/include/bits/ranges_algobase.h
+++ b/libstdc++-v3/include/bits/ranges_algobase.h
@@ -35,6 +35,7 @@ 
 #include <compare>
 #include <bits/stl_iterator_base_funcs.h>
 #include <bits/stl_iterator.h>
+#include <bits/stl_algobase.h> // __memcpy
 #include <bits/ranges_base.h> // ranges::begin, ranges::range etc.
 #include <bits/invoke.h>      // __invoke
 #include <bits/cpp_type_traits.h> // __is_byte
@@ -71,6 +72,29 @@  namespace ranges
 	__is_move_iterator<move_iterator<_Iterator>> = true;
   } // namespace __detail
 
+#if __glibcxx_ranges_iota >= 202202L // C++ >= 23
+  template<typename _Out, typename _Tp>
+    struct out_value_result
+    {
+      [[no_unique_address]] _Out out;
+      [[no_unique_address]] _Tp value;
+
+      template<typename _Out2, typename _Tp2>
+	requires convertible_to<const _Out&, _Out2>
+	  && convertible_to<const _Tp&, _Tp2>
+	constexpr
+	operator out_value_result<_Out2, _Tp2>() const &
+	{ return {out, value}; }
+
+      template<typename _Out2, typename _Tp2>
+	requires convertible_to<_Out, _Out2>
+	  && convertible_to<_Tp, _Tp2>
+	constexpr
+	operator out_value_result<_Out2, _Tp2>() &&
+	{ return {std::move(out), std::move(value)}; }
+    };
+#endif // __glibcxx_ranges_iota
+
   struct __equal_fn
   {
     template<input_iterator _Iter1, sentinel_for<_Iter1> _Sent1,
diff --git a/libstdc++-v3/include/std/numeric b/libstdc++-v3/include/std/numeric
index c912db4a519..201bb8e74a1 100644
--- a/libstdc++-v3/include/std/numeric
+++ b/libstdc++-v3/include/std/numeric
@@ -89,6 +89,10 @@ 
 #define __glibcxx_want_saturation_arithmetic
 #include <bits/version.h>
 
+#if __glibcxx_ranges_iota >= 202202L // C++ >= 23
+# include <bits/ranges_algobase.h> // for ranges::out_value_result
+#endif
+
 #ifdef __glibcxx_saturation_arithmetic // C++ >= 26
 # include <bits/sat_arith.h>
 #endif
@@ -726,6 +730,40 @@  namespace __detail
   /// @} group numeric_ops
 #endif // C++17
 
+namespace ranges
+{
+#if __glibcxx_ranges_iota >= 202202L // C++ >= 23
+
+  template<typename _Out, typename _Tp>
+  using iota_result = out_value_result<_Out, _Tp>;
+
+  struct __iota_fn
+  {
+    template<input_or_output_iterator _Out, sentinel_for<_Out> _Sent, weakly_incrementable _Tp>
+      requires indirectly_writable<_Out, const _Tp&>
+      constexpr iota_result<_Out, _Tp>
+      operator()(_Out __first, _Sent __last, _Tp __value) const
+      {
+	while (__first != __last)
+	  {
+	    *__first = static_cast<const _Tp&>(__value);
+	    ++__first;
+	    ++__value;
+	  }
+	return {std::move(__first), std::move(__value)};
+      }
+
+    template<weakly_incrementable _Tp, output_range<const _Tp&> _Range>
+      constexpr iota_result<borrowed_iterator_t<_Range>, _Tp>
+      operator()(_Range&& __r, _Tp __value) const
+      { return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__value)); }
+  };
+
+  inline constexpr __iota_fn iota{};
+
+#endif // __glibcxx_ranges_iota
+} // namespace ranges
+
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
 
diff --git a/libstdc++-v3/testsuite/25_algorithms/iota/1.cc b/libstdc++-v3/testsuite/26_numerics/iota/2.cc
similarity index 96%
rename from libstdc++-v3/testsuite/25_algorithms/iota/1.cc
rename to libstdc++-v3/testsuite/26_numerics/iota/2.cc
index 61bf418b4da..040c48d91ce 100644
--- a/libstdc++-v3/testsuite/25_algorithms/iota/1.cc
+++ b/libstdc++-v3/testsuite/26_numerics/iota/2.cc
@@ -1,6 +1,6 @@ 
 // { dg-do run { target c++23 } }
 
-#include <algorithm>
+#include <numeric>
 #include <testsuite_hooks.h>
 #include <testsuite_iterators.h>