public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH 2/4] libstdc++: Implement LWG 3569 changes to join_view::_Iterator
Date: Mon, 12 Sep 2022 12:45:29 -0400	[thread overview]
Message-ID: <20220912164531.1742034-2-ppalka@redhat.com> (raw)
In-Reply-To: <20220912164531.1742034-1-ppalka@redhat.com>

Tested on x86_64-pc-linux-gnu, does this look OK for trunk only?

libstdc++-v3/ChangeLog:

	* include/std/ranges (join_view::_Iterator::_M_satisfy):
	Adjust resetting _M_inner as per LWG 3569.
	(join_view::_Iterator::_M_inner): Wrap in std::optional
	as per LWG 3569.
	(join_view::_Iterator::_Iterator): Relax constraints as
	per LWG 3569.
	(join_view::_Iterator::operator*): Adjust as per LWG 3569.
	(join_view::_Iterator::operator->): Likewise.
	(join_view::_Iterator::operator++): Likewise.
	(join_view::_Iterator::operator--): Likewise.
	(join_view::_Iterator::iter_move): Likewise.
	(join_view::_Iterator::iter_swap): Likewise.
	* testsuite/std/ranges/adaptor/join.cc (test14): New test.
---
 libstdc++-v3/include/std/ranges               | 28 +++++++++----------
 .../testsuite/std/ranges/adaptors/join.cc     | 17 +++++++++++
 2 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 20eb4e82ac8..6297ce7cee3 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -2746,7 +2746,7 @@ namespace views::__adaptor
 	      }
 
 	    if constexpr (_S_ref_is_glvalue)
-	      _M_inner = _Inner_iter();
+	      _M_inner.reset();
 	  }
 
 	  static constexpr auto
@@ -2769,7 +2769,7 @@ namespace views::__adaptor
 	  using _Inner_iter = join_view::_Inner_iter<_Const>;
 
 	  _Outer_iter _M_outer = _Outer_iter();
-	  _Inner_iter _M_inner = _Inner_iter();
+	  optional<_Inner_iter> _M_inner;
 	  _Parent* _M_parent = nullptr;
 
 	public:
@@ -2780,9 +2780,7 @@ namespace views::__adaptor
 	    = common_type_t<range_difference_t<_Base>,
 			    range_difference_t<range_reference_t<_Base>>>;
 
-	  _Iterator() requires (default_initializable<_Outer_iter>
-				&& default_initializable<_Inner_iter>)
-	    = default;
+	  _Iterator() requires default_initializable<_Outer_iter> = default;
 
 	  constexpr
 	  _Iterator(_Parent* __parent, _Outer_iter __outer)
@@ -2801,7 +2799,7 @@ namespace views::__adaptor
 
 	  constexpr decltype(auto)
 	  operator*() const
-	  { return *_M_inner; }
+	  { return **_M_inner; }
 
 	  // _GLIBCXX_RESOLVE_LIB_DEFECTS
 	  // 3500. join_view::iterator::operator->() is bogus
@@ -2809,7 +2807,7 @@ namespace views::__adaptor
 	  operator->() const
 	    requires __detail::__has_arrow<_Inner_iter>
 	      && copyable<_Inner_iter>
-	  { return _M_inner; }
+	  { return *_M_inner; }
 
 	  constexpr _Iterator&
 	  operator++()
@@ -2820,7 +2818,7 @@ namespace views::__adaptor
 	      else
 		return *_M_parent->_M_inner;
 	    }();
-	    if (++_M_inner == ranges::end(__inner_range))
+	    if (++*_M_inner == ranges::end(__inner_range))
 	      {
 		++_M_outer;
 		_M_satisfy();
@@ -2850,9 +2848,9 @@ namespace views::__adaptor
 	  {
 	    if (_M_outer == ranges::end(_M_parent->_M_base))
 	      _M_inner = ranges::end(*--_M_outer);
-	    while (_M_inner == ranges::begin(*_M_outer))
-	      _M_inner = ranges::end(*--_M_outer);
-	    --_M_inner;
+	    while (*_M_inner == ranges::begin(*_M_outer))
+	      *_M_inner = ranges::end(*--_M_outer);
+	    --*_M_inner;
 	    return *this;
 	  }
 
@@ -2879,14 +2877,14 @@ namespace views::__adaptor
 
 	  friend constexpr decltype(auto)
 	  iter_move(const _Iterator& __i)
-	  noexcept(noexcept(ranges::iter_move(__i._M_inner)))
-	  { return ranges::iter_move(__i._M_inner); }
+	  noexcept(noexcept(ranges::iter_move(*__i._M_inner)))
+	  { return ranges::iter_move(*__i._M_inner); }
 
 	  friend constexpr void
 	  iter_swap(const _Iterator& __x, const _Iterator& __y)
-	    noexcept(noexcept(ranges::iter_swap(__x._M_inner, __y._M_inner)))
+	    noexcept(noexcept(ranges::iter_swap(*__x._M_inner, *__y._M_inner)))
 	    requires indirectly_swappable<_Inner_iter>
-	  { return ranges::iter_swap(__x._M_inner, __y._M_inner); }
+	  { return ranges::iter_swap(*__x._M_inner, *__y._M_inner); }
 
 	  friend _Iterator<!_Const>;
 	  template<bool> friend struct _Sentinel;
diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc
index 530ab6663b5..afc11d4bd7a 100644
--- a/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc
+++ b/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc
@@ -21,6 +21,7 @@
 #include <algorithm>
 #include <array>
 #include <ranges>
+#include <sstream>
 #include <string>
 #include <string_view>
 #include <vector>
@@ -217,6 +218,21 @@ test13()
   std::vector<std::vector<int>> v{{5, 6, 7}};
   v | l | std::views::join;
 }
+
+void
+test14()
+{
+  // LWG 3569: join_view fails to support ranges of ranges with
+  // non-default_initializable iterators
+  auto ss = std::istringstream{"1 2 3"};
+  auto v = views::single(views::istream<int>(ss));
+  using inner = ranges::range_reference_t<decltype(v)>;
+  static_assert(ranges::input_range<inner>
+		&& !ranges::forward_range<inner>
+		&& !std::default_initializable<ranges::iterator_t<inner>>);
+  VERIFY( ranges::equal(v | views::join, (int[]){1, 2, 3}) );
+}
+
 int
 main()
 {
@@ -233,4 +249,5 @@ main()
   test11();
   test12();
   test13();
+  test14();
 }
-- 
2.37.3.542.gdd3f6c4cae


  reply	other threads:[~2022-09-12 16:45 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-12 16:45 [PATCH 1/4] libstdc++: Add already-accepted <ranges> testcase [PR106320] Patrick Palka
2022-09-12 16:45 ` Patrick Palka [this message]
2022-09-13 10:57   ` [PATCH 2/4] libstdc++: Implement LWG 3569 changes to join_view::_Iterator Jonathan Wakely
2022-09-12 16:45 ` [PATCH 3/4] libstdc++: Implement ranges::chunk_view from P2442R1 Patrick Palka
2022-09-13 11:10   ` Jonathan Wakely
2022-09-12 16:45 ` [PATCH 4/4] libstdc++: Implement ranges::slide_view " Patrick Palka
2022-09-13 11:12   ` 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=20220912164531.1742034-2-ppalka@redhat.com \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --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).