libstdc++: Implement P2836R1 changes to const_iterator
Checks
Commit Message
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(-)
@@ -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))
@@ -1548,7 +1548,7 @@ ftms = {
ftms = {
name = ranges_as_const;
values = {
- v = 202207;
+ v = 202311;
cxxmin = 23;
};
};
@@ -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) */
@@ -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();
}
@@ -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
@@ -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