From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2136) id C18F1395ACC6; Wed, 17 Jun 2020 20:00:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C18F1395ACC6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1592424055; bh=I/ZLn2i8zI4qIVliGAWPdwdyHDppnYEZcWl9eh/tNsQ=; h=From:To:Subject:Date:From; b=VmAWx/NRXF1L14elYcq+P66GSAliaMU9DYdcD/q3avdjoNQvYPqfyhC8kH81jD1eI /RfSnEevR5vVwrL4b3Wus0iqHdz6SEqEmuq3lmkxQFVyFb9NAzNjCFvoZaxepNlOeZ gzS1DYL8gthRYcfr7jaqLAx3+s0Apzd4Orrti+20= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Aldy Hernandez To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc/devel/ranger] libstdc++: Implement C++20 changes to insert iterators X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/devel/ranger X-Git-Oldrev: 0302a2de7f143bc4ac2a963761abc684b4dd6ddb X-Git-Newrev: ae6076b5bc1e0b689eaa8521571b0516de336553 Message-Id: <20200617200055.C18F1395ACC6@sourceware.org> Date: Wed, 17 Jun 2020 20:00:55 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Jun 2020 20:00:55 -0000 https://gcc.gnu.org/g:ae6076b5bc1e0b689eaa8521571b0516de336553 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: --- libstdc++-v3/ChangeLog | 19 ++++++ libstdc++-v3/include/bits/iterator_concepts.h | 50 ++++++++++++++++ libstdc++-v3/include/bits/range_access.h | 27 ++------- libstdc++-v3/include/bits/stl_iterator.h | 67 ++++++++++++++++------ .../24_iterators/back_insert_iterator/constexpr.cc | 54 +++++++++++++++++ .../front_insert_iterator/constexpr.cc | 54 +++++++++++++++++ .../headers/iterator/synopsis_c++17.cc | 18 ++++++ .../24_iterators/insert_iterator/constexpr.cc | 57 ++++++++++++++++++ 8 files changed, 305 insertions(+), 41 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index e9bb4bf3980..c529206a4b9 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,24 @@ 2020-03-27 Jonathan Wakely + * 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. + * include/std/type_traits (__is_array_convertible): Move definition to immediately after is_convertible. 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() );