[libcxx-commits] [libcxx] [libc++] Implement `ranges::shift_right` (PR #177847)

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jan 31 06:00:53 PST 2026


================
@@ -27,65 +33,70 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 20
 
-template <class _ForwardIterator>
-inline _LIBCPP_HIDE_FROM_ABI constexpr _ForwardIterator
-shift_right(_ForwardIterator __first,
-            _ForwardIterator __last,
-            typename iterator_traits<_ForwardIterator>::difference_type __n) {
+template <class _AlgPolicy, class _Iter, class _Sent>
+_LIBCPP_HIDE_FROM_ABI constexpr pair<_Iter, _Iter>
+__shift_right(_Iter __first, _Sent __last, typename _IterOps<_AlgPolicy>::template __difference_type<_Iter> __n) {
+  _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n >= 0, "n must be greater than or equal to 0");
+  _Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
   if (__n == 0) {
-    return __first;
+    return pair<_Iter, _Iter>(std::move(__first), std::move(__end));
----------------
huixie90 wrote:

Actually We can't get rid of `__n >= __size` check. Because we can have situations where `Iter` is `random_access` but `sized_sentinel_for<Iter, Sent>` is false. A simple example is `iota_view::iterator`

https://github.com/llvm/llvm-project/pull/177847


More information about the libcxx-commits mailing list