From patchwork Fri Oct 4 18:34:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1992948 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=flHo5cEw; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XKy2J4GP4z1xtH for ; Sat, 5 Oct 2024 04:37:11 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C0026385EC1D for ; Fri, 4 Oct 2024 18:37:08 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 3E6BD385DDFF for ; Fri, 4 Oct 2024 18:36:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3E6BD385DDFF Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3E6BD385DDFF Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728067008; cv=none; b=en29ESTdnTzxYPAPdjEiuecW+a5MBaiDea77XX+98LOJ307hkVCU1HwLtGFqOM5MTB+j5cDxfdVrWyJEYhjJP1NfB3AKVcVTUSFJqtR85nBL4otmbdPIiqK4G9/ZFjje4duxXqbXqfKad8i2Dvgj2S0Lp9pxCK7rSsHG/dmcMtw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728067008; c=relaxed/simple; bh=01Dycj+Tvmj9XPTunKMmKoKVO9P0rSq7AnlWoUA1WVg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=XSWkYFsX4mCq0+tXmJilNkWALkf6fpk0Vib6m2JHvHjNDyQnkjFC/ZPvds7YX8Tmf6r9c+xRseUdEGFBNZpetiF0GNsHqm8x4pm6m45jrILxImVlFogQNkRnhy/PqCEQC14jwahFMO/U2warlgzvaHYbRRTwZ09mV0XWPD3ucD8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728067007; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=cWlapiCMmMlevqRncvFn3Qhglpb1T1iiWxUvB7U3fxc=; b=flHo5cEwpjR8xsYv/80nOzd8KkdFNPAUm634bL0ySkLRG62opO+ne/iI4M7WQJ7YJnQOMZ mm/MFAFQWUpHxaPm25IMCEdLD/pPLx4c68VNIxwkARLF17NFKgGXVRH/lMSYXmibGKSNfc VCd+NTDZ69A9crfLOvNrSsRlFyhC7vw= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-5-FAlD2c8xOX-uaO287wrCmQ-1; Fri, 04 Oct 2024 14:36:46 -0400 X-MC-Unique: FAlD2c8xOX-uaO287wrCmQ-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7a9eabcbaf0so30746685a.3 for ; Fri, 04 Oct 2024 11:36:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728067004; x=1728671804; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=cWlapiCMmMlevqRncvFn3Qhglpb1T1iiWxUvB7U3fxc=; b=mcVSOsZLji4WCF3S1FG9RCMfHIRDQh7lhlFZ5gD8z79Rlq3us/nP/4H2xz8FWXsERl Lj6mMf+TFYqH1leahrsNikC14Tk44KqT20xOMRqNKsrmvoDWW2m14W/6898D8msvXPFC SEUT23rizcSSPnPjgYx42zFHhZoDpT3Vjj2QowD5oA+Q9j66KGUo30cPZFCQI+aJYi0T 3U12pzT0C9OYA6A9BCgg04RZ8yOA+3cfWFPDBOS0R0oVCDPNRTJ4I3rtvMv0UhTynWXD +SYvXKOVKe2D0CCM4feCU4ZB+L1mJDcH/fXPBUHoGg2wHV/JbJWogqj1wD5Wy2HMFjq6 agAw== X-Gm-Message-State: AOJu0YyPU0V9+nreh7wgMOcSG/V8F4og2+KSGNXFippMSIW7mIoWf7wV aI2dLwIf7dEGYp8PKkDoirZ76E+jUXcx2K9fcOzwEuL/FeOGF8EO+fgZJ3FGr/RzGKovjdTt+R3 Cia+bkorcXTlQ6hYSX4OQ8xpKAeVn9Ezf/M6xtz5ZJtfakLI1TA/xHW909pQvqHUFGZmltawZ9z x8xn8qEeC/G2ZOATyVcBRdSmPFr0xvoK5aU1w9 X-Received: by 2002:a05:620a:2913:b0:7a9:a80e:3a with SMTP id af79cd13be357-7ae6f442390mr260054185a.4.1728067003820; Fri, 04 Oct 2024 11:36:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE6VF02hB5VPeNKRGwXk6pwXN5IhHJZKzfzPb8VCPkGuxD+krIBLf/XvTdm36DUeT3ZgYITpQ== X-Received: by 2002:a05:620a:2913:b0:7a9:a80e:3a with SMTP id af79cd13be357-7ae6f442390mr260051985a.4.1728067003263; Fri, 04 Oct 2024 11:36:43 -0700 (PDT) Received: from localhost.localdomain (ool-18bb2a2e.dyn.optonline.net. [24.187.42.46]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7ae7562e37csm9050785a.47.2024.10.04.11.36.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Oct 2024 11:36:42 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH] libstdc++: Implement LWG 3664 changes to ranges::distance Date: Fri, 4 Oct 2024 14:34:04 -0400 Message-ID: <20241004183404.218709-1-ppalka@redhat.com> X-Mailer: git-send-email 2.47.0.rc1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.5 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_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested on x86_64-pc-linux-gnu, does this look OK for trunk/backports? -- >8 -- libstdc++-v3/ChangeLog: * include/bits/ranges_base.h (__distance_fn::operator()): Adjust iterator/sentinel overloads as per LWG 3664. * testsuite/24_iterators/range_operations/distance.cc: Test LWG 3664 example. --- libstdc++-v3/include/bits/ranges_base.h | 14 +++++++------- .../24_iterators/range_operations/distance.cc | 11 +++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/libstdc++-v3/include/bits/ranges_base.h b/libstdc++-v3/include/bits/ranges_base.h index 137c3c98e14..cb2eba1f841 100644 --- a/libstdc++-v3/include/bits/ranges_base.h +++ b/libstdc++-v3/include/bits/ranges_base.h @@ -947,7 +947,9 @@ namespace ranges struct __distance_fn final { - template _Sent> + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3664. LWG 3392 broke std::ranges::distance(a, a+3) + template _Sent> requires (!sized_sentinel_for<_Sent, _It>) constexpr iter_difference_t<_It> operator()[[nodiscard]](_It __first, _Sent __last) const @@ -961,13 +963,11 @@ namespace ranges return __n; } - template _Sent> + template> _Sent> [[nodiscard]] - constexpr iter_difference_t<_It> - operator()(const _It& __first, const _Sent& __last) const - { - return __last - __first; - } + constexpr iter_difference_t> + operator()(_It&& __first, _Sent __last) const + { return __last - static_cast&>(__first); } template [[nodiscard]] diff --git a/libstdc++-v3/testsuite/24_iterators/range_operations/distance.cc b/libstdc++-v3/testsuite/24_iterators/range_operations/distance.cc index 9a1d0c3efe8..336956936c2 100644 --- a/libstdc++-v3/testsuite/24_iterators/range_operations/distance.cc +++ b/libstdc++-v3/testsuite/24_iterators/range_operations/distance.cc @@ -144,6 +144,16 @@ test05() VERIFY( std::ranges::distance(c4) == 5 ); } +void +test06() +{ + // LWG 3664 - LWG 3392 broke std::ranges::distance(a, a+3) + int a[] = {1, 2, 3}; + VERIFY( std::ranges::distance(a, a+3) == 3 ); + VERIFY( std::ranges::distance(a, a) == 0 ); + VERIFY( std::ranges::distance(a+3, a) == -3 ); +} + int main() { @@ -152,4 +162,5 @@ main() test03(); test04(); test05(); + test06(); }