libstdc++: Implement P2836R1 changes to const_iterator

Message ID 20240115184920.2752407-1-ppalka@redhat.com
State Unresolved
Headers
Series libstdc++: Implement P2836R1 changes to const_iterator |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Patrick Palka Jan. 15, 2024, 6:49 p.m. UTC
  Tested on x86_64-pc-linux-gnu, does this look OK for trunk/13?

libstdc++-v3/ChangeLog:

	* include/bits/stl_iterator.h (const_iterator): Define
	conversion operators as per P2836R1.
	* include/bits/version.def (ranges_as_const): Update value.
	* include/bits/version.h: Regenerate.
	* 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.
---
 libstdc++-v3/include/bits/stl_iterator.h      | 12 ++++++++++
 libstdc++-v3/include/bits/version.def         |  2 +-
 libstdc++-v3/include/bits/version.h           |  4 ++--
 .../24_iterators/const_iterator/1.cc          | 22 +++++++++++++++++++
 .../std/ranges/adaptors/as_const/1.cc         |  2 +-
 .../testsuite/std/ranges/version_c++23.cc     |  2 +-
 6 files changed, 39 insertions(+), 5 deletions(-)
  

Patch

diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h
index 6434ef64750..d71a793e10d 100644
--- a/libstdc++-v3/include/bits/stl_iterator.h
+++ b/libstdc++-v3/include/bits/stl_iterator.h
@@ -2775,6 +2775,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/bits/version.def b/libstdc++-v3/include/bits/version.def
index 21cdc65121b..afbec6c3e6a 100644
--- a/libstdc++-v3/include/bits/version.def
+++ b/libstdc++-v3/include/bits/version.def
@@ -1548,7 +1548,7 @@  ftms = {
 ftms = {
   name = ranges_as_const;
   values = {
-    v = 202207;
+    v = 202311;
     cxxmin = 23;
   };
 };
diff --git a/libstdc++-v3/include/bits/version.h b/libstdc++-v3/include/bits/version.h
index f8dd16416a4..9688b246ef4 100644
--- a/libstdc++-v3/include/bits/version.h
+++ b/libstdc++-v3/include/bits/version.h
@@ -1875,9 +1875,9 @@ 
 // from version.def line 1549
 #if !defined(__cpp_lib_ranges_as_const)
 # if (__cplusplus >= 202100L)
-#  define __glibcxx_ranges_as_const 202207L
+#  define __glibcxx_ranges_as_const 202311L
 #  if defined(__glibcxx_want_all) || defined(__glibcxx_want_ranges_as_const)
-#   define __cpp_lib_ranges_as_const 202207L
+#   define __cpp_lib_ranges_as_const 202311L
 #  endif
 # endif
 #endif /* !defined(__cpp_lib_ranges_as_const) && defined(__glibcxx_want_ranges_as_const) */
diff --git a/libstdc++-v3/testsuite/24_iterators/const_iterator/1.cc b/libstdc++-v3/testsuite/24_iterators/const_iterator/1.cc
index 8b74d110fdf..fe952bfad14 100644
--- a/libstdc++-v3/testsuite/24_iterators/const_iterator/1.cc
+++ b/libstdc++-v3/testsuite/24_iterators/const_iterator/1.cc
@@ -1,6 +1,7 @@ 
 // { dg-do run { target c++23 } }
 
 #include <iterator>
+#include <ranges>
 #include <array>
 #include <concepts>
 #include <string_view>
@@ -97,6 +98,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()
 {
@@ -136,4 +157,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 2d36e0a4712..c36786a8c5f 100644
--- a/libstdc++-v3/testsuite/std/ranges/adaptors/as_const/1.cc
+++ b/libstdc++-v3/testsuite/std/ranges/adaptors/as_const/1.cc
@@ -3,7 +3,7 @@ 
 
 #include <ranges>
 
-#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 823264f32aa..d475d3dc114 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