From: Patrick Palka <ppalka@redhat.com>
To: Jonathan Wakely <jwakely@redhat.com>
Cc: Patrick Palka <ppalka@redhat.com>,
gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org
Subject: Re: [PATCH] libstdc++: Add missing bits of P0896R4 pertaining to [back|front]_insert_iterator
Date: Mon, 24 Feb 2020 13:53:00 -0000 [thread overview]
Message-ID: <alpine.DEB.2.22.395.2002240846350.1709@idea> (raw)
In-Reply-To: <20200224134311.GB9441@redhat.com>
On Mon, 24 Feb 2020, Jonathan Wakely wrote:
> On 24/02/20 08:29 -0500, Patrick Palka wrote:
> > 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;
>
> I think this will fail to compile with Clang, because it needs
> 'typename' there.
>
> > 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;
>
> Same here.
Ah right... Here's v2 of the patch that adds typename to those two
aliases and fixes the file extensions on the tests.
-- >8 --
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.
* 24_iterators/back_insert_iterator/pr93884.cc: New test.
* 24_iterators/front_insert_iterator/pr93884.cc: New test.
---
libstdc++-v3/include/bits/ranges_algobase.h | 4 +-
libstdc++-v3/include/bits/stl_iterator.h | 22 ++++++++++
.../back_insert_iterator/pr93884.cc | 44 +++++++++++++++++++
.../front_insert_iterator/pr93884.cc | 44 +++++++++++++++++++
4 files changed, 112 insertions(+), 2 deletions(-)
create mode 100644 libstdc++-v3/testsuite/24_iterators/back_insert_iterator/pr93884.cc
create mode 100644 libstdc++-v3/testsuite/24_iterators/front_insert_iterator/pr93884.cc
diff --git a/libstdc++-v3/include/bits/ranges_algobase.h b/libstdc++-v3/include/bits/ranges_algobase.h
index 807822e99c8..73f0205ba7f 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 = typename 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 = typename 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..caaa8c483b8 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<output_iterator_tag, void, void, void, void>
{
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;
+#endif
/// The only way to create this %iterator is with a container.
explicit
@@ -588,11 +599,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public iterator<output_iterator_tag, void, void, void, void>
{
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.cc b/libstdc++-v3/testsuite/24_iterators/back_insert_iterator/pr93884.cc
new file mode 100644
index 00000000000..5a6287ddd40
--- /dev/null
+++ b/libstdc++-v3/testsuite/24_iterators/back_insert_iterator/pr93884.cc
@@ -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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <iterator>
+#include <algorithm>
+#include <vector>
+#include <testsuite_hooks.h>
+
+namespace ranges = std::ranges;
+namespace views = std::views;
+
+void
+test01()
+{
+ auto v = std::vector<int>{};
+ auto i = views::iota(0, 10);
+ auto o = std::back_inserter(v);
+ static_assert(std::output_iterator<decltype(o), int>);
+ ranges::copy(i, o);
+ VERIFY( ranges::equal(v, i) );
+}
+
+int
+main()
+{
+ test01();
+}
diff --git a/libstdc++-v3/testsuite/24_iterators/front_insert_iterator/pr93884.cc b/libstdc++-v3/testsuite/24_iterators/front_insert_iterator/pr93884.cc
new file mode 100644
index 00000000000..eaf4e9a187f
--- /dev/null
+++ b/libstdc++-v3/testsuite/24_iterators/front_insert_iterator/pr93884.cc
@@ -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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <iterator>
+#include <algorithm>
+#include <deque>
+#include <testsuite_hooks.h>
+
+namespace ranges = std::ranges;
+namespace views = std::views;
+
+void
+test01()
+{
+ auto v = std::deque<int>{};
+ auto i = views::iota(0, 10);
+ auto o = std::front_inserter(v);
+ static_assert(std::output_iterator<decltype(o), int>);
+ ranges::copy(i, o);
+ VERIFY( ranges::equal(v | views::reverse, i) );
+}
+
+int
+main()
+{
+ test01();
+}
--
2.25.1.291.ge68e29171c
next prev parent reply other threads:[~2020-02-24 13:53 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-24 13:29 Patrick Palka
2020-02-24 13:37 ` Patrick Palka
2020-02-24 13:43 ` Jonathan Wakely
2020-02-24 13:53 ` Patrick Palka [this message]
2020-02-24 14:25 ` Jonathan Wakely
2020-02-24 17:17 ` Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.DEB.2.22.395.2002240846350.1709@idea \
--to=ppalka@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jwakely@redhat.com \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).