From patchwork Fri Mar 27 23:28:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 1263094 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=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=M10q6TxP; dkim-atps=neutral Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48pyhc26YDz9sRf for ; Sat, 28 Mar 2020 10:28:16 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DAA78387703C; Fri, 27 Mar 2020 23:28:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DAA78387703C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1585351693; bh=2cyz+UL4OJAFbGH9pZZfX0afT/XdngcniPyz1UKk7YY=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=M10q6TxPh/+snm6ZNoFeSssI+uIn3II1rWYUoJi4lnsI0HT25AQNI+R5BHxOaYgnQ 0qQU29nlMu3X17dBELgdO1jaxiRywSNPDpnYL7xWytCg01jQBpIRjddH3QorP84TWF kKZji9HA459B8XYDXlMMMB+Y9XptCoF9ectBhYPs= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [63.128.21.74]) by sourceware.org (Postfix) with ESMTP id 9D76E387703C for ; Fri, 27 Mar 2020 23:28:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 9D76E387703C 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-172-ZNrn23QJOFWg9IZZn2Y9ew-1; Fri, 27 Mar 2020 19:28:06 -0400 X-MC-Unique: ZNrn23QJOFWg9IZZn2Y9ew-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7581C800D5F; Fri, 27 Mar 2020 23:28:05 +0000 (UTC) Received: from localhost (unknown [10.33.36.130]) by smtp.corp.redhat.com (Postfix) with ESMTP id DFF4F19C7F; Fri, 27 Mar 2020 23:28:04 +0000 (UTC) Date: Fri, 27 Mar 2020 23:28:04 +0000 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 1/3] libstdc++: Implement C++20 changes to insert iterators Message-ID: <20200327232804.GA492830@redhat.com> MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-34.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: "Li, Pan2 via Gcc-patches" Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" std::insert_iterator and std::inserter need to be adjusted for C++20, so that they use ranges::iterator_t. That alias template requires ranges::begin to be defined. Rather than moving the whole of ranges::begin (and related details like ranges::enable_borrowed_range) into , this defines a new, simpler version of ranges::begin that is sufficient for ranges::iterator_t to be defined. This works because ranges::iterator_t uses an lvalue reference type, so the logic in ranges::begin for non-lvalue ranges (i.e. borrowed ranges) isn't needed. This also adds the missing constexpr specifiers to the other insert iterators. * include/bits/iterator_concepts.h (__detail::__decay_copy) (__detail::__member_begin, __detail::__adl_begin): Move here from . (__detail::__ranges_begin, __detail::__range_iter_t): Define. * bits/range_access.h (__cust_access::__decay_copy) (__cust_access::__member_begin, __cust_access::__adl_begin): Move to . (ranges::iterator_t): Use __detail::__range_iter_t. * include/bits/stl_iterator.h (back_insert_iterator): Simplify conditional compilation. Add _GLIBCXX20_CONSTEXPR to all members. (front_insert_iterator): Likewise. (insert_iterator): Implement changes from P0896R4 for C++20. * testsuite/24_iterators/back_insert_iterator/constexpr.cc: New test. * testsuite/24_iterators/front_insert_iterator/constexpr.cc: New test. * testsuite/24_iterators/headers/iterator/synopsis_c++17.cc: Adjust for inclusion in synopsis_c++20.cc which expects different signatures for some function templates. * testsuite/24_iterators/insert_iterator/constexpr.cc: New test. Tested powerpc64le-linux, committed to master. commit ae6076b5bc1e0b689eaa8521571b0516de336553 Author: Jonathan Wakely Date: Fri Mar 27 23:21:58 2020 +0000 libstdc++: Implement C++20 changes to insert iterators std::insert_iterator and std::inserter need to be adjusted for C++20, so that they use ranges::iterator_t. That alias template requires ranges::begin to be defined. Rather than moving the whole of ranges::begin (and related details like ranges::enable_borrowed_range) into , this defines a new, simpler version of ranges::begin that is sufficient for ranges::iterator_t to be defined. This works because ranges::iterator_t uses an lvalue reference type, so the logic in ranges::begin for non-lvalue ranges (i.e. borrowed ranges) isn't needed. This also adds the missing constexpr specifiers to the other insert iterators. * include/bits/iterator_concepts.h (__detail::__decay_copy) (__detail::__member_begin, __detail::__adl_begin): Move here from . (__detail::__ranges_begin, __detail::__range_iter_t): Define. * bits/range_access.h (__cust_access::__decay_copy) (__cust_access::__member_begin, __cust_access::__adl_begin): Move to . (ranges::iterator_t): Use __detail::__range_iter_t. * include/bits/stl_iterator.h (back_insert_iterator): Simplify conditional compilation. Add _GLIBCXX20_CONSTEXPR to all members. (front_insert_iterator): Likewise. (insert_iterator): Implement changes from P0896R4 for C++20. * testsuite/24_iterators/back_insert_iterator/constexpr.cc: New test. * testsuite/24_iterators/front_insert_iterator/constexpr.cc: New test. * testsuite/24_iterators/headers/iterator/synopsis_c++17.cc: Adjust for inclusion in synopsis_c++20.cc which expects different signatures for some function templates. * testsuite/24_iterators/insert_iterator/constexpr.cc: New test. diff --git a/libstdc++-v3/include/bits/iterator_concepts.h b/libstdc++-v3/include/bits/iterator_concepts.h index 08e622259b4..b598532089e 100644 --- a/libstdc++-v3/include/bits/iterator_concepts.h +++ b/libstdc++-v3/include/bits/iterator_concepts.h @@ -835,6 +835,56 @@ namespace ranges struct default_sentinel_t { }; inline constexpr default_sentinel_t default_sentinel{}; + namespace __detail + { + template + constexpr decay_t<_Tp> + __decay_copy(_Tp&& __t) + noexcept(is_nothrow_convertible_v<_Tp, decay_t<_Tp>>) + { return std::forward<_Tp>(__t); } + + template + concept __member_begin = requires(_Tp& __t) + { + { __detail::__decay_copy(__t.begin()) } -> input_or_output_iterator; + }; + + void begin(auto&) = delete; + void begin(const auto&) = delete; + + template + concept __adl_begin = __class_or_enum> + && requires(_Tp& __t) + { + { __detail::__decay_copy(begin(__t)) } -> input_or_output_iterator; + }; + + // Simplified version of std::ranges::begin that only supports lvalues, + // for use by __range_iter_t below. + template + requires is_array_v<_Tp> || __member_begin<_Tp&> || __adl_begin<_Tp&> + auto + __ranges_begin(_Tp& __t) + { + if constexpr (is_array_v<_Tp>) + { + static_assert(sizeof(remove_all_extents_t<_Tp>) != 0, + "not array of incomplete type"); + return __t + 0; + } + else if constexpr (__member_begin<_Tp&>) + return __t.begin(); + else + return begin(__t); + } + + // Implementation of std::ranges::iterator_t, without using ranges::begin. + template + using __range_iter_t + = decltype(__detail::__ranges_begin(std::declval<_Tp&>())); + + } // namespace __detail + _GLIBCXX_END_NAMESPACE_VERSION } // namespace std #endif // C++20 library concepts diff --git a/libstdc++-v3/include/bits/range_access.h b/libstdc++-v3/include/bits/range_access.h index 4dd48bbc6df..acd701e1385 100644 --- a/libstdc++-v3/include/bits/range_access.h +++ b/libstdc++-v3/include/bits/range_access.h @@ -372,28 +372,9 @@ namespace ranges { using std::ranges::__detail::__maybe_borrowed_range; using std::__detail::__class_or_enum; - - template - constexpr decay_t<_Tp> - __decay_copy(_Tp&& __t) - noexcept(is_nothrow_convertible_v<_Tp, decay_t<_Tp>>) - { return std::forward<_Tp>(__t); } - - template - concept __member_begin = requires(_Tp& __t) - { - { __decay_copy(__t.begin()) } -> input_or_output_iterator; - }; - - void begin(auto&) = delete; - void begin(const auto&) = delete; - - template - concept __adl_begin = __class_or_enum> - && requires(_Tp& __t) - { - { __decay_copy(begin(__t)) } -> input_or_output_iterator; - }; + using std::__detail::__decay_copy; + using std::__detail::__member_begin; + using std::__detail::__adl_begin; struct _Begin { @@ -889,7 +870,7 @@ namespace ranges = range<_Tp> && __detail::__maybe_borrowed_range<_Tp>; template - using iterator_t = decltype(ranges::begin(std::declval<_Tp&>())); + using iterator_t = std::__detail::__range_iter_t<_Tp>; template using sentinel_t = decltype(ranges::end(std::declval<_Range&>())); diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h index caaa8c483b8..d10c30cbfcc 100644 --- a/libstdc++-v3/include/bits/stl_iterator.h +++ b/libstdc++-v3/include/bits/stl_iterator.h @@ -76,6 +76,7 @@ #if __cplusplus > 201703L # include # include +# include #endif namespace std _GLIBCXX_VISIBILITY(default) @@ -496,25 +497,19 @@ _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 back_insert_iterator() noexcept = default; + constexpr back_insert_iterator() noexcept : container(nullptr) { } #endif /// The only way to create this %iterator is with a container. - explicit + explicit _GLIBCXX20_CONSTEXPR back_insert_iterator(_Container& __x) : container(std::__addressof(__x)) { } @@ -537,6 +532,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this; } #else + _GLIBCXX20_CONSTEXPR back_insert_iterator& operator=(const typename _Container::value_type& __value) { @@ -544,6 +540,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this; } + _GLIBCXX20_CONSTEXPR back_insert_iterator& operator=(typename _Container::value_type&& __value) { @@ -553,16 +550,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif /// Simply returns *this. + _GLIBCXX20_CONSTEXPR back_insert_iterator& operator*() { return *this; } /// Simply returns *this. (This %iterator does not @a move.) + _GLIBCXX20_CONSTEXPR back_insert_iterator& operator++() { return *this; } /// Simply returns *this. (This %iterator does not @a move.) + _GLIBCXX20_CONSTEXPR back_insert_iterator operator++(int) { return *this; } @@ -580,6 +580,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * types for you. */ template + _GLIBCXX20_CONSTEXPR inline back_insert_iterator<_Container> back_inserter(_Container& __x) { return back_insert_iterator<_Container>(__x); } @@ -599,25 +600,20 @@ _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; + constexpr front_insert_iterator() noexcept : container(nullptr) { } #endif /// The only way to create this %iterator is with a container. - explicit front_insert_iterator(_Container& __x) + explicit _GLIBCXX20_CONSTEXPR + front_insert_iterator(_Container& __x) : container(std::__addressof(__x)) { } /** @@ -639,6 +635,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this; } #else + _GLIBCXX20_CONSTEXPR front_insert_iterator& operator=(const typename _Container::value_type& __value) { @@ -646,6 +643,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this; } + _GLIBCXX20_CONSTEXPR front_insert_iterator& operator=(typename _Container::value_type&& __value) { @@ -655,16 +653,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif /// Simply returns *this. + _GLIBCXX20_CONSTEXPR front_insert_iterator& operator*() { return *this; } /// Simply returns *this. (This %iterator does not @a move.) + _GLIBCXX20_CONSTEXPR front_insert_iterator& operator++() { return *this; } /// Simply returns *this. (This %iterator does not @a move.) + _GLIBCXX20_CONSTEXPR front_insert_iterator operator++(int) { return *this; } @@ -682,6 +683,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * types for you. */ template + _GLIBCXX20_CONSTEXPR inline front_insert_iterator<_Container> front_inserter(_Container& __x) { return front_insert_iterator<_Container>(__x); } @@ -704,19 +706,36 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION class insert_iterator : public iterator { +#if __cplusplus > 201703L && defined __cpp_lib_concepts + using _Iter = std::__detail::__range_iter_t<_Container>; + + protected: + _Container* container = nullptr; + _Iter iter = _Iter(); +#else + typedef typename _Container::iterator _Iter; + protected: _Container* container; - typename _Container::iterator iter; + _Iter iter; +#endif public: /// A nested typedef for the type of whatever container you used. typedef _Container container_type; +#if __cplusplus > 201703L && defined __cpp_lib_concepts + using difference_type = ptrdiff_t; + + insert_iterator() = default; +#endif + /** * The only way to create this %iterator is with a container and an * initial position (a normal %iterator into the container). */ - insert_iterator(_Container& __x, typename _Container::iterator __i) + _GLIBCXX20_CONSTEXPR + insert_iterator(_Container& __x, _Iter __i) : container(std::__addressof(__x)), iter(__i) {} /** @@ -751,6 +770,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this; } #else + _GLIBCXX20_CONSTEXPR insert_iterator& operator=(const typename _Container::value_type& __value) { @@ -759,6 +779,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this; } + _GLIBCXX20_CONSTEXPR insert_iterator& operator=(typename _Container::value_type&& __value) { @@ -769,16 +790,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif /// Simply returns *this. + _GLIBCXX20_CONSTEXPR insert_iterator& operator*() { return *this; } /// Simply returns *this. (This %iterator does not @a move.) + _GLIBCXX20_CONSTEXPR insert_iterator& operator++() { return *this; } /// Simply returns *this. (This %iterator does not @a move.) + _GLIBCXX20_CONSTEXPR insert_iterator& operator++(int) { return *this; } @@ -796,6 +820,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * template parameter deduction, making the compiler match the correct * types for you. */ +#if __cplusplus > 201703L && defined __cpp_lib_concepts + template + constexpr insert_iterator<_Container> + inserter(_Container& __x, std::__detail::__range_iter_t<_Container> __i) + { return insert_iterator<_Container>(__x, __i); } +#else template inline insert_iterator<_Container> inserter(_Container& __x, _Iterator __i) @@ -803,6 +833,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return insert_iterator<_Container>(__x, typename _Container::iterator(__i)); } +#endif // @} group iterators diff --git a/libstdc++-v3/testsuite/24_iterators/back_insert_iterator/constexpr.cc b/libstdc++-v3/testsuite/24_iterators/back_insert_iterator/constexpr.cc new file mode 100644 index 00000000000..63b7854a0c9 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/back_insert_iterator/constexpr.cc @@ -0,0 +1,54 @@ +// 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 compile { target c++2a } } + +#include + +struct container +{ + using value_type = int; + + constexpr int* begin() { return std::begin(data); } + constexpr int* end() { return next; } + + constexpr void push_back(int val) + { + if (next == std::end(data)) + throw val; + *next++ = val; + } + + int data[3]; + int* next = std::begin(data); +}; + +constexpr bool +test01() +{ + container c; + std::back_insert_iterator iter; + iter = std::back_inserter(c); + *iter++ = 1; + int i = 2; + *iter = i; + *++iter = 3; + return c.data[0] == 1 && c.data[1] == 2 && c.data[2] == 3; +} + +static_assert( test01() ); diff --git a/libstdc++-v3/testsuite/24_iterators/front_insert_iterator/constexpr.cc b/libstdc++-v3/testsuite/24_iterators/front_insert_iterator/constexpr.cc new file mode 100644 index 00000000000..62bddad9f7c --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/front_insert_iterator/constexpr.cc @@ -0,0 +1,54 @@ +// 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 compile { target c++2a } } + +#include + +struct container +{ + using value_type = int; + + constexpr int* begin() { return next; } + constexpr int* end() { return std::end(data); } + + constexpr void push_front(int val) + { + if (next == std::begin(data)) + throw val; + *--next = val; + } + + int data[3]; + int* next = std::end(data); +}; + +constexpr bool +test01() +{ + container c; + std::front_insert_iterator iter; + iter = std::front_inserter(c); + *iter++ = 1; + int i = 2; + *iter = i; + *++iter = 3; + return c.data[0] == 3 && c.data[1] == 2 && c.data[2] == 1; +} + +static_assert( test01() ); diff --git a/libstdc++-v3/testsuite/24_iterators/headers/iterator/synopsis_c++17.cc b/libstdc++-v3/testsuite/24_iterators/headers/iterator/synopsis_c++17.cc index 0cbf5c3ecd9..590f3050efc 100644 --- a/libstdc++-v3/testsuite/24_iterators/headers/iterator/synopsis_c++17.cc +++ b/libstdc++-v3/testsuite/24_iterators/headers/iterator/synopsis_c++17.cc @@ -45,6 +45,10 @@ namespace std { // C++17 24.5, iterator adaptors: template class reverse_iterator; +#if __cplusplus == 201703L + // These operators are constrained in C++20 mode and so don't match + // these signatures. + template constexpr bool operator==(const reverse_iterator& x, @@ -74,6 +78,7 @@ namespace std { constexpr bool operator<=(const reverse_iterator& x, const reverse_iterator& y); +#endif // C++17 template constexpr auto @@ -92,18 +97,30 @@ namespace std { template class back_insert_iterator; template +#if __cplusplus > 201703L + constexpr +#endif back_insert_iterator back_inserter(Container& x); template class front_insert_iterator; template +#if __cplusplus > 201703L + constexpr +#endif front_insert_iterator front_inserter(Container& x); template class insert_iterator; +#if __cplusplus == 201703L + // This function template is defined differently in C++20 mode and so + // doesn't match this signature. template insert_iterator inserter(Container& x, Iterator i); + // These operators are constrained in C++20 mode and so don't match + // these signatures. + template class move_iterator; template @@ -135,6 +152,7 @@ namespace std { constexpr bool operator>=(const move_iterator& x, const move_iterator& y); +#endif // C++17 template constexpr diff --git a/libstdc++-v3/testsuite/24_iterators/insert_iterator/constexpr.cc b/libstdc++-v3/testsuite/24_iterators/insert_iterator/constexpr.cc new file mode 100644 index 00000000000..b4fc1b5e4e7 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/insert_iterator/constexpr.cc @@ -0,0 +1,57 @@ +// 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 compile { target c++2a } } + +#include + +struct container +{ + using value_type = int; + + constexpr int* begin() { return std::begin(data); } + constexpr int* end() { return last; } + + constexpr int* insert(int* pos, int val) + { + if (last == std::end(data)) + throw val; + for (int* i = last++; i != pos; --i) + i[1] = i[0]; + *pos = val; + return pos; + } + + int data[3]; + int* last = std::begin(data); +}; + +constexpr bool +test01() +{ + container c; + std::insert_iterator iter; + iter = std::inserter(c, c.begin()); + *iter++ = 1; + int i = 2; + *iter = i; + *++iter = 3; + return c.data[0] == 1 && c.data[1] == 2 && c.data[2] == 3; +} + +static_assert( test01() );