[gcc r16-3576] libstdc++: Conditionalize LWG 3569 changes to join_view

Patrick Palka ppalka@gcc.gnu.org
Thu Sep 4 13:57:55 GMT 2025


https://gcc.gnu.org/g:7f7f1878eedd8093d382e1e7b74649d7e97d5918

commit r16-3576-g7f7f1878eedd8093d382e1e7b74649d7e97d5918
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu Sep 4 09:57:36 2025 -0400

    libstdc++: Conditionalize LWG 3569 changes to join_view
    
    LWG 3569 adjusted join_view's iterator specification to handle non
    default-constructible iterators by wrapping the corresponding data member
    in std::optional, which we followed suit in r13-2649-g7aa80c82ecf3a3.
    
    But this wrapping is unnecessary for iterators that are already
    default-constructible.  Rather than unconditionally using std::optional
    here, which introduces time/space overhead, this patch conditionalizes
    our LWG 3569 changes on the iterator in question being non-forward (and
    thus non default-constructible).  We check forwardness instead of
    default-constructibility in order to accommodate input-only iterators
    that satisfy but do not model default_initializable, e.g. whose default
    constructor is underconstrained.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/ranges (join_view::_Iterator::_M_satisfy):
            Adjust to handle non-std::optional _M_inner as per before LWG 3569.
            (join_view::_Iterator::_M_get_inner): New.
            (join_view::_Iterator::_M_inner): Don't wrap in std::optional if
            the iterator is forward.  Initialize.
            (join_view::_Iterator::operator*): Use _M_get_inner instead
            of *_M_inner.
            (join_view::_Iterator::operator++): Likewise.
            (join_view::_Iterator::iter_move): Likewise.
            (join_view::_Iterator::iter_swap): Likewise.
    
    Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
    Reviewed-by: Jonathan Wakely <jwakely@redhat.com>

Diff:
---
 libstdc++-v3/include/std/ranges | 49 +++++++++++++++++++++++++++++++----------
 1 file changed, 37 insertions(+), 12 deletions(-)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 156d12b31ba1..bae50d06ca81 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -2889,7 +2889,12 @@ namespace views::__adaptor
 	      }
 
 	    if constexpr (_S_ref_is_glvalue)
-	      _M_inner.reset();
+	      {
+		if constexpr (forward_iterator<_Inner_iter>)
+		  _M_inner = _Inner_iter();
+		else
+		  _M_inner.reset();
+	      }
 	  }
 
 	  static constexpr auto
@@ -2929,6 +2934,24 @@ namespace views::__adaptor
 	      return *_M_parent->_M_outer;
 	  }
 
+	  constexpr _Inner_iter&
+	  _M_get_inner()
+	  {
+	    if constexpr (forward_iterator<_Inner_iter>)
+	      return _M_inner;
+	    else
+	      return *_M_inner;
+	  }
+
+	  constexpr const _Inner_iter&
+	  _M_get_inner() const
+	  {
+	    if constexpr (forward_iterator<_Inner_iter>)
+	      return _M_inner;
+	    else
+	      return *_M_inner;
+	  }
+
 	  constexpr
 	  _Iterator(_Parent* __parent, _Outer_iter __outer) requires forward_range<_Base>
 	    : _M_outer(std::move(__outer)), _M_parent(__parent)
@@ -2942,7 +2965,9 @@ namespace views::__adaptor
 	  [[no_unique_address]]
 	    __detail::__maybe_present_t<forward_range<_Base>, _Outer_iter> _M_outer
 	      = decltype(_M_outer)();
-	  optional<_Inner_iter> _M_inner;
+	  __conditional_t<forward_iterator<_Inner_iter>,
+			  _Inner_iter, optional<_Inner_iter>> _M_inner
+	    = decltype(_M_inner)();
 	  _Parent* _M_parent = nullptr;
 
 	public:
@@ -2966,7 +2991,7 @@ namespace views::__adaptor
 
 	  constexpr decltype(auto)
 	  operator*() const
-	  { return **_M_inner; }
+	  { return *_M_get_inner(); }
 
 	  // _GLIBCXX_RESOLVE_LIB_DEFECTS
 	  // 3500. join_view::iterator::operator->() is bogus
@@ -2974,7 +2999,7 @@ namespace views::__adaptor
 	  operator->() const
 	    requires __detail::__has_arrow<_Inner_iter>
 	      && copyable<_Inner_iter>
-	  { return *_M_inner; }
+	  { return _M_get_inner(); }
 
 	  constexpr _Iterator&
 	  operator++()
@@ -2985,7 +3010,7 @@ namespace views::__adaptor
 	      else
 		return *_M_parent->_M_inner;
 	    }();
-	    if (++*_M_inner == ranges::end(__inner_range))
+	    if (++_M_get_inner() == ranges::end(__inner_range))
 	      {
 		++_M_get_outer();
 		_M_satisfy();
@@ -3015,9 +3040,9 @@ namespace views::__adaptor
 	  {
 	    if (_M_outer == ranges::end(_M_parent->_M_base))
 	      _M_inner = ranges::end(__detail::__as_lvalue(*--_M_outer));
-	    while (*_M_inner == ranges::begin(__detail::__as_lvalue(*_M_outer)))
-	      *_M_inner = ranges::end(__detail::__as_lvalue(*--_M_outer));
-	    --*_M_inner;
+	    while (_M_get_inner() == ranges::begin(__detail::__as_lvalue(*_M_outer)))
+	      _M_get_inner() = ranges::end(__detail::__as_lvalue(*--_M_outer));
+	    --_M_get_inner();
 	    return *this;
 	  }
 
@@ -3044,14 +3069,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_get_inner())))
+	  { return ranges::iter_move(__i._M_get_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_get_inner(), __y._M_get_inner())))
 	    requires indirectly_swappable<_Inner_iter>
-	  { return ranges::iter_swap(*__x._M_inner, *__y._M_inner); }
+	  { return ranges::iter_swap(__x._M_get_inner(), __y._M_get_inner()); }
 
 	  friend _Iterator<!_Const>;
 	  template<bool> friend struct _Sentinel;


More information about the Libstdc++-cvs mailing list