libstdc++: Implement P2836R1 changes to const_iterator

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.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
This commit is contained in:
Patrick Palka 2024-01-15 15:13:53 -05:00
parent 8108c6be99
commit 731444b3c3
6 changed files with 39 additions and 5 deletions

View File

@ -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))

View File

@ -1548,7 +1548,7 @@ ftms = {
ftms = {
name = ranges_as_const;
values = {
v = 202207;
v = 202311;
cxxmin = 23;
};
};

View File

@ -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) */

View File

@ -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();
}

View File

@ -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

View File

@ -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