From patchwork Mon Feb 24 13:29:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1243083 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-519983-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=g73OZ/Ms; 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=dW+fL6Ii; 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 48R2xH02NXz9sRQ for ; Tue, 25 Feb 2020 00:30:09 +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:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=APFxiAq0HtaoTdMy FpYdlOvnlQokJYs3jUAg9S5Aw4VyCzMMQkaw2TuSy//3402yofd44ks7ssCUWgWN BxJkz+so66QhN5WBpsgoGCaPuimfRvLcFY+j7BxFFeu4XJh0iKrQR+Sm7kK9mA1i FyJN6KMOMrXXjC983CrbjRf/34c= 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:mime-version:content-type :content-transfer-encoding; s=default; bh=mBPyf0gNyrntbwhszP4Mew BqHhA=; b=g73OZ/MsgSYc0P+648tmwIms5Oxr4SufTwYNCLJuXdY28I8U9DIIa1 L6i5uBYHd/jvYVz4/Bu//hghjz2XRL5PXXwPmXXwWgSHsouq0WIKLAGN4A2SbhtK RbIkXsVwlZcpZY6gh+tV6W712rJE32NePELebZuHxPmt3ROX92nqI= Received: (qmail 81236 invoked by alias); 24 Feb 2020 13:29:51 -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 81174 invoked by uid 89); 24 Feb 2020 13:29:51 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-24.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 spammy=UD:stl_iterator.h, stl_iterator.h, stl_iteratorh, pertaining X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (205.139.110.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 24 Feb 2020 13:29:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582550987; 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=KbdU6wEX4dIGKeTxLnuzhD1mpcXFtWb6QIl3MfHxaLw=; b=dW+fL6Ii9exeaON68OTHO0AC480N6iYRqmzjApU7n9GDbBrFSdw0tXtpz9s94f6XMcRNjV zT7lla4cRfwQepSeePK1SKeodoKCjS1ya2CdeqgtUHAuDu0mWLmRLM48ZOA/CHfZVePxeA 58Z47SEDjoeqzy4jA5MSjmPVKNZEArc= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-64-mGAPULsTMouBLqLDNKvzrg-1; Mon, 24 Feb 2020 08:29:38 -0500 Received: by mail-qk1-f197.google.com with SMTP id a10so5643464qkg.11 for ; Mon, 24 Feb 2020 05:29:38 -0800 (PST) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id z194sm623044qkb.28.2020.02.24.05.29.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Feb 2020 05:29:36 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, jwakely@redhat.com, Patrick Palka Subject: [PATCH] libstdc++: Add missing bits of P0896R4 pertaining to [back|front]_insert_iterator Date: Mon, 24 Feb 2020 08:29:34 -0500 Message-Id: <20200224132934.559186-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-IsSubscribed: yes This adds some missing pieces of the Ranges TS that make back_insert_iterator and front_insert_iterator conform to the new output_iterator requirements. It also fixes a bug in ranges::__copy_or_move and ranges::__copy_or_move_backward in which we were inspecting the iter_value_t of the output iterator, but iterators such as back_insert_iterator and front_insert_iterator whose value_type is defined to be void do not have an iter_value_t according to [readable.traits] p4. The entire __use_memmove condition can probably be refactored, but the simplest fix for now is to inspect the iterator_traits of the output iterator instead. libstdc++-v3/ChangeLog: PR libstdc++/93884 * include/bits/ranges_algobase.h (__copy_or_move, __copy_or_move_backward): Don't inspect the iter_value_t of the output iterator, instead inspect its iterator_traits directly. * include/bits/stl_iterator.h (back_insert_iterator::container): Conditionally initialize. (back_insert_iterator::difference_type): Conditionally define. (back_insert_iterator::back_insert_iterator): Conditionally define this default constructor. (front_insert_iterator::container): Conditionally initialize. (front_insert_iterator::difference_type): Conditionally define. (front_insert_iterator::front_insert_iterator): Conditionally define this default constructor. --- libstdc++-v3/include/bits/ranges_algobase.h | 4 +- libstdc++-v3/include/bits/stl_iterator.h | 22 ++++++++++ .../back_insert_iterator/pr93884.C | 44 +++++++++++++++++++ .../front_insert_iterator/pr93884.C | 44 +++++++++++++++++++ 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/24_iterators/back_insert_iterator/pr93884.C create mode 100644 libstdc++-v3/testsuite/24_iterators/front_insert_iterator/pr93884.C diff --git a/libstdc++-v3/include/bits/ranges_algobase.h b/libstdc++-v3/include/bits/ranges_algobase.h index 807822e99c8..739424e1a2d 100644 --- a/libstdc++-v3/include/bits/ranges_algobase.h +++ b/libstdc++-v3/include/bits/ranges_algobase.h @@ -249,7 +249,7 @@ namespace ranges else if constexpr (sized_sentinel_for<_Sent, _Iter>) { using _ValueTypeI = iter_value_t<_Iter>; - using _ValueTypeO = iter_value_t<_Out>; + using _ValueTypeO = iterator_traits<_Out>::value_type; constexpr bool __use_memmove = (is_trivially_copyable_v<_ValueTypeI> && is_same_v<_ValueTypeI, _ValueTypeO> @@ -386,7 +386,7 @@ namespace ranges else if constexpr (sized_sentinel_for<_Sent, _Iter>) { using _ValueTypeI = iter_value_t<_Iter>; - using _ValueTypeO = iter_value_t<_Out>; + using _ValueTypeO = iterator_traits<_Out>::value_type; constexpr bool __use_memmove = (is_trivially_copyable_v<_ValueTypeI> && is_same_v<_ValueTypeI, _ValueTypeO> diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h index 372df223113..0f2742ae4cf 100644 --- a/libstdc++-v3/include/bits/stl_iterator.h +++ b/libstdc++-v3/include/bits/stl_iterator.h @@ -496,11 +496,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : public iterator { protected: +#if __cplusplus > 201703L + _Container* container = nullptr; +#else _Container* container; +#endif public: /// A nested typedef for the type of whatever container you used. typedef _Container container_type; +#if __cplusplus > 201703L + using difference_type = ptrdiff_t; +#endif + +#if __cplusplus > 201703L + constexpr back_insert_iterator() noexcept = default; +#endif /// The only way to create this %iterator is with a container. explicit @@ -588,11 +599,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : public iterator { protected: +#if __cplusplus <= 201703L _Container* container; +#else + _Container* container = nullptr; +#endif public: /// A nested typedef for the type of whatever container you used. typedef _Container container_type; +#if __cplusplus > 201703L + using difference_type = ptrdiff_t; +#endif + +#if __cplusplus > 201703L + constexpr front_insert_iterator() noexcept = default; +#endif /// The only way to create this %iterator is with a container. explicit front_insert_iterator(_Container& __x) diff --git a/libstdc++-v3/testsuite/24_iterators/back_insert_iterator/pr93884.C b/libstdc++-v3/testsuite/24_iterators/back_insert_iterator/pr93884.C new file mode 100644 index 00000000000..5a6287ddd40 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/back_insert_iterator/pr93884.C @@ -0,0 +1,44 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include +#include + +namespace ranges = std::ranges; +namespace views = std::views; + +void +test01() +{ + auto v = std::vector{}; + auto i = views::iota(0, 10); + auto o = std::back_inserter(v); + static_assert(std::output_iterator); + ranges::copy(i, o); + VERIFY( ranges::equal(v, i) ); +} + +int +main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/24_iterators/front_insert_iterator/pr93884.C b/libstdc++-v3/testsuite/24_iterators/front_insert_iterator/pr93884.C new file mode 100644 index 00000000000..ee3d9ac4ed9 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/front_insert_iterator/pr93884.C @@ -0,0 +1,44 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include +#include + +namespace ranges = std::ranges; +namespace views = std::views; + +void +test01() +{ + auto v = std::deque{}; + auto i = views::iota(0, 10); + auto o = std::front_inserter(v); + static_assert(std::output_iterator); + ranges::copy(i, std::front_inserter(v)); + VERIFY( ranges::equal(v | views::reverse, i) ); +} + +int +main() +{ + test01(); +}