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

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jan 28 12:49:50 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));
----------------
ldionne wrote:

You could also add a case like this:

```c++
if constexpr (sized_sentinel_for<Iter, Sent>) {
  if (__n >= ranges::distance(__first, __last)) {
    return pair<_Iter, _Iter>(__end, std::move(__end));
  }
}
```

This would allow removing the same check below under `if constexpr (derived_from<_IterCategory, random_access_iterator_tag>`, the one in `ranges_shift_right.h` for the range overload, and it would centralize things. WDYT?

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


More information about the libcxx-commits mailing list