[gcc r13-9710] libstdc++: Implement P2836R1 changes to const_iterator
Patrick Palka
ppalka@gcc.gnu.org
Tue May 27 20:31:00 GMT 2025
https://gcc.gnu.org/g:d6551d6d83edf3ee07eca4baeb0cc3f6050af645
commit r13-9710-gd6551d6d83edf3ee07eca4baeb0cc3f6050af645
Author: Patrick Palka <ppalka@redhat.com>
Date: Mon Jan 15 15:13:53 2024 -0500
libstdc++: Implement P2836R1 changes to const_iterator
libstdc++-v3/ChangeLog:
* include/bits/stl_iterator.h (const_iterator): Define conversion
operators as per P2836R1.
* include/std/ranges (__cpp_lib_ranges_as_const): Update value.
* include/std/version (__cpp_lib_ranges_as_const): Likewise.
* testsuite/24_iterators/const_iterator/1.cc (test04): New test.
* testsuite/std/ranges/adaptors/as_const/1.cc: Adjust expected
value of __cpp_lib_ranges_as_const.
* testsuite/std/ranges/version_c++23.cc: Likewise.
Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
(cherry picked from commit 731444b3c39e3dc3dd8778f430a38742861dcca1)
Diff:
---
libstdc++-v3/include/bits/stl_iterator.h | 12 ++++++++++++
libstdc++-v3/include/std/ranges | 2 +-
libstdc++-v3/include/std/version | 2 +-
.../testsuite/24_iterators/const_iterator/1.cc | 22 ++++++++++++++++++++++
.../testsuite/std/ranges/adaptors/as_const/1.cc | 2 +-
libstdc++-v3/testsuite/std/ranges/version_c++23.cc | 2 +-
6 files changed, 38 insertions(+), 4 deletions(-)
diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h
index 68bb1023081a..a6b55500be98 100644
--- a/libstdc++-v3/include/bits/stl_iterator.h
+++ b/libstdc++-v3/include/bits/stl_iterator.h
@@ -2798,6 +2798,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
noexcept(noexcept(_M_current == __s))
{ return _M_current == __s; }
+ template<__detail::__not_a_const_iterator _CIt>
+ requires __detail::__constant_iterator<_CIt> && convertible_to<_It, _CIt>
+ constexpr
+ operator _CIt() const&
+ { return _M_current; }
+
+ template<__detail::__not_a_const_iterator _CIt>
+ requires __detail::__constant_iterator<_CIt> && convertible_to<_It, _CIt>
+ constexpr
+ operator _CIt() &&
+ { return std::move(_M_current); }
+
constexpr bool
operator<(const basic_const_iterator& __y) const
noexcept(noexcept(_M_current < __y._M_current))
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 7bf4746973ff..daf3f32b808c 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -8953,7 +8953,7 @@ namespace views::__adaptor
inline constexpr _Enumerate enumerate;
}
-#define __cpp_lib_ranges_as_const 202207L
+#define __cpp_lib_ranges_as_const 202311L
template<view _Vp>
requires input_range<_Vp>
diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version
index ee515c4e66ca..7376f8eed4d4 100644
--- a/libstdc++-v3/include/std/version
+++ b/libstdc++-v3/include/std/version
@@ -341,7 +341,7 @@
#define __cpp_lib_ranges_stride 202207L
#define __cpp_lib_ranges_cartesian_product 202207L
#define __cpp_lib_ranges_as_rvalue 202207L
-#define __cpp_lib_ranges_as_const 202207L
+#define __cpp_lib_ranges_as_const 202311L
#define __cpp_lib_ranges_enumerate 202302L
#define __cpp_lib_ranges_contains 202207L
#define __cpp_lib_ranges_iota 202202L
diff --git a/libstdc++-v3/testsuite/24_iterators/const_iterator/1.cc b/libstdc++-v3/testsuite/24_iterators/const_iterator/1.cc
index 51befd295410..81e2e69ce39d 100644
--- a/libstdc++-v3/testsuite/24_iterators/const_iterator/1.cc
+++ b/libstdc++-v3/testsuite/24_iterators/const_iterator/1.cc
@@ -2,6 +2,7 @@
// { dg-do run { target c++23 } }
#include <iterator>
+#include <ranges>
#include <array>
#include <concepts>
#include <string_view>
@@ -98,6 +99,26 @@ test03()
std::unreachable_sentinel_t> );
}
+void
+test04()
+{
+ // Example from P2836R1
+ auto f = [](std::vector<int>::const_iterator i) {};
+
+ auto v = std::vector<int>();
+ {
+ auto i1 = ranges::cbegin(v); // returns vector<T>::const_iterator
+ f(i1); // okay
+ }
+
+ auto t = v | std::views::take_while([](int const x) { return x < 100; });
+ {
+ auto i2 = ranges::cbegin(t); // returns basic_const_iterator<vector<T>::iterator>
+ f(i2); // was an error in C++23 before P2836R1
+ f(std::move(i2)); // same
+ }
+}
+
int
main()
{
@@ -137,4 +158,5 @@ main()
test02<const std::vector<bool>, true>();
test03();
+ test04();
}
diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/as_const/1.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/as_const/1.cc
index ac1be7440e47..7bafc752f50a 100644
--- a/libstdc++-v3/testsuite/std/ranges/adaptors/as_const/1.cc
+++ b/libstdc++-v3/testsuite/std/ranges/adaptors/as_const/1.cc
@@ -9,7 +9,7 @@
#include <testsuite_hooks.h>
#include <testsuite_iterators.h>
-#if __cpp_lib_ranges_as_const != 202207L
+#if __cpp_lib_ranges_as_const != 202311L
# error "Feature-test macro __cpp_lib_ranges_as_const has wrong value in <ranges>"
#endif
diff --git a/libstdc++-v3/testsuite/std/ranges/version_c++23.cc b/libstdc++-v3/testsuite/std/ranges/version_c++23.cc
index e8342fa986a8..11c51c948219 100644
--- a/libstdc++-v3/testsuite/std/ranges/version_c++23.cc
+++ b/libstdc++-v3/testsuite/std/ranges/version_c++23.cc
@@ -45,7 +45,7 @@
# error "Feature-test macro __cpp_lib_ranges_as_rvalue has wrong value in <version>"
#endif
-#if __cpp_lib_ranges_as_const != 202207L
+#if __cpp_lib_ranges_as_const != 202311L
# error "Feature-test macro __cpp_lib_ranges_as_const has wrong value in <version>"
#endif
More information about the Libstdc++-cvs
mailing list