[libcxx] cbegin should always return a constant iterator #99915
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.
<!--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:
````````````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>
No change requested for these conversion functions. But the entry for P2836R1 in Cxx2cPapers.csv needs to be updated (to Complete, 20.0?), and the description of this PR should also mention that paper.
https://github.com/llvm/llvm-project/blob/ac1a1e5797388598201511d17f05aa088ef4a2e2/libcxx/docs/Status/Cxx2cPapers.csv#L45
Edit: Oops, the __cpp_lib_ranges_as_const feature-test macro can't be set yet, so the status is not yet (strictly) complete.
Mmh, thanks! I was reading the C++ draft, rather than the paper, and I missed that this was changed in a later paper.
This one definitely looks like a feature to me, do you have additional context on why the other implementations did this as a DR?
This one definitely looks like a feature to me, do you have additional context on why the other implementations did this as a DR?
As @frederick-vs-ja points out, the DR was a DR to this feature; the original paper (P2278R4) was not implemented as a DR.
@strega-nil Can you update the test test/std/containers/views/views.span/span.iterators/iterator.pass.cpp. I recently added it with some ifdefs since we didn't have this paper implemented. The ifdef should be adjusted to the proper language version; depending whether or not this is a DR.
@mordante can do! I hope to write tests on Monday so I actually get this going again haha
Per the policy confirmed in (?) #101852, should we just say P2836R1 is Complete?
I'm happy with "Complete with a note"
Okay, this should be complete (ish) with testing - ready for real review!
While testing this code, I found some _very_ weird behaviour in clang that seems (to me, at least) like a bug: https://gcc.godbolt.org/z/GrnbEa411
LWG3853 is completed here, which should be reflected in this entry:
https://github.com/llvm/llvm-project/blob/445023f1739d3764f1dae6e6b7b0602328198a1e/libcxx/docs/Status/Cxx23Issues.csv#L274
Should we implement LWG3946 together? (Or is it intended to leave it to be done in another PR?)
https://github.com/llvm/llvm-project/blob/445023f1739d3764f1dae6e6b7b0602328198a1e/libcxx/docs/Status/Cxx2cIssues.csv#L28
Requested changes.
Oh, I was somehow confused by the new PR... The entry for LWG3766 should also be updated in this PR:
https://github.com/llvm/llvm-project/blob/445023f1739d3764f1dae6e6b7b0602328198a1e/libcxx/docs/Status/Cxx23Issues.csv#L223
Requested changes.
typo: applies -> applied
@strega-nil There were lots of conflicts due to some changes I've made to the status pages. I should have resolved everything and this should be ready for review IIUC.
I am a bit confused: the PR description mentions that the changes to span and a few other things are not implemented in this PR, but this is marked as complete. Can we instead implement all the changes in this PR?
Also, since this is a C++23 paper, it seems wrong to say that the paper is being applied as a DR against C++23. It's just a C++23 paper implemented in C++23 (and above). Or did I miss something?
Similarly here, I don't fully understand what part of this paper is not being implemented.
Could you confirm that you've gone through these LWG issues and made sure that we have test coverage for each of them? For example, I would expect a test that uses basic_const_iterator<volatile int*>::operator-> to guard against LWG3853.
auto __ret = ranges::copy(ranges::__reverse_range(__range), std::move(__result));
Do you have a test that this overload is noexcept?
Is there a test for noexcept?
I think the remove_reference.h header above can also be removed.
I'm a bit confused by these #ifs. IIUC that's because we're not implementing this as a DR in C++20, so the #else branch is basically the code as it was before your patch. Correct?
Same for remove_reference.h include.
Same for remove_reference.h include.
We need to also actually call these new methods at runtime.
Can we avoid closing and reopening the ranges namespace here and above? IMO that would make the code a bit lighter.
concept constant_range = see below; // since C++23
We should have a test dedicated to this concept, not only the ones here and in cend.pass.cpp.
General comment about coverage here: we have a lot of compile-only tests (e.g. static_assert(decltype(...))), but we have very few tests that actually run. Almost all tests that are not negative (e.g. not tests that do !invocable<...> to make sure we SFINAE out) could also be run to ensure we properly exercise these code paths at runtime.
I think it's probably overkill to run everything depending on the complexity it adds, but I'll leave that comment here and let you look into ways it can be addressed while being realistic, maintainable, etc. I don't want to prescribe one way of doing it, but the goal here would be to ensure that we actually hit these code paths at runtime.
This test could be templated on the iterator to make sure that we exercise it with all iterator categories.
Thanks for the patch! I left some comments after my initial review. I am still a bit confused by the DR and partial implementation of this paper, so please bear with me if some of my questions seem dumb :-).
The only missing part is bumping of the feature-test macro, because the FTM itself can't be added at this moment.
Also, since this is a C++23 paper, it seems wrong to say that the paper is being applied as a DR against C++23. It's just a C++23 paper implemented in C++23 (and above). Or did I miss something?
Looks like that something was mistakenly duplicated in force-pushing. It was right in 160a204f24092aeffcb9674ce4c3093949301535.
"`P2278R4 <https://wg21.link/P2278R4>`__","``cbegin`` should always return a constant iterator","2022-07 (Virtual)","|In Progress|","20.0","``basic_const_iterator`` has been implemented, and the const range accessors (``cbegin``, ``cend``, ``crbegin``, ``crend``) have been updated to return this new type. However, the changes to ``cdata`` and ``span``, and ``const_view``, have not yet been implemented."
I think we can and should just merge the changes in #102301 to this PR. It's less confusing to me to have a unified PR completing this paper (and P2836R1, and several LWG issues).
As to your first question, originally the plan was to implement this in two parts for ease of review; however, since I've been waiting for review for so long, I did end up implementing the other parts of the paper. I can certainly merge that PR into this one.
On the second point... I need to look through this and figure out exactly what's going on, it's been a while since I've looked.
I've pushed a merge with main to resolve merge conflicts.
Some coverages (for incomplete types and volatile) in these 3 files are no longer supported. Perhaps we need some guards.
@strega-nil @ldionne I pushed small changes to resolve CI failures. Please double check that there are no undesired changes.
@frederick-vs-ja I spoke with @strega-nil a few weeks ago, who won't have time to push this through the finish line. If you'd like to pick this up, let me know once this is ready to re-review and I can commit some time to it.
@ldionne @strega-nil I think I'll pick this up in this or the next week.
<!--LLVM CODE FORMAT COMMENT: {darker}-->
:white_check_mark: With the latest revision this PR passed the Python code formatter.
Could someone give me a quick update on where things currently stand, what's actively being worked on, and which areas still need attention?
I believe The changes in product code are completed. But some stuffs are organized in a weird way. I think we still need to
basic_const_iterator.I’ve started small addition to basic_const_iterator test coverage here: https://github.com/strega-nil/llvm-project/pull/1.
My plan would be to start incrementally, following the suggested counted_iterator test structure. Feedback on the approach would be welcome before taking on larger chunks.
If this isn’t useful at the moment, no worries—feel free to leave it aside.
frederick-vs-ja
strega-nil
Papers:
- [P2278R4][]:
cbeginshould always return a constant iterator- [P2836R1][]:
std::basic_const_iteratorshould follow itsunderlying type's convertibility
Issues:
- [LWG3765][]:
const_sentinelshould be constrained- [LWG3766][]:
view_interface::cbeginis underconstrained- [LWG3770][]:
const_sentinel_tis missing- [LWG3769][]:
basic_const_iterator::operator==causes infiniteconstraint recursion
- [LWG3811][]:
views::as_constonref_view<T>should returnref_view<const T>- [LWG3850][]:
views::as_constonempty_view<T>should returnempty_view<const T>- [LWG3853][]:
basic_const_iterator<volatile int*>::operator->isill-formed
- [LWG3862][]:
basic_const_iterator'scommon_typespecializationis underconstrained
- [LWG3872][]:
basic_const_iteratorshould have customiter_move- [LWG3946][]: The definition of
const_iterator_tshould be reworked- [LWG3948][]: _
possibly-const-range_ and _as-const-pointer_should be
noexcept- [LWG4027][]: _
possibly-const-range_ should prefer returningconst R&- [LWG4218][]: Constraint recursion in
basic_const_iterator's relationaloperators due to ADL + CWG 2369
- [LWG4253][]:
basic_const_iteratorshould provideiterator_typeImplements
basic_const_iterator,const_iterator, andconst_sentinelranges::{cbegin,cend,crbegin,crend}cbeginandcendmember functions toview_interfaceranges::as_const_view,ranges::views::as_constspancdataDrive-by:
[[nodiscard]]wherever appropriate.[P2278R4]: https://wg21.link/p2278r4
[P2836R1]: https://wg21.link/p2836r1
[LWG3765]: https://wg21.link/lwg3765
[LWG3766]: https://wg21.link/lwg3766
[LWG3770]: https://wg21.link/lwg3770
[LWG3769]: https://wg21.link/lwg3769
[LWG3811]: https://wg21.link/lwg3811
[LWG3850]: https://wg21.link/lwg3850
[LWG3853]: https://wg21.link/lwg3853
[LWG3862]: https://wg21.link/lwg3862
[LWG3872]: https://wg21.link/lwg3872
[LWG3946]: https://wg21.link/lwg3946
[LWG3948]: https://wg21.link/lwg3948
[LWG4027]: https://wg21.link/lwg4027
[LWG4218]: https://wg21.link/lwg4218
[LWG4253]: https://wg21.link/lwg4253
Closes #105201
Closes #105406
Closes #105032
Closes #105033
Closes #105034
Closes #105069
Closes #105071
Closes #105088
Closes #105090
Closes #105116
Closes #105123
Closes #105298
Closes #105300
Closes #118342
Closes #171319
Closes #204445