From patchwork Fri Oct 1 19:43:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 45729 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0D12E385742F for ; Fri, 1 Oct 2021 20:17:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0D12E385742F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1633119449; bh=8OGJY87NXy3HTiNUufXYE3BvJFm8rSaHh+I/StDPu8c=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=JY0lAfE7u+T0a4+19YF1LevFpns0y0YE/3dUVvcH8NcCH06IGWc9O8WF0oj8dgaQk BezAI34g59myjRWAZ/z6RhuBRlbj/3grgwepKBbqu9YwvZkTZzd3DpvrYkE1W5u8wJ QEPz/QDWG35MVhgYFAJT1Z6biclDsFC9POTi+mec= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 5BABC3857810 for ; Fri, 1 Oct 2021 19:43:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5BABC3857810 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-591-MQ8GWR_MMUqAZZEbWv21kg-1; Fri, 01 Oct 2021 15:43:47 -0400 X-MC-Unique: MQ8GWR_MMUqAZZEbWv21kg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8543C802928; Fri, 1 Oct 2021 19:43:46 +0000 (UTC) Received: from localhost (unknown [10.33.36.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id EBC3A5C1B4; Fri, 1 Oct 2021 19:43:45 +0000 (UTC) Date: Fri, 1 Oct 2021 20:43:45 +0100 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add container adaptor constructors taking iterators (P1425R4) Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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, KAM_SHORT, 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This adds a feature that was recently added to the C++23 working draft. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/bits/stl_queue.h (__cpp_lib_adaptor_iterator_pair_constructor): Define for C++23, as per P1425R4. (queue(InputIterator, InputIterator)): Likewise. (queue(InputIterator, InputIterator, const Alloc&)): Likewise. * include/bits/stl_stack.h (__cpp_lib_adaptor_iterator_pair_constructor): Likewise. (stack(InputIterator, InputIterator)): Likewise. (stack(InputIterator, InputIterator, const Alloc&)): Likewise. * include/std/version (__cpp_lib_adaptor_iterator_pair_constructor): Define. * testsuite/23_containers/queue/cons_from_iters.cc: New test. * testsuite/23_containers/stack/cons_from_iters.cc: New test. Tested powerpc64le-linux. Committed to trunk. commit b7e8fb5e48279ffa5f424e3dd0bb3dfcbe69f5d5 Author: Jonathan Wakely Date: Thu May 13 16:16:26 2021 libstdc++: Add container adaptor constructors taking iterators (P1425R4) This adds a feature that was recently added to the C++23 working draft. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/bits/stl_queue.h (__cpp_lib_adaptor_iterator_pair_constructor): Define for C++23, as per P1425R4. (queue(InputIterator, InputIterator)): Likewise. (queue(InputIterator, InputIterator, const Alloc&)): Likewise. * include/bits/stl_stack.h (__cpp_lib_adaptor_iterator_pair_constructor): Likewise. (stack(InputIterator, InputIterator)): Likewise. (stack(InputIterator, InputIterator, const Alloc&)): Likewise. * include/std/version (__cpp_lib_adaptor_iterator_pair_constructor): Define. * testsuite/23_containers/queue/cons_from_iters.cc: New test. * testsuite/23_containers/stack/cons_from_iters.cc: New test. diff --git a/libstdc++-v3/include/bits/stl_queue.h b/libstdc++-v3/include/bits/stl_queue.h index 4519f9f2fec..ccd1122f848 100644 --- a/libstdc++-v3/include/bits/stl_queue.h +++ b/libstdc++-v3/include/bits/stl_queue.h @@ -194,6 +194,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template> queue(queue&& __q, const _Alloc& __a) : c(std::move(__q.c), __a) { } + +#if __cplusplus > 202002L +#define __cpp_lib_adaptor_iterator_pair_constructor 202100L + + template> + queue(_InputIterator __first, _InputIterator __last) + : c(__first, __last) { } + + template, + typename = _Uses<_Alloc>> + queue(_InputIterator __first, _InputIterator __last, const _Alloc& __a) + : c(__first, __last, __a) { } +#endif #endif /** @@ -331,6 +346,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typename = _RequireAllocator<_Allocator>> queue(_Container, _Allocator) -> queue; + +#ifdef __cpp_lib_adaptor_iterator_pair_constructor + template::value_type, + typename = _RequireInputIter<_InputIterator>> + queue(_InputIterator, _InputIterator) -> queue<_ValT>; + + template::value_type, + typename = _RequireInputIter<_InputIterator>, + typename = _RequireAllocator<_Allocator>> + queue(_InputIterator, _InputIterator, _Allocator) + -> queue<_ValT, deque<_ValT, _Allocator>>; +#endif #endif /** diff --git a/libstdc++-v3/include/bits/stl_stack.h b/libstdc++-v3/include/bits/stl_stack.h index 85137b9d428..af234d6899c 100644 --- a/libstdc++-v3/include/bits/stl_stack.h +++ b/libstdc++-v3/include/bits/stl_stack.h @@ -170,6 +170,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION stack(_Sequence&& __c) : c(std::move(__c)) { } +#if __cplusplus > 202002L +#define __cpp_lib_adaptor_iterator_pair_constructor 202100L + + template> + stack(_InputIterator __first, _InputIterator __last) + : c(__first, __last) { } +#endif + + template> explicit stack(const _Alloc& __a) @@ -190,6 +200,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template> stack(stack&& __q, const _Alloc& __a) : c(std::move(__q.c), __a) { } + +#if __cplusplus > 202002L + template, + typename = _Uses<_Alloc>> + stack(_InputIterator __first, _InputIterator __last, const _Alloc& __a) + : c(__first, __last, __a) { } +#endif #endif /** @@ -303,6 +321,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typename = _RequireAllocator<_Allocator>> stack(_Container, _Allocator) -> stack; + +#ifdef __cpp_lib_adaptor_iterator_pair_constructor + template::value_type, + typename = _RequireInputIter<_InputIterator>> + stack(_InputIterator, _InputIterator) -> stack<_ValT>; + + template::value_type, + typename = _RequireInputIter<_InputIterator>, + typename = _RequireAllocator<_Allocator>> + stack(_InputIterator, _InputIterator, _Allocator) + -> stack<_ValT, deque<_ValT, _Allocator>>; +#endif #endif /** diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version index 42f7dfa62ad..66b3d1704b7 100644 --- a/libstdc++-v3/include/std/version +++ b/libstdc++-v3/include/std/version @@ -281,6 +281,7 @@ #if __cplusplus > 202002L // c++2b +#define __cpp_lib_adaptor_iterator_pair_constructor 202100L #define __cpp_lib_invoke_r 202106L #define __cpp_lib_is_scoped_enum 202011L #define __cpp_lib_string_contains 202011L diff --git a/libstdc++-v3/testsuite/23_containers/queue/cons_from_iters.cc b/libstdc++-v3/testsuite/23_containers/queue/cons_from_iters.cc new file mode 100644 index 00000000000..de0fc310c24 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/queue/cons_from_iters.cc @@ -0,0 +1,68 @@ +// Copyright (C) 2021 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++23" } +// { dg-do run { target c++23 } } + +#include + +#ifndef __cpp_lib_adaptor_iterator_pair_constructor +#error Feature test macro for iterator pair constructors is missing in +#elif __cpp_lib_adaptor_iterator_pair_constructor != 202100L +#error Feature test macro for iterator pair constructors has wrong value in +#endif + +#include +#include + +void +test_p1425r4() +{ + const int vals[] = { 5, 4, 3, 2, 1, 9 }; + + std::queue q(std::begin(vals), std::end(vals)); + VERIFY( q.size() == std::size(vals) ); + VERIFY( q.front() == 5 ); + VERIFY( q.back() == 9 ); + + using Alloc = __gnu_test::uneq_allocator; + + struct Queue : std::queue> + { + using queue::queue; + + Alloc get_allocator() const { return c.get_allocator(); } + }; + + Alloc a0, a1(1); + Queue q0(std::next(vals), std::end(vals)); + VERIFY( q0.size() == std::size(vals) - 1 ); + VERIFY( q0.front() == 4 ); + VERIFY( q0.back() == 9 ); + VERIFY( q0.get_allocator() == a0 ); + + Queue q1(std::next(vals, 2), std::end(vals), a1); + VERIFY( q1.size() == std::size(vals) - 2 ); + VERIFY( q1.front() == 3 ); + VERIFY( q1.back() == 9 ); + VERIFY( q1.get_allocator() == a1 ); +} + +int main() +{ + test_p1425r4(); +} diff --git a/libstdc++-v3/testsuite/23_containers/stack/cons_from_iters.cc b/libstdc++-v3/testsuite/23_containers/stack/cons_from_iters.cc new file mode 100644 index 00000000000..4c648926d62 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/stack/cons_from_iters.cc @@ -0,0 +1,65 @@ +// Copyright (C) 2021 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++23" } +// { dg-do run { target c++23 } } + +#include + +#ifndef __cpp_lib_adaptor_iterator_pair_constructor +#error Feature test macro for iterator pair constructors is missing in +#elif __cpp_lib_adaptor_iterator_pair_constructor != 202100L +#error Feature test macro for iterator pair constructors has wrong value in +#endif + +#include +#include + +void +test_p1425r4() +{ + const int vals[] = { 1, 2, 3, 7, 8, 9, 5, 6, 7 }; + + std::stack s(std::begin(vals), std::end(vals)); + VERIFY( s.size() == std::size(vals) ); + VERIFY( s.top() == 7 ); + + using Alloc = __gnu_test::uneq_allocator; + + struct Stack : std::stack> + { + using stack::stack; + + Alloc get_allocator() const { return c.get_allocator(); } + }; + + Alloc a0, a1(1); + Stack s0(std::begin(vals), std::end(vals) - 1); + VERIFY( s0.size() == std::size(vals) - 1 ); + VERIFY( s0.top() == 6 ); + VERIFY( s0.get_allocator() == a0 ); + + Stack s1(std::begin(vals), std::end(vals) - 2, a1); + VERIFY( s1.size() == std::size(vals) - 2 ); + VERIFY( s1.top() == 5 ); + VERIFY( s1.get_allocator() == a1 ); +} + +int main() +{ + test_p1425r4(); +}