From: Patrick Palka Date: Fri, 28 Nov 2025 20:38:04 +0000 (-0500) Subject: libstdc++: Correctly implement LWG 3946 changes to const_iterator_t [PR122842] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2d3142c00934c419755c17dd85ecdb0e72f249d1;p=thirdparty%2Fgcc.git libstdc++: Correctly implement LWG 3946 changes to const_iterator_t [PR122842] LWG 3946 made const_iterator_t/sentinel_t agree with ranges::cbegin/cend by defining the aliases in terms of the CPOs, but I defined it the other way around in an incorrect way that made the aliases not consider range-ness of const T via __possibly_const_range. This patch reimplements the proposed resolution in a more obviously correct way, mirroring the wording. PR libstdc++/122842 libstdc++-v3/ChangeLog: * include/bits/ranges_base.h (__access:_CBegin): Define in terms of const_iterator directly, not const_iterator_t. (__access::_CEnd): Likewise in terms of const_sentinel vs const_sentinel_t. (const_iterator_t): Move down definition and define in terms of ranges::cbegin as per LWG 3946. (const_sentinel_t): Likewise in terms of ranges::cend. * testsuite/24_iterators/const_iterator/1.cc (test02): Correct test for int[], std::array and std::vector. Also test std::string. Reviewed-by: Tomasz KamiƄski Reviewed-by: Jonathan Wakely --- diff --git a/libstdc++-v3/include/bits/ranges_base.h b/libstdc++-v3/include/bits/ranges_base.h index 1c4bf432c8f..0b8151cd65d 100644 --- a/libstdc++-v3/include/bits/ranges_base.h +++ b/libstdc++-v3/include/bits/ranges_base.h @@ -525,11 +525,7 @@ namespace ranges using sentinel_t = decltype(ranges::end(std::declval<_Range&>())); #if __glibcxx_ranges_as_const // >= C++23 - template - using const_iterator_t = const_iterator>; - - template - using const_sentinel_t = const_sentinel>; + // const_iterator_t and const_sentinel_t defined below. template using range_const_reference_t = iter_const_reference_t>; @@ -683,7 +679,7 @@ namespace ranges (ranges::begin(__access::__possibly_const_range(__t))); } { auto& __r = __access::__possibly_const_range(__t); - return const_iterator_t(ranges::begin(__r)); + return const_iterator(ranges::begin(__r)); } #else template @@ -711,7 +707,7 @@ namespace ranges (ranges::end(__access::__possibly_const_range(__t))); } { auto& __r = __access::__possibly_const_range(__t); - return const_sentinel_t(ranges::end(__r)); + return const_sentinel(ranges::end(__r)); } #else template @@ -815,6 +811,16 @@ namespace ranges inline constexpr ranges::__access::_CData cdata{}; } +#if __glibcxx_ranges_as_const // >= C++23 + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3946. The definition of const_iterator_t should be reworked + template + using const_iterator_t = decltype(ranges::cbegin(std::declval<_Range&>())); + + template + using const_sentinel_t = decltype(ranges::cend(std::declval<_Range&>())); +#endif + namespace __detail { template diff --git a/libstdc++-v3/testsuite/24_iterators/const_iterator/1.cc b/libstdc++-v3/testsuite/24_iterators/const_iterator/1.cc index fe952bfad14..f2bcad4f09c 100644 --- a/libstdc++-v3/testsuite/24_iterators/const_iterator/1.cc +++ b/libstdc++-v3/testsuite/24_iterators/const_iterator/1.cc @@ -42,12 +42,13 @@ test01() } } -template +template void test02() { if constexpr (Const) { + static_assert(Constable); static_assert( ranges::constant_range ); static_assert( std::same_as, ranges::iterator_t> ); static_assert( std::same_as, ranges::sentinel_t> ); @@ -64,9 +65,21 @@ test02() static_assert( !ranges::constant_range ); using Wrapped = std::basic_const_iterator>; - static_assert( std::same_as, Wrapped> ); - if constexpr (ranges::common_range) - static_assert( std::same_as, Wrapped> ); + if constexpr (Constable) + { + // Verify LWG 3946 changes to const_iterator/sentinel_t (PR122842). + static_assert( std::same_as, + ranges::iterator_t> ); + static_assert( std::same_as, + ranges::sentinel_t> ); + } + else + { + static_assert( std::same_as, Wrapped> ); + if constexpr (ranges::common_range) + static_assert( std::same_as, Wrapped> ); + } + static_assert( std::same_as, std::iter_reference_t> ); @@ -138,13 +151,14 @@ main() test01(); test01::const_iterator, true>(); - test02(); + test02(); test02, false>(); test02, false>(); test02, false>(); test02, false>(); - test02, false>(); - test02, false>(); + test02, false, true>(); + test02, false, true>(); + test02(); test02(); test02, true>(); @@ -155,6 +169,7 @@ main() test02, true>(); test02(); test02, true>(); + test02(); test03(); test04();