From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id E3F813858013; Fri, 1 Oct 2021 19:39:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E3F813858013 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-4076] libstdc++: Implement LWG 3392 for std::ranges::distance X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 9b790acc2207d69b4ebc0f4addd34a0aa32ec6cf X-Git-Newrev: 20751fad19e1b0fb4272309dd6d7fde182b08dc1 Message-Id: <20211001193935.E3F813858013@sourceware.org> Date: Fri, 1 Oct 2021 19:39:35 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 Oct 2021 19:39:36 -0000 https://gcc.gnu.org/g:20751fad19e1b0fb4272309dd6d7fde182b08dc1 commit r12-4076-g20751fad19e1b0fb4272309dd6d7fde182b08dc1 Author: Jonathan Wakely Date: Tue Jun 1 14:22:38 2021 +0100 libstdc++: Implement LWG 3392 for std::ranges::distance libstdc++-v3/ChangeLog: * include/bits/ranges_base.h (ranges::distance): Split overload into two (LWG 3392). * testsuite/24_iterators/range_operations/lwg3392.cc: New test. Diff: --- libstdc++-v3/include/bits/ranges_base.h | 27 ++++++++++--------- .../24_iterators/range_operations/lwg3392.cc | 30 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/libstdc++-v3/include/bits/ranges_base.h b/libstdc++-v3/include/bits/ranges_base.h index d6166ab1dd3..01d0c35f4b4 100644 --- a/libstdc++-v3/include/bits/ranges_base.h +++ b/libstdc++-v3/include/bits/ranges_base.h @@ -787,22 +787,25 @@ namespace ranges struct __distance_fn final { template _Sent> - [[nodiscard]] + requires (!sized_sentinel_for<_Sent, _It>) constexpr iter_difference_t<_It> - operator()(_It __first, _Sent __last) const + operator()[[nodiscard]](_It __first, _Sent __last) const { - if constexpr (sized_sentinel_for<_Sent, _It>) - return __last - __first; - else + iter_difference_t<_It> __n = 0; + while (__first != __last) { - iter_difference_t<_It> __n = 0; - while (__first != __last) - { - ++__first; - ++__n; - } - return __n; + ++__first; + ++__n; } + return __n; + } + + template _Sent> + [[nodiscard]] + constexpr iter_difference_t<_It> + operator()(const _It& __first, const _Sent& __last) const + { + return __last - __first; } template diff --git a/libstdc++-v3/testsuite/24_iterators/range_operations/lwg3392.cc b/libstdc++-v3/testsuite/24_iterators/range_operations/lwg3392.cc new file mode 100644 index 00000000000..32780353512 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/range_operations/lwg3392.cc @@ -0,0 +1,30 @@ +// { dg-options "-std=gnu++20" } +// { dg-do compile { target c++20 } } + +#include + +struct movable_iterator +{ + using difference_type = long; + + movable_iterator() = default; + movable_iterator(movable_iterator&&) = default; + movable_iterator& operator=(movable_iterator&&) = default; + + int operator*() const { return 1; } + + movable_iterator& operator++() { return *this; } + void operator++(int) { } + + bool operator==(const movable_iterator&) const = default; +}; + +using namespace std; + +constexpr counted_iterator it({}, 3); + +static_assert( sized_sentinel_for> ); +// LWG 3392 +// ranges::distance() cannot be used on a move-only iterator +// with a sized sentinel +static_assert( ranges::distance(it, default_sentinel) == 3 );