[libcxx-commits] [libcxx] [libcxx] P2278R4: implement `{basic_, }const_iterator`, and have `cbegin` et. al. return it (PR #99915)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jul 24 09:47:30 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: nicole mazzuca (strega-nil)

<details>
<summary>Changes</summary>

Implements `basic_const_iterator` and the const accessors.

Does not implement `const_view`.

Does not implement changes to `span` and friends.

Still to do: testing.

---

Patch is 101.21 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/99915.diff


16 Files Affected:

- (modified) libcxx/docs/Status/Cxx23Papers.csv (+1-1) 
- (modified) libcxx/include/CMakeLists.txt (+2) 
- (added) libcxx/include/__iterator/const_iterator.h (+340) 
- (modified) libcxx/include/__ranges/access.h (-55) 
- (modified) libcxx/include/__ranges/concepts.h (+14) 
- (added) libcxx/include/__ranges/const_access.h (+208) 
- (modified) libcxx/include/__ranges/rbegin.h (-28) 
- (modified) libcxx/include/__ranges/rend.h (-27) 
- (modified) libcxx/include/iterator (+29) 
- (modified) libcxx/include/module.modulemap (+2) 
- (modified) libcxx/include/ranges (+8) 
- (modified) libcxx/modules/std/iterator.inc (+9-6) 
- (modified) libcxx/test/std/ranges/range.access/begin.pass.cpp (+96-64) 
- (modified) libcxx/test/std/ranges/range.access/end.pass.cpp (+92-67) 
- (modified) libcxx/test/std/ranges/range.access/rbegin.pass.cpp (+147-86) 
- (modified) libcxx/test/std/ranges/range.access/rend.pass.cpp (+141-88) 


``````````diff
diff --git a/libcxx/docs/Status/Cxx23Papers.csv b/libcxx/docs/Status/Cxx23Papers.csv
index f8970320ce1fa..2d294e8501e5b 100644
--- a/libcxx/docs/Status/Cxx23Papers.csv
+++ b/libcxx/docs/Status/Cxx23Papers.csv
@@ -61,7 +61,7 @@
 "`P1899R3 <https://wg21.link/P1899R3>`__","LWG","``stride_view``","July 2022","","","|ranges|"
 "`P2093R14 <https://wg21.link/P2093R14>`__","LWG","Formatted output","July 2022","|Complete|","18.0","|format|"
 "`P2165R4 <https://wg21.link/P2165R4>`__","LWG","Compatibility between ``tuple``, ``pair`` and ``tuple-like`` objects","July 2022","",""
-"`P2278R4 <https://wg21.link/P2278R4>`__","LWG","``cbegin`` should always return a constant iterator","July 2022","","","|ranges|"
+"`P2278R4 <https://wg21.link/P2278R4>`__","LWG","``cbegin`` should always return a constant iterator","July 2022","In Progress","","|ranges|"
 "`P2286R8 <https://wg21.link/P2286R8>`__","LWG","Formatting Ranges","July 2022","|Complete|","16.0","|format| |ranges|"
 "`P2291R3 <https://wg21.link/P2291R3>`__","LWG","Add Constexpr Modifiers to Functions ``to_chars`` and ``from_chars`` for Integral Types in ``<charconv>`` Header","July 2022","|Complete|","16.0"
 "`P2302R4 <https://wg21.link/P2302R4>`__","LWG","``std::ranges::contains``","July 2022","|Complete|","19.0","|ranges|"
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 32579272858a8..577b7975f3eb2 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -444,6 +444,7 @@ set(files
   __iterator/bounded_iter.h
   __iterator/common_iterator.h
   __iterator/concepts.h
+  __iterator/const_iterator.h
   __iterator/counted_iterator.h
   __iterator/cpp17_iterator_concepts.h
   __iterator/data.h
@@ -639,6 +640,7 @@ set(files
   __ranges/chunk_by_view.h
   __ranges/common_view.h
   __ranges/concepts.h
+  __ranges/const_access.h
   __ranges/container_compatible_range.h
   __ranges/counted.h
   __ranges/dangling.h
diff --git a/libcxx/include/__iterator/const_iterator.h b/libcxx/include/__iterator/const_iterator.h
new file mode 100644
index 0000000000000..4ad37345cd887
--- /dev/null
+++ b/libcxx/include/__iterator/const_iterator.h
@@ -0,0 +1,340 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// 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___ITERATOR_CONST_ITERATOR_H
+#define _LIBCPP___ITERATOR_CONST_ITERATOR_H
+
+#include <__compare/three_way_comparable.h>
+#include <__concepts/common_with.h>
+#include <__concepts/convertible_to.h>
+#include <__concepts/different_from.h>
+#include <__concepts/same_as.h>
+#include <__concepts/semiregular.h>
+#include <__concepts/totally_ordered.h>
+#include <__iterator/concepts.h>
+#include <__iterator/incrementable_traits.h>
+#include <__iterator/iter_move.h>
+#include <__iterator/iterator_traits.h>
+#include <__memory/addressof.h>
+#include <__memory/pointer_traits.h>
+#include <__type_traits/common_reference.h>
+#include <__type_traits/common_type.h>
+#include <__type_traits/conditional.h>
+#include <__type_traits/integral_constant.h>
+#include <__type_traits/is_reference.h>
+#include <__type_traits/is_specialization.h>
+#include <__utility/forward.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
+
+template <indirectly_readable _Iter>
+using iter_const_reference_t = common_reference_t<const iter_value_t<_Iter>&&, iter_reference_t<_Iter>>;
+
+template <class _Iter>
+concept __constant_iterator = input_iterator<_Iter> && same_as<iter_const_reference_t<_Iter>, iter_reference_t<_Iter>>;
+
+template <input_iterator _Iter>
+class basic_const_iterator;
+
+template <input_iterator _Iter>
+using const_iterator = conditional_t<__constant_iterator<_Iter>, _Iter, basic_const_iterator<_Iter>>;
+template <semiregular _Sent>
+using const_sentinel = conditional_t<input_iterator<_Sent>, const_iterator<_Sent>, _Sent>;
+
+template <class _Iter>
+concept __not_a_const_iterator = !__is_specialization_v<_Iter, basic_const_iterator>;
+
+template <indirectly_readable _Iter>
+using __iter_const_rvalue_reference_t = common_reference_t<const iter_value_t<_Iter>&&, iter_rvalue_reference_t<_Iter>>;
+
+template <class _Iter>
+struct __basic_const_iterator_concept {
+  // clang-format off
+  using iterator_concept =
+    conditional_t<contiguous_iterator<_Iter>,
+      contiguous_iterator_tag,
+    conditional_t<random_access_iterator<_Iter>,
+      random_access_iterator_tag,
+    conditional_t<bidirectional_iterator<_Iter>,
+      bidirectional_iterator_tag,
+    conditional_t<forward_iterator<_Iter>,
+      forward_iterator_tag,
+    // else
+      input_iterator_tag>>>>;
+  // clang-format on
+};
+
+template <class _Iter>
+struct __basic_const_iterator_category : __basic_const_iterator_concept<_Iter> {};
+template <forward_iterator _Iter>
+struct __basic_const_iterator_category<_Iter> : __basic_const_iterator_concept<_Iter> {
+  using iterator_category = __basic_const_iterator_concept<_Iter>::iterator_concept;
+};
+
+template <input_iterator _Iter>
+class _LIBCPP_TEMPLATE_VIS basic_const_iterator : __basic_const_iterator_category<_Iter> {
+  _Iter __current = _Iter();
+
+  using __reference        = iter_const_reference_t<_Iter>;
+  using __rvalue_reference = __iter_const_rvalue_reference_t<_Iter>;
+
+public:
+  using value_type      = iter_value_t<_Iter>;
+  using difference_type = iter_difference_t<_Iter>;
+
+  _LIBCPP_HIDE_FROM_ABI basic_const_iterator()
+    requires default_initializable<_Iter>
+  = default;
+
+  _LIBCPP_HIDE_FROM_ABI constexpr basic_const_iterator(_Iter __cur) : __current(std::move(__cur)) {}
+  template <convertible_to<_Iter> _Type>
+  _LIBCPP_HIDE_FROM_ABI constexpr basic_const_iterator(basic_const_iterator<_Type> __cur)
+      : __current(std::move(__cur.__current)) {}
+  template <__different_from<basic_const_iterator> _Type>
+    requires convertible_to<_Type, _Iter>
+  _LIBCPP_HIDE_FROM_ABI constexpr basic_const_iterator(_Type&& __cur) : __current(std::forward<_Type>(__cur)) {}
+
+  _LIBCPP_HIDE_FROM_ABI constexpr const _Iter& base() const& noexcept { return __current; }
+  _LIBCPP_HIDE_FROM_ABI constexpr _Iter base() && { return std::move(__current); }
+
+  constexpr __reference operator*() const { return static_cast<__reference>(*__current); }
+  _LIBCPP_HIDE_FROM_ABI constexpr const auto* operator->() const
+    requires is_lvalue_reference_v<iter_reference_t<_Iter>> &&
+             same_as<remove_cvref_t<iter_reference_t<_Iter>>, value_type>
+  {
+    if constexpr (contiguous_iterator<_Iter>) {
+      return std::to_address(__current);
+    } else {
+      return std::addressof(*__current);
+    }
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr basic_const_iterator& operator++() {
+    ++__current;
+    return *this;
+  }
+  constexpr void operator++(int) { ++__current; }
+  _LIBCPP_HIDE_FROM_ABI constexpr basic_const_iterator operator++(int)
+    requires forward_iterator<_Iter>
+  {
+    auto __tmp = *this;
+    ++__current;
+    return __tmp;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr basic_const_iterator& operator--()
+    requires bidirectional_iterator<_Iter>
+  {
+    --__current;
+    return *this;
+  }
+  _LIBCPP_HIDE_FROM_ABI constexpr basic_const_iterator operator--(int)
+    requires bidirectional_iterator<_Iter>
+  {
+    auto __tmp = *this;
+    --__current;
+    return __tmp;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr basic_const_iterator& operator+=(difference_type __n)
+    requires random_access_iterator<_Iter>
+  {
+    __current += __n;
+    return *this;
+  }
+  _LIBCPP_HIDE_FROM_ABI constexpr basic_const_iterator& operator-=(difference_type __n)
+    requires random_access_iterator<_Iter>
+  {
+    __current -= __n;
+    return *this;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr __reference operator[](difference_type __n) const
+    requires random_access_iterator<_Iter>
+  {
+    return static_cast<__reference>(__current[__n]);
+  }
+
+  template <sentinel_for<_Iter> _Sent>
+  _LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const _Sent& __sent) const {
+    return __current == __sent;
+  }
+
+  template <__not_a_const_iterator _ConstIt>
+    requires __constant_iterator<_ConstIt> && convertible_to<_Iter const&, _ConstIt>
+  _LIBCPP_HIDE_FROM_ABI constexpr operator _ConstIt() const& {
+    return __current;
+  }
+  template <__not_a_const_iterator _ConstIt>
+    requires __constant_iterator<_ConstIt> && convertible_to<_Iter, _ConstIt>
+  _LIBCPP_HIDE_FROM_ABI constexpr operator _ConstIt() && {
+    return std::move(__current);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const basic_const_iterator& __rhs) const
+    requires random_access_iterator<_Iter>
+  {
+    return __current < __rhs.__current;
+  }
+  _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const basic_const_iterator& __rhs) const
+    requires random_access_iterator<_Iter>
+  {
+    return __current > __rhs.__current;
+  }
+  _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const basic_const_iterator& __rhs) const
+    requires random_access_iterator<_Iter>
+  {
+    return __current <= __rhs.__current;
+  }
+  _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const basic_const_iterator& __rhs) const
+    requires random_access_iterator<_Iter>
+  {
+    return __current >= __rhs.__current;
+  }
+  _LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(const basic_const_iterator& __rhs) const
+    requires random_access_iterator<_Iter> && three_way_comparable<_Iter>
+  {
+    return __current <=> __rhs.__current;
+  }
+
+  template <__different_from<basic_const_iterator> _Iter2>
+  _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const _Iter2& __rhs) const
+    requires random_access_iterator<_Iter> && totally_ordered_with<_Iter, _Iter2>
+  {
+    return __current < __rhs.__current;
+  }
+  template <__different_from<basic_const_iterator> _Iter2>
+  _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const _Iter2& __rhs) const
+    requires random_access_iterator<_Iter> && totally_ordered_with<_Iter, _Iter2>
+  {
+    return __current > __rhs.__current;
+  }
+  template <__different_from<basic_const_iterator> _Iter2>
+  _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const _Iter2& __rhs) const
+    requires random_access_iterator<_Iter> && totally_ordered_with<_Iter, _Iter2>
+  {
+    return __current <= __rhs.__current;
+  }
+  template <__different_from<basic_const_iterator> _Iter2>
+  _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const _Iter2& __rhs) const
+    requires random_access_iterator<_Iter> && totally_ordered_with<_Iter, _Iter2>
+  {
+    return __current >= __rhs.__current;
+  }
+  template <__different_from<basic_const_iterator> _Iter2>
+  _LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(const _Iter2& __rhs) const
+    requires random_access_iterator<_Iter> && totally_ordered_with<_Iter, _Iter2> &&
+             three_way_comparable_with<_Iter, _Iter2>
+  {
+    return __current <=> __rhs.__current;
+  }
+
+  template <__not_a_const_iterator _Iter2>
+  friend _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const _Iter2& __lhs, const basic_const_iterator& __rhs)
+    requires random_access_iterator<_Iter> && totally_ordered_with<_Iter, _Iter2>
+  {
+    return __lhs < __rhs.__current;
+  }
+  template <__not_a_const_iterator _Iter2>
+  friend _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const _Iter2& __lhs, const basic_const_iterator& __rhs)
+    requires random_access_iterator<_Iter> && totally_ordered_with<_Iter, _Iter2>
+  {
+    return __lhs > __rhs.__current;
+  }
+  template <__not_a_const_iterator _Iter2>
+  friend _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const _Iter2& __lhs, const basic_const_iterator& __rhs)
+    requires random_access_iterator<_Iter> && totally_ordered_with<_Iter, _Iter2>
+  {
+    return __lhs <= __rhs.__current;
+  }
+  template <__not_a_const_iterator _Iter2>
+  friend _LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const _Iter2& __lhs, const basic_const_iterator& __rhs)
+    requires random_access_iterator<_Iter> && totally_ordered_with<_Iter, _Iter2>
+  {
+    return __lhs >= __rhs.__current;
+  }
+
+  friend _LIBCPP_HIDE_FROM_ABI constexpr basic_const_iterator
+  operator+(const basic_const_iterator& __it, difference_type __n)
+    requires random_access_iterator<_Iter>
+  {
+    return basic_const_iterator(__it.__current + __n);
+  }
+  friend _LIBCPP_HIDE_FROM_ABI constexpr basic_const_iterator
+  operator+(difference_type __n, const basic_const_iterator& __it)
+    requires random_access_iterator<_Iter>
+  {
+    return basic_const_iterator(__it.__current + __n);
+  }
+
+  friend _LIBCPP_HIDE_FROM_ABI constexpr basic_const_iterator
+  operator-(const basic_const_iterator& __it, difference_type __n)
+    requires random_access_iterator<_Iter>
+  {
+    return basic_const_iterator(__it.__current - __n);
+  }
+  template <sized_sentinel_for<_Iter> _Sent>
+  _LIBCPP_HIDE_FROM_ABI constexpr difference_type operator-(const _Sent& __rhs) const {
+    return __current - __rhs;
+  }
+  template <__not_a_const_iterator _Sent>
+    requires sized_sentinel_for<_Sent, _Iter>
+  friend _LIBCPP_HIDE_FROM_ABI constexpr difference_type
+  operator-(const _Sent& __lhs, const basic_const_iterator& __rhs) {
+    return __lhs - __rhs;
+  }
+
+  friend _LIBCPP_HIDE_FROM_ABI constexpr __rvalue_reference iter_move(const basic_const_iterator& __it) noexcept(
+      noexcept(static_cast<__rvalue_reference>(ranges::iter_move(__it.current_)))) {
+    return static_cast<__rvalue_reference>(ranges::iter_move(__it.current_));
+  }
+};
+
+template <class _Type1, common_with<_Type1> _Type2>
+  requires input_iterator<common_type_t<_Type1, _Type2>>
+struct common_type<basic_const_iterator<_Type1>, _Type2> {
+  using type = basic_const_iterator<common_type_t<_Type1, _Type2>>;
+};
+template <class _Type1, common_with<_Type1> _Type2>
+  requires input_iterator<common_type_t<_Type1, _Type2>>
+struct common_type<_Type2, basic_const_iterator<_Type1>> {
+  using type = basic_const_iterator<common_type_t<_Type1, _Type2>>;
+};
+template <class _Type1, common_with<_Type1> _Type2>
+  requires input_iterator<common_type_t<_Type1, _Type2>>
+struct common_type<basic_const_iterator<_Type1>, basic_const_iterator<_Type2>> {
+  using type = basic_const_iterator<common_type_t<_Type1, _Type2>>;
+};
+
+template <input_iterator _Iter>
+_LIBCPP_HIDE_FROM_ABI constexpr const_iterator<_Iter> make_const_iterator(_Iter __it) {
+  return __it;
+}
+template <semiregular _Sent>
+_LIBCPP_HIDE_FROM_ABI constexpr const_sentinel<_Sent> make_const_sentinel(_Sent __sent) {
+  return __sent;
+}
+
+#endif // _LIBCPP_STD_VER >= 23
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___ITERATOR_CONST_ITERATOR_H
diff --git a/libcxx/include/__ranges/access.h b/libcxx/include/__ranges/access.h
index c0a40c5e10178..7955b3b2166dc 100644
--- a/libcxx/include/__ranges/access.h
+++ b/libcxx/include/__ranges/access.h
@@ -148,61 +148,6 @@ inline constexpr auto end = __end::__fn{};
 } // namespace __cpo
 } // namespace ranges
 
-// [range.access.cbegin]
-
-namespace ranges {
-namespace __cbegin {
-struct __fn {
-  template <class _Tp>
-    requires is_lvalue_reference_v<_Tp&&>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
-      noexcept(noexcept(ranges::begin(static_cast<const remove_reference_t<_Tp>&>(__t))))
-          -> decltype(ranges::begin(static_cast<const remove_reference_t<_Tp>&>(__t))) {
-    return ranges::begin(static_cast<const remove_reference_t<_Tp>&>(__t));
-  }
-
-  template <class _Tp>
-    requires is_rvalue_reference_v<_Tp&&>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
-      noexcept(noexcept(ranges::begin(static_cast<const _Tp&&>(__t))))
-          -> decltype(ranges::begin(static_cast<const _Tp&&>(__t))) {
-    return ranges::begin(static_cast<const _Tp&&>(__t));
-  }
-};
-} // namespace __cbegin
-
-inline namespace __cpo {
-inline constexpr auto cbegin = __cbegin::__fn{};
-} // namespace __cpo
-} // namespace ranges
-
-// [range.access.cend]
-
-namespace ranges {
-namespace __cend {
-struct __fn {
-  template <class _Tp>
-    requires is_lvalue_reference_v<_Tp&&>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
-      noexcept(noexcept(ranges::end(static_cast<const remove_reference_t<_Tp>&>(__t))))
-          -> decltype(ranges::end(static_cast<const remove_reference_t<_Tp>&>(__t))) {
-    return ranges::end(static_cast<const remove_reference_t<_Tp>&>(__t));
-  }
-
-  template <class _Tp>
-    requires is_rvalue_reference_v<_Tp&&>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const noexcept(
-      noexcept(ranges::end(static_cast<const _Tp&&>(__t)))) -> decltype(ranges::end(static_cast<const _Tp&&>(__t))) {
-    return ranges::end(static_cast<const _Tp&&>(__t));
-  }
-};
-} // namespace __cend
-
-inline namespace __cpo {
-inline constexpr auto cend = __cend::__fn{};
-} // namespace __cpo
-} // namespace ranges
-
 #endif // _LIBCPP_STD_VER >= 20
 
 _LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/include/__ranges/concepts.h b/libcxx/include/__ranges/concepts.h
index 674a3f359ff99..19264d4ac76c3 100644
--- a/libcxx/include/__ranges/concepts.h
+++ b/libcxx/include/__ranges/concepts.h
@@ -15,6 +15,7 @@
 #include <__concepts/same_as.h>
 #include <__config>
 #include <__iterator/concepts.h>
+#include <__iterator/const_iterator.h>
 #include <__iterator/incrementable_traits.h>
 #include <__iterator/iter_move.h>
 #include <__iterator/iterator_traits.h>
@@ -61,6 +62,11 @@ concept borrowed_range =
 template <range _Rp>
 using sentinel_t = decltype(ranges::end(std::declval<_Rp&>()));
 
+#  if _LIBCPP_STD_VER >= 23
+template <range _Rp>
+using const_iterator_t = const_iterator<iterator_t<_Rp>>;
+#  endif // _LIBCPP_STD_VER >= 23
+
 template <range _Rp>
 using range_difference_t = iter_difference_t<iterator_t<_Rp>>;
 
@@ -70,6 +76,9 @@ using range_value_t = iter_value_t<iterator_t<_Rp>>;
 template <range _Rp>
 using range_reference_t = iter_reference_t<iterator_t<_Rp>>;
 
+template <range _Rp>
+using range_const_reference_t = iter_const_reference_t<iterator_t<_Rp>>;
+
 template <range _Rp>
 using range_rvalue_reference_t = iter_rvalue_reference_t<iterator_t<_Rp>>;
 
@@ -133,6 +142,11 @@ concept viewable_range =
       (is_lvalue_reference_v<_Tp> ||
        (movable<remove_reference_t<_Tp>> && !__is_std_initializer_list<remove_cvref_t<_Tp>>))));
 
+#  if _LIBCPP_STD_VER >= 23
+template <class _Tp>
+concept constant_range = input_range<_Tp> && __constant_iterator<iterator_t<_Tp>>;
+#  endif // _LIBCPP_STD_VER >= 23
+
 } // namespace ranges
 
 #endif // _LIBCPP_STD_VER >= 20
diff --git a/libcxx/include/__ranges/const_access.h b/libcxx/include/__ranges/const_access.h
new file mode 100644
index 0000000000000..df2811839bc46
--- /dev/null
+++ b/libcxx/include/__ranges/const_access.h
@@ -0,0 +1,208 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// 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___RANGES_CONST_ACCESS_H
+#define _LIBCPP___RANGES_CONST_ACCESS_H
+
+#include <__iterator/const_iterator.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <__ranges/enable_borrowed_range.h>
+#include <__ranges/rbegin.h>
+#include <__ranges/rend.h>
+#include <__type_traits/is_reference.h>
+#include <__type_traits/remove_cv.h>
+#include <__type_traits/remove_reference.h>
+#include <__utility/declval.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 20
+
+// [range.const]
+#  if _LIBCPP_STD_VER >= 23
+namespace ranges {
+template <input_range _Rp>
+constexpr auto& __possibly_const_range(_Rp& __rng) {
+  if constexpr (constant_range<const _Rp> ...
[truncated]

``````````

</details>


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


More information about the libcxx-commits mailing list