From patchwork Tue Mar 3 16:30:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1248500 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-520547-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha1 header.s=default header.b=pTPa/FJ5; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LtM1a0+t; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48X2bQ1p2Dz9sRN for ; Wed, 4 Mar 2020 03:32:02 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; q=dns; s= default; b=VNYXXF6xJAe3oefTxJKAsxHsGo7DKPj+aOaXoF4vkDkYQsU1po9GD Z/IfFHvH2PV+AoATF0UR6fB1KWroODBrgRdpxVIAmuTLj+owcs9aDqVMiwJeAPdu EeKkXUExFTUcgRuzvbf92VgPZV7KLVdVe/ek4VdDBwJicfHjP21e4E= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; s=default; bh=L2LM/bh1kmfTYXkqwj6PfgtMJFA=; b=pTPa/FJ5j1OcxLI/3UtbHbefoCR7 NMb3E/N3uEyuKdC39biww+Jbfg/Nmo63ApRAXlYAZfmyC1CUzm6/77IUHpy6BKSq dTxW7wp+UM7StAEsD1GMtU8/tWVGHTCs/VPbIa+G+IOE2vZuazf0bkOxY/ACFMn3 LTpLT68N+9Jvk88= Received: (qmail 103082 invoked by alias); 3 Mar 2020 16:31:03 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 102949 invoked by uid 89); 3 Mar 2020 16:31:02 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 spammy= X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (207.211.31.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 03 Mar 2020 16:31:00 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583253059; 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: in-reply-to:in-reply-to:references:references; bh=aNLNMksQWTfl8lDPTxpDb0xvXIYsOhdv3mD6d1jPpAs=; b=LtM1a0+tjWhN5Cew3/Oj6ZFf2jBfYVUY+86bHudHAcbSlKYrWtVn2ybUxTilDCutOGrE/d pJ3eJ3ZKNJkivToTwi6tgQnroK1tnGiSfysQoLuWNw0VgB7Gn5LcwUTB9wOSFLmphEBB9g m0xDYOthdbGZuHrmqw8gEAh5/3FcVJM= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-281-LWkM-P1oP4SAt2fUTG7RCw-1; Tue, 03 Mar 2020 11:30:57 -0500 Received: by mail-qk1-f199.google.com with SMTP id z124so2489997qkd.20 for ; Tue, 03 Mar 2020 08:30:57 -0800 (PST) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id h24sm5412159qkk.3.2020.03.03.08.30.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2020 08:30:55 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH 4/4] libstdc++: Move-only input iterator support in algorithms (LWG 3355) Date: Tue, 3 Mar 2020 11:30:43 -0500 Message-Id: <20200303163043.2182013-4-ppalka@redhat.com> In-Reply-To: <20200303163043.2182013-1-ppalka@redhat.com> References: <20200303163043.2182013-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-IsSubscribed: yes This adds support for move-only input iterators in the ranges::unitialized_* algorithms defined in , as per LWG 3355. The only changes needed are to add calls to std::move in the appropriate places and to use operator-() instead of ranges::distance() because the latter cannot be used with a move-only iterator with a sized sentinel as is the case here. (This issue with ranges::distance is LWG 3392.) libstdc++-v3/ChangeLog: LWG 3355 The memory algorithms should support move-only input iterators introduced by P1207 * include/bits/ranges_uninitialized.h (__uninitialized_copy_fn::operator()): Use std::move to avoid attempting to copy __ifirst, which could be a move-only input iterator. Use operator- instead of ranges::distance to compute distance from a sized sentinel. (__uninitialized_copy_n_fn::operator()): Likewise. (__uninitialized_move_fn::operator()): Likewise. (__uninitialized_move_n_fn::operator()): Likewise. (__uninitialized_destroy_fn::operator()): Use std::move to avoid attempting to copy __first. (__uninitialized_destroy_n_fn::operator()): Likewise. * testsuite/20_util/specialized_algorithms/destroy/constrained.cc: Augment test. * .../specialized_algorithms/uninitialized_copy/constrained.cc: Likewise. * .../specialized_algorithms/uninitialized_move/constrained.cc: Likewise. --- .../include/bits/ranges_uninitialized.h | 34 ++++++++++--------- .../destroy/constrained.cc | 15 ++++++++ .../uninitialized_copy/constrained.cc | 25 ++++++++++++++ .../uninitialized_move/constrained.cc | 25 ++++++++++++++ 4 files changed, 83 insertions(+), 16 deletions(-) diff --git a/libstdc++-v3/include/bits/ranges_uninitialized.h b/libstdc++-v3/include/bits/ranges_uninitialized.h index f97a07a9b4a..d758078fc03 100644 --- a/libstdc++-v3/include/bits/ranges_uninitialized.h +++ b/libstdc++-v3/include/bits/ranges_uninitialized.h @@ -272,9 +272,10 @@ namespace ranges && is_nothrow_assignable_v<_OutType&, iter_reference_t<_Iter>>) { - auto __d1 = ranges::distance(__ifirst, __ilast); - auto __d2 = ranges::distance(__ofirst, __olast); - return ranges::copy_n(__ifirst, std::min(__d1, __d2), __ofirst); + auto __d1 = __ilast - __ifirst; + auto __d2 = __olast - __ofirst; + return ranges::copy_n(std::move(__ifirst), std::min(__d1, __d2), + __ofirst); } else { @@ -283,7 +284,7 @@ namespace ranges ++__ofirst, (void)++__ifirst) ::new (__detail::__voidify(*__ofirst)) _OutType(*__ifirst); __guard.release(); - return {__ifirst, __ofirst}; + return {std::move(__ifirst), __ofirst}; } } @@ -319,8 +320,9 @@ namespace ranges && is_nothrow_assignable_v<_OutType&, iter_reference_t<_Iter>>) { - auto __d = ranges::distance(__ofirst, __olast); - return ranges::copy_n(__ifirst, std::min(__n, __d), __ofirst); + auto __d = __olast - __ofirst; + return ranges::copy_n(std::move(__ifirst), std::min(__n, __d), + __ofirst); } else { @@ -329,7 +331,7 @@ namespace ranges ++__ofirst, (void)++__ifirst, (void)--__n) ::new (__detail::__voidify(*__ofirst)) _OutType(*__ifirst); __guard.release(); - return {__ifirst, __ofirst}; + return {std::move(__ifirst), __ofirst}; } } }; @@ -357,10 +359,10 @@ namespace ranges && is_nothrow_assignable_v<_OutType&, iter_rvalue_reference_t<_Iter>>) { - auto __d1 = ranges::distance(__ifirst, __ilast); - auto __d2 = ranges::distance(__ofirst, __olast); + auto __d1 = __ilast - __ifirst; + auto __d2 = __olast - __ofirst; auto [__in, __out] - = ranges::copy_n(std::make_move_iterator(__ifirst), + = ranges::copy_n(std::make_move_iterator(std::move(__ifirst)), std::min(__d1, __d2), __ofirst); return {std::move(__in).base(), __out}; } @@ -372,7 +374,7 @@ namespace ranges ::new (__detail::__voidify(*__ofirst)) _OutType(ranges::iter_move(__ifirst)); __guard.release(); - return {__ifirst, __ofirst}; + return {std::move(__ifirst), __ofirst}; } } @@ -409,9 +411,9 @@ namespace ranges && is_nothrow_assignable_v<_OutType&, iter_rvalue_reference_t<_Iter>>) { - auto __d = ranges::distance(__ofirst, __olast); + auto __d = __olast - __ofirst; auto [__in, __out] - = ranges::copy_n(std::make_move_iterator(__ifirst), + = ranges::copy_n(std::make_move_iterator(std::move(__ifirst)), std::min(__n, __d), __ofirst); return {std::move(__in).base(), __out}; } @@ -423,7 +425,7 @@ namespace ranges ::new (__detail::__voidify(*__ofirst)) _OutType(ranges::iter_move(__ifirst)); __guard.release(); - return {__ifirst, __ofirst}; + return {std::move(__ifirst), __ofirst}; } } }; @@ -524,7 +526,7 @@ namespace ranges __destroy_fn::operator()(_Iter __first, _Sent __last) const noexcept { if constexpr (is_trivially_destructible_v>) - return ranges::next(__first, __last); + return ranges::next(std::move(__first), __last); else { for (; __first != __last; ++__first) @@ -549,7 +551,7 @@ namespace ranges operator()(_Iter __first, iter_difference_t<_Iter> __n) const noexcept { if constexpr (is_trivially_destructible_v>) - return ranges::next(__first, __n); + return ranges::next(std::move(__first), __n); else { for (; __n > 0; ++__first, (void)--__n) diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/destroy/constrained.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/destroy/constrained.cc index c04049de0b5..89085d381ed 100644 --- a/libstdc++-v3/testsuite/20_util/specialized_algorithms/destroy/constrained.cc +++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/destroy/constrained.cc @@ -29,6 +29,9 @@ #include #include +using __gnu_test::test_range; +using __gnu_test::input_iterator_wrapper_nocopy; + namespace ranges = std::ranges; struct X @@ -69,6 +72,18 @@ test01() } } +void +test02() +{ + // LWG 3355 + { + int x[3] = {0}; + test_range rx(x); + ranges::destroy(rx); + ranges::destroy_n(rx.begin(), 3); + } +} + int main() { diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_copy/constrained.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_copy/constrained.cc index c1a50c45df9..ac5d74558d2 100644 --- a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_copy/constrained.cc +++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_copy/constrained.cc @@ -31,6 +31,9 @@ using __gnu_test::test_input_range; using __gnu_test::test_forward_range; +using __gnu_test::test_range; +using __gnu_test::test_sized_range_sized_sent; +using __gnu_test::input_iterator_wrapper_nocopy; namespace ranges = std::ranges; @@ -150,6 +153,28 @@ test02() } } +void +test03() +{ + // LWG 3355 + { + int x[3] = {0}; + int y[3]; + test_sized_range_sized_sent rx(x); + ranges::uninitialized_copy(rx, y); + ranges::uninitialized_copy_n(rx.begin(), 3, y, y+3); + } + + { + int x[3] = {0}; + int y[3]; + test_range rx(x); + test_forward_range ry(y); + ranges::uninitialized_copy(rx, y); + ranges::uninitialized_copy_n(rx.begin(), 3, ry.begin(), ry.end()); + } +} + int main() { diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_move/constrained.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_move/constrained.cc index a7d6fd39b38..6b3544edc55 100644 --- a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_move/constrained.cc +++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_move/constrained.cc @@ -31,6 +31,9 @@ using __gnu_test::test_input_range; using __gnu_test::test_forward_range; +using __gnu_test::test_range; +using __gnu_test::test_sized_range_sized_sent; +using __gnu_test::input_iterator_wrapper_nocopy; namespace ranges = std::ranges; @@ -160,6 +163,28 @@ test02() } } +void +test03() +{ + // LWG 3355 + { + int x[3] = {0}; + int y[3]; + test_sized_range_sized_sent rx(x); + ranges::uninitialized_move(rx, y); + ranges::uninitialized_move_n(rx.begin(), 3, y, y+3); + } + + { + int x[3] = {0}; + int y[3]; + test_range rx(x); + test_forward_range ry(y); + ranges::uninitialized_move(rx, y); + ranges::uninitialized_move_n(rx.begin(), 3, ry.begin(), ry.end()); + } +} + int main() {