From ed96a6cd79b206b39d8604996c39345fd495f397 Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Sat, 22 Jan 2022 14:41:42 -0800 Subject: [PATCH] Fix handling of arrays in two-argument `ranges::distance` .... which was broken by LWG-3392. This implements the proposed resolution of a submitted-but-not-yet-numbered LWG issue. --- stl/inc/xutility | 6 +++--- .../tests/P0896R4_ranges_iterator_machinery/test.cpp | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/stl/inc/xutility b/stl/inc/xutility index b1e218a031..b787d1ae84 100644 --- a/stl/inc/xutility +++ b/stl/inc/xutility @@ -2843,7 +2843,7 @@ namespace ranges { using _Not_quite_object::_Not_quite_object; // clang-format off - template _Se> + template _Se> requires (!sized_sentinel_for<_Se, _It>) _NODISCARD constexpr iter_difference_t<_It> operator()(_It _First, _Se _Last) const { // clang-format on @@ -2851,8 +2851,8 @@ namespace ranges { return _Distance_unchecked(_Get_unwrapped(_STD move(_First)), _Get_unwrapped(_STD move(_Last))); } - template _Se> - _NODISCARD constexpr iter_difference_t<_It> operator()(const _It& _First, const _Se& _Last) const + template > _Se> + _NODISCARD constexpr iter_difference_t<_It> operator()(const _It& _First, _Se _Last) const noexcept(noexcept(_Last - _First)) /* strengthened */ { return _Last - _First; } diff --git a/tests/std/tests/P0896R4_ranges_iterator_machinery/test.cpp b/tests/std/tests/P0896R4_ranges_iterator_machinery/test.cpp index 1479dc3c5b..e679f52996 100644 --- a/tests/std/tests/P0896R4_ranges_iterator_machinery/test.cpp +++ b/tests/std/tests/P0896R4_ranges_iterator_machinery/test.cpp @@ -3474,9 +3474,19 @@ namespace vso1121031 { STATIC_ASSERT(!has_member_value_type>>); } // namespace vso1121031 +constexpr bool test_lwg_XXX() { + int a[3] = {}; + assert(ranges::distance(a, a + 3) == 3); + assert(ranges::distance(a + 3, a) == -3); + return true; +} + int main() { iterator_cust_swap_test::test(); iter_ops::test(); reverse_iterator_test::test(); move_iterator_test::test(); + + test_lwg_XXX(); + STATIC_ASSERT(test_lwg_XXX()); }