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

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jan 11 01:08:31 PST 2026


================
@@ -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_difference_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_difference_t<_Range> __n) {
+    if constexpr (sized_range<_Range>) {
----------------
huixie90 wrote:

i am not 100% sure but I wonder if it is worth to change the condition to 
```
if constexpr (sized_range<_Range> && !sized_sentinel_for<iterator_t<_Range>, sentinel_t<_Range>>) {
```
to avoid situations where the  ` n >= size` check happened twice (once here and once in the impl of `__shift_left`.  another option is to add a template parameter to `__shift_left` to indicate this has already been checked

However, if you check the clang codegen and it can elide the second check inside the `__shift_left` in that case, i think the simple `size_range` check is better as it is simpler

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


More information about the libcxx-commits mailing list