From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 99A1E3857C5E for ; Fri, 1 Oct 2021 19:43:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 99A1E3857C5E Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-447-PukiKhzcPFWMjWQ9iq-Kkw-1; Fri, 01 Oct 2021 15:43:26 -0400 X-MC-Unique: PukiKhzcPFWMjWQ9iq-Kkw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0C4A3362F8; Fri, 1 Oct 2021 19:43:25 +0000 (UTC) Received: from localhost (unknown [10.33.36.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id B0EAE5DEFB; Fri, 1 Oct 2021 19:43:24 +0000 (UTC) Date: Fri, 1 Oct 2021 20:43:23 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Implement LWG 3392 for std::ranges::distance Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="Wkf3m6d1u5AGIhX9" Content-Disposition: inline X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 Oct 2021 19:43:28 -0000 --Wkf3m6d1u5AGIhX9 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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. Tested powerpc64le-linux. Committed to trunk. --Wkf3m6d1u5AGIhX9 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 20751fad19e1b0fb4272309dd6d7fde182b08dc1 Author: Jonathan Wakely Date: Tue Jun 1 14:22:38 2021 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 --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> + requires (!sized_sentinel_for<_Sent, _It>) + constexpr iter_difference_t<_It> + operator()[[nodiscard]](_It __first, _Sent __last) const + { + iter_difference_t<_It> __n = 0; + while (__first != __last) + { + ++__first; + ++__n; + } + return __n; + } + + template _Sent> [[nodiscard]] constexpr iter_difference_t<_It> - operator()(_It __first, _Sent __last) const + operator()(const _It& __first, const _Sent& __last) const { - if constexpr (sized_sentinel_for<_Sent, _It>) - return __last - __first; - else - { - iter_difference_t<_It> __n = 0; - while (__first != __last) - { - ++__first; - ++__n; - } - return __n; - } + 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 ); --Wkf3m6d1u5AGIhX9--