[libcxx-commits] [libcxx] 9311996 - [libc++][ranges] implement `ranges::shift_left` (#83231)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jan 25 02:35:48 PST 2026


Author: xiaoyang-sde
Date: 2026-01-25T10:35:43Z
New Revision: 9311996261e1bc9acf979cf308e4d53a0330d671

URL: https://github.com/llvm/llvm-project/commit/9311996261e1bc9acf979cf308e4d53a0330d671
DIFF: https://github.com/llvm/llvm-project/commit/9311996261e1bc9acf979cf308e4d53a0330d671.diff

LOG: [libc++][ranges] implement `ranges::shift_left` (#83231)

Implement the `ranges::shift_left` algorithm from
[P2440R1](https://wg21.link/P2440R1).

Closes: #134061

---------

Co-authored-by: Hui Xie <hui.xie1990 at gmail.com>
Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>

Added: 
    libcxx/include/__algorithm/ranges_shift_left.h
    libcxx/test/libcxx/algorithms/alg.modifying.operations/alg.shift/assert.shift_left.pass.cpp
    libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp

Modified: 
    libcxx/docs/ReleaseNotes/23.rst
    libcxx/docs/Status/Cxx23Papers.csv
    libcxx/include/CMakeLists.txt
    libcxx/include/__algorithm/shift_left.h
    libcxx/include/algorithm
    libcxx/include/module.modulemap.in
    libcxx/modules/std/algorithm.inc
    libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
    libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
    libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/ReleaseNotes/23.rst b/libcxx/docs/ReleaseNotes/23.rst
index 73f5984768592..bec665c167ce3 100644
--- a/libcxx/docs/ReleaseNotes/23.rst
+++ b/libcxx/docs/ReleaseNotes/23.rst
@@ -38,6 +38,8 @@ What's New in Libc++ 23.0.0?
 Implemented Papers
 ------------------
 
+- P2440R1 (partial): ``ranges::iota`` and ``ranges::shift_left`` are supported (`Github <https://llvm.org/PR105184>`__)
+
 Improvements and New Features
 -----------------------------
 

diff  --git a/libcxx/docs/Status/Cxx23Papers.csv b/libcxx/docs/Status/Cxx23Papers.csv
index 2a5c64a53d79e..a787a4a6215ec 100644
--- a/libcxx/docs/Status/Cxx23Papers.csv
+++ b/libcxx/docs/Status/Cxx23Papers.csv
@@ -46,7 +46,7 @@
 "`P2255R2 <https://wg21.link/P2255R2>`__","A type trait to detect reference binding to temporary","2022-02 (Virtual)","|Partial|","","`#105180 <https://github.com/llvm/llvm-project/issues/105180>`__","Implemented the type traits only."
 "`P2273R3 <https://wg21.link/P2273R3>`__","Making ``std::unique_ptr`` constexpr","2022-02 (Virtual)","|Complete|","16","`#105182 <https://github.com/llvm/llvm-project/issues/105182>`__",""
 "`P2387R3 <https://wg21.link/P2387R3>`__","Pipe support for user-defined range adaptors","2022-02 (Virtual)","|Complete|","19","`#105183 <https://github.com/llvm/llvm-project/issues/105183>`__",""
-"`P2440R1 <https://wg21.link/P2440R1>`__","``ranges::iota``, ``ranges::shift_left`` and ``ranges::shift_right``","2022-02 (Virtual)","|Partial|","","`#105184 <https://github.com/llvm/llvm-project/issues/105184>`__","Only ``ranges::iota`` is implemented."
+"`P2440R1 <https://wg21.link/P2440R1>`__","``ranges::iota``, ``ranges::shift_left`` and ``ranges::shift_right``","2022-02 (Virtual)","|Partial|","","`#105184 <https://github.com/llvm/llvm-project/issues/105184>`__","``ranges::iota`` and ``ranges::shift_left`` are implemented."
 "`P2441R2 <https://wg21.link/P2441R2>`__","``views::join_with``","2022-02 (Virtual)","|Complete|","21","`#105185 <https://github.com/llvm/llvm-project/issues/105185>`__",""
 "`P2442R1 <https://wg21.link/P2442R1>`__","Windowing range adaptors: ``views::chunk`` and ``views::slide``","2022-02 (Virtual)","","","`#105187 <https://github.com/llvm/llvm-project/issues/105187>`__",""
 "`P2443R1 <https://wg21.link/P2443R1>`__","``views::chunk_by``","2022-02 (Virtual)","|Complete|","18","`#105188 <https://github.com/llvm/llvm-project/issues/105188>`__",""

diff  --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index a4ad3bedd2460..1ceba14329cb2 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -157,6 +157,7 @@ set(files
   __algorithm/ranges_set_intersection.h
   __algorithm/ranges_set_symmetric_
diff erence.h
   __algorithm/ranges_set_union.h
+  __algorithm/ranges_shift_left.h
   __algorithm/ranges_shuffle.h
   __algorithm/ranges_sort.h
   __algorithm/ranges_sort_heap.h

diff  --git a/libcxx/include/__algorithm/ranges_shift_left.h b/libcxx/include/__algorithm/ranges_shift_left.h
new file mode 100644
index 0000000000000..7d494820cc412
--- /dev/null
+++ b/libcxx/include/__algorithm/ranges_shift_left.h
@@ -0,0 +1,73 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___ALGORITHM_RANGES_SHIFT_LEFT_H
+#define _LIBCPP___ALGORITHM_RANGES_SHIFT_LEFT_H
+
+#include <__algorithm/iterator_operations.h>
+#include <__algorithm/shift_left.h>
+#include <__config>
+#include <__iterator/concepts.h>
+#include <__iterator/distance.h>
+#include <__iterator/incrementable_traits.h>
+#include <__iterator/permutable.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <__ranges/subrange.h>
+#include <__utility/move.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 23
+
+namespace ranges {
+namespace __shift_left {
+
+struct __fn {
+  template <permutable _Iter, sentinel_for<_Iter> _Sent>
+  _LIBCPP_HIDE_FROM_ABI static constexpr subrange<_Iter>
+  operator()(_Iter __first, _Sent __last, iter_
diff erence_t<_Iter> __n) {
+    auto __ret = std::__shift_left<_RangeAlgPolicy>(std::move(__first), std::move(__last), std::move(__n));
+    return {std::move(__ret.first), std::move(__ret.second)};
+  }
+
+  template <forward_range _Range>
+    requires permutable<iterator_t<_Range>>
+  _LIBCPP_HIDE_FROM_ABI static constexpr borrowed_subrange_t<_Range>
+  operator()(_Range&& __range, range_
diff erence_t<_Range> __n) {
+    if constexpr (sized_range<_Range>) {
+      if (__n >= ranges::distance(__range)) {
+        return {ranges::begin(__range), ranges::begin(__range)};
+      }
+    }
+
+    auto __ret = std::__shift_left<_RangeAlgPolicy>(ranges::begin(__range), ranges::end(__range), std::move(__n));
+    return {std::move(__ret.first), std::move(__ret.second)};
+  }
+};
+} // namespace __shift_left
+
+inline namespace __cpo {
+inline constexpr auto shift_left = __shift_left::__fn{};
+} // namespace __cpo
+} // namespace ranges
+
+#endif // _LIBCPP_STD_VER >= 23
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___ALGORITHM_RANGES_SHIFT_LEFT_H

diff  --git a/libcxx/include/__algorithm/shift_left.h b/libcxx/include/__algorithm/shift_left.h
index 06cd7c5f87644..42d6c2321ff7d 100644
--- a/libcxx/include/__algorithm/shift_left.h
+++ b/libcxx/include/__algorithm/shift_left.h
@@ -9,9 +9,14 @@
 #ifndef _LIBCPP___ALGORITHM_SHIFT_LEFT_H
 #define _LIBCPP___ALGORITHM_SHIFT_LEFT_H
 
+#include <__algorithm/iterator_operations.h>
 #include <__algorithm/move.h>
+#include <__assert>
 #include <__config>
+#include <__iterator/concepts.h>
 #include <__iterator/iterator_traits.h>
+#include <__utility/move.h>
+#include <__utility/pair.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -24,30 +29,42 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 20
 
-template <class _ForwardIterator>
-inline _LIBCPP_HIDE_FROM_ABI constexpr _ForwardIterator
-shift_left(_ForwardIterator __first,
-           _ForwardIterator __last,
-           typename iterator_traits<_ForwardIterator>::
diff erence_type __n) {
+template <class _AlgPolicy, class _Iter, class _Sent>
+_LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter>
+__shift_left(_Iter __first, _Sent __last, typename _IterOps<_AlgPolicy>::template __
diff erence_type<_Iter> __n) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n >= 0, "n must be greater than or equal to 0");
+
   if (__n == 0) {
-    return __last;
+    _Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
+    return {std::move(__first), std::move(__end)};
   }
 
-  _ForwardIterator __m = __first;
-  if constexpr (__has_random_access_iterator_category<_ForwardIterator>::value) {
-    if (__n >= __last - __first) {
-      return __first;
+  _Iter __m = __first;
+  if constexpr (sized_sentinel_for<_Sent, _Iter>) {
+    auto __size = _IterOps<_AlgPolicy>::distance(__first, __last);
+    if (__n >= __size) {
+      return {__first, std::move(__first)};
     }
-    __m += __n;
+    _IterOps<_AlgPolicy>::advance(__m, __n);
   } else {
     for (; __n > 0; --__n) {
       if (__m == __last) {
-        return __first;
+        return {__first, std::move(__first)};
       }
       ++__m;
     }
   }
-  return std::move(__m, __last, __first);
+
+  _Iter __result = std::__move<_AlgPolicy>(__m, __last, __first).second;
+  return {std::move(__first), std::move(__result)};
+}
+
+template <class _ForwardIterator>
+_LIBCPP_HIDE_FROM_ABI constexpr _ForwardIterator
+shift_left(_ForwardIterator __first,
+           _ForwardIterator __last,
+           typename iterator_traits<_ForwardIterator>::
diff erence_type __n) {
+  return std::__shift_left<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __n).second;
 }
 
 #endif // _LIBCPP_STD_VER >= 20

diff  --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 64e9fa6306145..5dd7b9dc990e1 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -883,6 +883,13 @@ namespace ranges {
             uniform_random_bit_generator<remove_reference_t<Gen>>
     O sample(R&& r, O out, range_
diff erence_t<R> n, Gen&& g);                                       // since C++20
 
+  template<permutable I, sentinel_for<I> S>
+    constexpr subrange<I> ranges::shift_left(I first, S last, iter_
diff erence_t<I> n);              // since C++23
+
+  template<forward_range R>
+    requires permutable<iterator_t<R>>
+    constexpr borrowed_subrange_t<R> ranges::shift_left(R&& r, range_
diff erence_t<R> n)             // since C++23
+
   template<random_access_iterator I, sentinel_for<I> S, class Gen>
     requires permutable<I> &&
             uniform_random_bit_generator<remove_reference_t<Gen>>
@@ -2035,6 +2042,7 @@ template <class BidirectionalIterator, class Compare>
 #    include <__algorithm/ranges_ends_with.h>
 #    include <__algorithm/ranges_find_last.h>
 #    include <__algorithm/ranges_fold.h>
+#    include <__algorithm/ranges_shift_left.h>
 #    include <__algorithm/ranges_starts_with.h>
 #  endif // _LIBCPP_STD_VER >= 23
 

diff  --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 62f928667bbd6..83b0259cac9e4 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -760,6 +760,9 @@ module std [system] {
       export std.functional.ranges_operations
       export std.algorithm.in_in_out_result
     }
+    module ranges_shift_left {
+      header "__algorithm/ranges_shift_left.h"
+    }
     module ranges_shuffle {
       header "__algorithm/ranges_shuffle.h"
     }

diff  --git a/libcxx/modules/std/algorithm.inc b/libcxx/modules/std/algorithm.inc
index 95c05f01e5562..540bcc3879678 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -356,9 +356,11 @@ export namespace std {
   // [alg.shift], shift
   using std::shift_left;
 
+#if _LIBCPP_STD_VER >= 23
   namespace ranges {
-    // using std::ranges::shift_left;
+    using std::ranges::shift_left;
   }
+#endif // _LIBCPP_STD_VER >= 23
 
   using std::shift_right;
 

diff  --git a/libcxx/test/libcxx/algorithms/alg.modifying.operations/alg.shift/assert.shift_left.pass.cpp b/libcxx/test/libcxx/algorithms/alg.modifying.operations/alg.shift/assert.shift_left.pass.cpp
new file mode 100644
index 0000000000000..0d29ebc1b3390
--- /dev/null
+++ b/libcxx/test/libcxx/algorithms/alg.modifying.operations/alg.shift/assert.shift_left.pass.cpp
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// UNSUPPORTED: libcpp-hardening-mode=none
+// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
+
+#include <algorithm>
+#include <array>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+  std::array<int, 5> arr = {1, 2, 3, 4, 5};
+  TEST_LIBCPP_ASSERT_FAILURE(std::ranges::shift_left(arr, -2), "n must be greater than or equal to 0");
+  TEST_LIBCPP_ASSERT_FAILURE(
+      std::ranges::shift_left(arr.begin(), arr.end(), -2), "n must be greater than or equal to 0");
+
+  return 0;
+}

diff  --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
new file mode 100644
index 0000000000000..9639fc529c2a0
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/ranges.shift_left.pass.cpp
@@ -0,0 +1,371 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=9000000
+
+// template<permutable I, sentinel_for<I> S>
+//   constexpr subrange<I> ranges::shift_left(I first, S last, iter_
diff erence_t<I> n);
+
+// template<forward_range R>
+//   requires permutable<iterator_t<R>>
+//   constexpr borrowed_subrange_t<R> ranges::shift_left(R&& r, range_
diff erence_t<R> n)
+
+#include <algorithm>
+#include <array>
+#include <cassert>
+#include <ranges>
+#include <iterator>
+#include <vector>
+
+#include "almost_satisfies_types.h"
+#include "test_iterators.h"
+#include "MoveOnly.h"
+
+struct InvalidDifferenceT {};
+
+template <class Iter, class Sent = Iter, class N = std::iter_
diff erence_t<Iter>>
+concept HasShiftLeftIt = requires(Iter iter, Sent sent, N n) { std::ranges::shift_left(iter, sent, n); };
+
+static_assert(HasShiftLeftIt<int*>);
+static_assert(HasShiftLeftIt<int*, sentinel_wrapper<int*>>);
+static_assert(HasShiftLeftIt<int*, sized_sentinel<int*>>);
+static_assert(!HasShiftLeftIt<int*, int*, InvalidDifferenceT>);
+static_assert(!HasShiftLeftIt<int*, int, int>);
+
+static_assert(!HasShiftLeftIt<ForwardIteratorNotDerivedFrom>);
+static_assert(!HasShiftLeftIt<PermutableNotForwardIterator>);
+static_assert(!HasShiftLeftIt<PermutableNotSwappable>);
+
+template <class Range, class N = std::ranges::range_
diff erence_t<Range>>
+concept HasShiftLeftR = requires(Range range, N n) { std::ranges::shift_left(range, n); };
+
+static_assert(HasShiftLeftR<UncheckedRange<int*>>);
+static_assert(!HasShiftLeftR<UncheckedRange<int*>, InvalidDifferenceT>);
+
+static_assert(!HasShiftLeftR<ForwardRangeNotDerivedFrom>);
+static_assert(!HasShiftLeftR<PermutableRangeNotForwardIterator>);
+static_assert(!HasShiftLeftR<PermutableRangeNotSwappable>);
+
+// An iterator whose iterator_traits::
diff erence_type is not the same as
+// std::iter_
diff erence_t<It>.
+struct DiffTypeIter {
+  int* it_;
+
+  using 
diff erence_type   = InvalidDifferenceT;
+  using value_type        = int;
+  using reference         = int&;
+  using pointer           = int*;
+  using iterator_category = std::random_access_iterator_tag;
+
+  constexpr DiffTypeIter() : it_() {}
+  constexpr explicit DiffTypeIter(int* it) : it_(it) {}
+
+  constexpr reference operator*() const { return *it_; }
+  constexpr reference operator[](int n) const { return it_[n]; }
+
+  constexpr DiffTypeIter& operator++() {
+    ++it_;
+    return *this;
+  }
+  constexpr DiffTypeIter& operator--() {
+    --it_;
+    return *this;
+  }
+  constexpr DiffTypeIter operator++(int) { return DiffTypeIter(it_++); }
+  constexpr DiffTypeIter operator--(int) { return DiffTypeIter(it_--); }
+
+  constexpr DiffTypeIter& operator+=(int n) {
+    it_ += n;
+    return *this;
+  }
+  constexpr DiffTypeIter& operator-=(int n) {
+    it_ -= n;
+    return *this;
+  }
+  friend constexpr DiffTypeIter operator+(DiffTypeIter x, int n) {
+    x += n;
+    return x;
+  }
+  friend constexpr DiffTypeIter operator+(int n, DiffTypeIter x) {
+    x += n;
+    return x;
+  }
+  friend constexpr DiffTypeIter operator-(DiffTypeIter x, int n) {
+    x -= n;
+    return x;
+  }
+  friend constexpr int operator-(DiffTypeIter x, DiffTypeIter y) { return x.it_ - y.it_; }
+
+  friend constexpr bool operator==(const DiffTypeIter& x, const DiffTypeIter& y) { return x.it_ == y.it_; }
+  friend constexpr bool operator!=(const DiffTypeIter& x, const DiffTypeIter& y) { return x.it_ != y.it_; }
+  friend constexpr bool operator<(const DiffTypeIter& x, const DiffTypeIter& y) { return x.it_ < y.it_; }
+  friend constexpr bool operator<=(const DiffTypeIter& x, const DiffTypeIter& y) { return x.it_ <= y.it_; }
+  friend constexpr bool operator>(const DiffTypeIter& x, const DiffTypeIter& y) { return x.it_ > y.it_; }
+  friend constexpr bool operator>=(const DiffTypeIter& x, const DiffTypeIter& y) { return x.it_ >= y.it_; }
+};
+
+template <>
+struct std::incrementable_traits<DiffTypeIter> {
+  using 
diff erence_type = std::ptr
diff _t;
+};
+
+struct TrackCopyMove {
+  mutable int copy_count = 0;
+  int move_count         = 0;
+
+  constexpr TrackCopyMove() = default;
+  constexpr TrackCopyMove(const TrackCopyMove& other) : copy_count(other.copy_count), move_count(other.move_count) {
+    ++copy_count;
+    ++other.copy_count;
+  }
+
+  constexpr TrackCopyMove(TrackCopyMove&& other) noexcept : copy_count(other.copy_count), move_count(other.move_count) {
+    ++move_count;
+    ++other.move_count;
+  }
+  constexpr TrackCopyMove& operator=(const TrackCopyMove& other) {
+    ++copy_count;
+    ++other.copy_count;
+    return *this;
+  }
+  constexpr TrackCopyMove& operator=(TrackCopyMove&& other) noexcept {
+    ++move_count;
+    ++other.move_count;
+    return *this;
+  }
+};
+
+template <class Iter, class Sent>
+constexpr void test_iter_sent() {
+  {
+    const std::array<int, 8> original = {3, 1, 4, 1, 5, 9, 2, 6};
+
+    // (iterator, sentinel) overload
+    for (size_t n = 0; n <= original.size(); ++n) {
+      for (size_t k = 0; k <= n + 2; ++k) {
+        std::array<int, 8> scratch;
+        auto begin = Iter(scratch.data());
+        auto end   = Sent(Iter(scratch.data() + n));
+        std::ranges::copy(original.begin(), original.begin() + n, begin);
+        std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::shift_left(begin, end, k);
+
+        assert(result.begin() == begin);
+        if (k < n) {
+          assert(result.end() == Iter(scratch.data() + n - k));
+          assert(std::ranges::equal(original.begin() + k, original.begin() + n, result.begin(), result.end()));
+        } else {
+          assert(result.end() == begin);
+          assert(std::ranges::equal(original.begin(), original.begin() + n, begin, end));
+        }
+      }
+    }
+
+    // (range) overload
+    for (size_t n = 0; n <= original.size(); ++n) {
+      for (size_t k = 0; k <= n + 2; ++k) {
+        std::array<int, 8> scratch;
+        auto begin = Iter(scratch.data());
+        auto end   = Sent(Iter(scratch.data() + n));
+        std::ranges::copy(original.begin(), original.begin() + n, begin);
+        auto range                                                      = std::ranges::subrange(begin, end);
+        std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::shift_left(range, k);
+
+        assert(result.begin() == begin);
+        if (k < n) {
+          assert(result.end() == Iter(scratch.data() + n - k));
+          assert(std::ranges::equal(original.begin() + k, original.begin() + n, begin, result.end()));
+        } else {
+          assert(result.end() == begin);
+          assert(std::ranges::equal(original.begin(), original.begin() + n, begin, end));
+        }
+      }
+    }
+  }
+
+  // n == 0
+  {
+    std::array<int, 3> input          = {0, 1, 2};
+    const std::array<int, 3> expected = {0, 1, 2};
+
+    { // (iterator, sentinel) overload
+      auto in                                                         = input;
+      auto begin                                                      = Iter(in.data());
+      auto end                                                        = Sent(Iter(in.data() + in.size()));
+      std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::shift_left(begin, end, 0);
+      assert(std::ranges::equal(expected, result));
+      assert(result.begin() == begin);
+      assert(result.end() == end);
+    }
+
+    { // (range) overload
+      auto in                                                         = input;
+      auto begin                                                      = Iter(in.data());
+      auto end                                                        = Sent(Iter(in.data() + in.size()));
+      auto range                                                      = std::ranges::subrange(begin, end);
+      std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::shift_left(range, 0);
+      assert(std::ranges::equal(expected, result));
+      assert(result.begin() == begin);
+      assert(result.end() == end);
+    }
+  }
+
+  // n == len
+  {
+    std::array<int, 3> input          = {0, 1, 2};
+    const std::array<int, 3> expected = {0, 1, 2};
+
+    { // (iterator, sentinel) overload
+      auto in    = input;
+      auto begin = Iter(in.data());
+      auto end   = Sent(Iter(in.data() + in.size()));
+      std::same_as<std::ranges::subrange<Iter>> decltype(auto) result =
+          std::ranges::shift_left(begin, end, input.size());
+      assert(std::ranges::equal(expected, input));
+      assert(result.begin() == begin);
+      assert(result.end() == begin);
+    }
+
+    { // (range) overload
+      auto in                                                         = input;
+      auto begin                                                      = Iter(in.data());
+      auto end                                                        = Sent(Iter(in.data() + in.size()));
+      auto range                                                      = std::ranges::subrange(begin, end);
+      std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::shift_left(range, input.size());
+      assert(std::ranges::equal(expected, input));
+      assert(result.begin() == begin);
+      assert(result.end() == begin);
+    }
+  }
+
+  // n > len
+  {
+    std::array<int, 3> input          = {0, 1, 2};
+    const std::array<int, 3> expected = {0, 1, 2};
+
+    { // (iterator, sentinel) overload
+      auto in    = input;
+      auto begin = Iter(in.data());
+      auto end   = Sent(Iter(in.data() + in.size()));
+      std::same_as<std::ranges::subrange<Iter>> decltype(auto) result =
+          std::ranges::shift_left(begin, end, input.size() + 1);
+      assert(std::ranges::equal(expected, input));
+      assert(result.begin() == begin);
+      assert(result.end() == begin);
+    }
+
+    { // (range) overload
+      auto in    = input;
+      auto begin = Iter(in.data());
+      auto end   = Sent(Iter(in.data() + in.size()));
+      auto range = std::ranges::subrange(begin, end);
+      std::same_as<std::ranges::subrange<Iter>> decltype(auto) result =
+          std::ranges::shift_left(range, input.size() + 1);
+      assert(std::ranges::equal(expected, input));
+      assert(result.begin() == begin);
+      assert(result.end() == begin);
+    }
+  }
+
+  // empty range
+  {
+    std::vector<int> input          = {};
+    const std::vector<int> expected = {};
+    { // (iterator, sentinel) overload
+      auto in    = input;
+      auto begin = Iter(in.data());
+      auto end   = Sent(Iter(in.data() + in.size()));
+      std::same_as<std::ranges::subrange<Iter>> decltype(auto) result =
+          std::ranges::shift_left(begin, end, input.size() + 1);
+      assert(std::ranges::equal(expected, input));
+      assert(result.begin() == begin);
+      assert(result.end() == begin);
+    }
+
+    { // (range) overload
+      auto in    = input;
+      auto begin = Iter(in.data());
+      auto end   = Sent(Iter(in.data() + in.size()));
+      auto range = std::ranges::subrange(begin, end);
+      std::same_as<std::ranges::subrange<Iter>> decltype(auto) result =
+          std::ranges::shift_left(range, input.size() + 1);
+      assert(std::ranges::equal(expected, input));
+      assert(result.begin() == begin);
+      assert(result.end() == begin);
+    }
+  }
+}
+
+constexpr bool test() {
+  types::for_each(types::forward_iterator_list<int*>{}, []<class Iter> {
+    test_iter_sent<Iter, Iter>();
+    test_iter_sent<Iter, sentinel_wrapper<Iter>>();
+    test_iter_sent<Iter, sized_sentinel<Iter>>();
+  });
+  test_iter_sent<DiffTypeIter, DiffTypeIter>();
+
+  // Complexity: At most (last - first) - n assignments
+  {
+    constexpr int length = 100;
+    constexpr int n      = length / 2;
+    auto make_vec        = []() {
+      std::vector<TrackCopyMove> vec;
+      vec.reserve(length);
+      for (int i = 0; i < length; ++i) {
+        vec.emplace_back();
+      }
+      return vec;
+    };
+
+    { // (iterator, sentinel) overload
+      auto input  = make_vec();
+      auto result = std::ranges::shift_left(input.begin(), input.end(), n);
+      assert(result.begin() == input.begin());
+      assert(result.end() == input.begin() + (length - n));
+
+      auto total_copies = 0;
+      auto total_moves  = 0;
+      for (auto it = result.begin(); it != result.end(); ++it) {
+        const auto& item = *it;
+        total_copies += item.copy_count;
+        total_moves += item.move_count;
+      }
+
+      assert(total_copies == 0);
+      assert(total_moves <= length - n);
+    }
+
+    { // (range) overload
+      auto input  = make_vec();
+      auto result = std::ranges::shift_left(input, n);
+      assert(result.begin() == input.begin());
+      assert(result.end() == input.begin() + (length - n));
+
+      auto total_copies = 0;
+      auto total_moves  = 0;
+      for (auto it = result.begin(); it != result.end(); ++it) {
+        const auto& item = *it;
+        total_copies += item.copy_count;
+        total_moves += item.move_count;
+      }
+
+      assert(total_copies == 0);
+      assert(total_moves <= length - n);
+    }
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+  return 0;
+}

diff  --git a/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
index e0a6aaaf03aa0..17037acfe7184 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
@@ -224,6 +224,7 @@ constexpr bool test_all() {
 
 #if TEST_STD_VER >= 23
   dangling_1st<out_value_result<dangling, decltype(x)>>(std::ranges::iota, in, x);
+  dangling_1st(std::ranges::shift_left, in, x);
 #endif
 
   return true;

diff  --git a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
index 9d4b0d608518a..a442743333ea8 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
@@ -191,7 +191,9 @@ constexpr void run_tests() {
   test(std::ranges::sort_heap, in);
   test(std::ranges::prev_permutation, in);
   test(std::ranges::next_permutation, in);
-
+#if TEST_STD_VER >= 23
+  test(std::ranges::shift_left, in, count);
+#endif
   // The algorithms that work on uninitialized memory have constraints that prevent proxy iterators from being used with
   // them.
 }

diff  --git a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
index dc9134f3deb3c..c799af04fc688 100644
--- a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
+++ b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
@@ -155,6 +155,9 @@ static_assert(test(std::ranges::set_
diff erence, a, a, a));
 static_assert(test(std::ranges::set_intersection, a, a, a));
 static_assert(test(std::ranges::set_symmetric_
diff erence, a, a, a));
 static_assert(test(std::ranges::set_union, a, a, a));
+#if TEST_STD_VER >= 23
+static_assert(test(std::ranges::shift_left, a, 42));
+#endif
 static_assert(test(std::ranges::shuffle, a, g));
 static_assert(test(std::ranges::sort, a));
 static_assert(test(std::ranges::sort_heap, a));


        


More information about the libcxx-commits mailing list