]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Use std::move for iterator in ranges::fill [PR117094]
authorJonathan Wakely <jwakely@redhat.com>
Sun, 13 Oct 2024 21:48:43 +0000 (22:48 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Mon, 14 Oct 2024 09:55:50 +0000 (10:55 +0100)
Input iterators aren't required to be copyable.

libstdc++-v3/ChangeLog:

PR libstdc++/117094
* include/bits/ranges_algobase.h (__fill_fn): Use std::move for
iterator that might not be copyable.
* testsuite/25_algorithms/fill/constrained.cc: Check
non-copyable iterator with sized sentinel.

libstdc++-v3/include/bits/ranges_algobase.h
libstdc++-v3/testsuite/25_algorithms/fill/constrained.cc

index 3c8d46198c5dd30528e312dbf6d5472ed256dd30..0345ea850a4e5db173016b00526609f7e6b948f0 100644 (file)
@@ -592,7 +592,7 @@ namespace ranges
        if constexpr (sized_sentinel_for<_Sent, _Out>)
          {
            const auto __len = __last - __first;
-           return ranges::fill_n(__first, __len, __value);
+           return ranges::fill_n(std::move(__first), __len, __value);
          }
        else if constexpr (is_scalar_v<_Tp>)
          {
index 126515eddcaa94b305b53d1c5025d0b2bad3a03e..7cae99f2d5cef208c184fb8b7d8f8b994fd35b73 100644 (file)
@@ -83,9 +83,43 @@ test02()
   return ok;
 }
 
+void
+test03()
+{
+  // Bug libstdc++/117094 - ranges::fill misses std::move for output_iterator
+
+  // Move-only output iterator
+  struct Iterator
+  {
+    using difference_type = long;
+    Iterator(int* p) : p(p) { }
+    Iterator(Iterator&&) = default;
+    Iterator& operator=(Iterator&&) = default;
+    int& operator*() const { return *p; }
+    Iterator& operator++() { ++p; return *this; }
+    Iterator operator++(int) { return Iterator(p++ ); }
+    int* p;
+
+    struct Sentinel
+    {
+      const int* p;
+      bool operator==(const Iterator& i) const { return p == i.p; }
+      long operator-(const Iterator& i) const { return p - i.p; }
+    };
+
+    long operator-(Sentinel s) const { return p - s.p; }
+  };
+  static_assert(std::sized_sentinel_for<Iterator::Sentinel, Iterator>);
+  int a[2];
+  std::ranges::fill(Iterator(a), Iterator::Sentinel{a+2}, 999);
+  VERIFY( a[0] == 999 );
+  VERIFY( a[1] == 999 );
+}
+
 int
 main()
 {
   test01();
   static_assert(test02());
+  test03();
 }