[PATCH 1/5] libstdc++: Implement P2325 changes to default-constructibilty of views

Patrick Palka ppalka@redhat.com
Thu Jun 17 15:22:02 GMT 2021


This implements the wording changes of P2325R3 "Views should not be
required to be default constructible".  Changes are relatively
straightforward, besides perhaps those to __box (which now stands
for copyable-box instead of semiregular-box) and __non_propagating_cache.

For __box, this patch implements the recommended practice to also avoid
std::optional when the boxed type is nothrow_move/copy_constructible.

For __non_propagating_cache, now that it's used by split_view::_M_current,
we need to add assignment from a value of the underlying type to the
subset of the std::optional API implemented for the cache (needed by
split_view::begin()).  Hence the new __non_propagating_cache::operator=
overload.

While we're changing __box, this fixes the undesirable list-init in
the constuctors of the partial specialization as reported in PR100475
comment #7.

libstdc++-v3/ChangeLog:

	* include/bits/iterator_concepts.h (weakly_incrementable): Remove
	default_initializable requirement.
	* include/bits/ranges_base.h (ranges::view): Likewise.
	* include/bits/ranges_util.h (subrange): Constrain the default
	ctor.
	* include/bits/stl_iterator.h (back_insert_iterator): Remove the
	default ctor.
	(front_insert_iterator): Likewise.
	(insert_iterator): Likewise.  Remove NSDMIs.
	(common_iterator): Constrain the default ctor.
	(counted_iterator): Likewise.
	* include/bits/stream_iterator.h (ostream_iterator): Remove the
	default ctor.
	* include/std/ranges (__detail::__box::operator=): Handle
	self-assignment in the primary template.
	(__detail::__box): In the partial specialization: adjust
	constraints as per P2325.  Add specialized operator= for the
	case when the wrapped type is not copyable.  Constrain the
	default ctor.  Avoid list-initialization.
	(single_view): Constraint the default ctor.
	(iota_view): Relax semiregular constraint to copyable.
	Constrain the default ctor.
	(iota_view::_Iterator): Constraint the default ctor.
	(basic_istream_view): Remove the default ctor.  Remove NSDMIs.
	Remove redundant checks for empty _M_stream.
	(basic_istream_view::_Iterator): Likewise.
	(ref_view): Remove the default ctor.  Remove NSDMIs.
	(ref_view::_Iterator): Constrain the default ctor.
	(__detail::__non_propagating_cache::operator=): Define overload
	for assigning from a value of the underlying type.
	(filter_view): Likewise.
	(filter_view::_Iterator): Likewise.
	(transform_view): Likewise.
	(transform_view::_Iterator): Likewise.
	(take_view): Likewise.
	(take_view::_Iterator): Likewise.
	(take_while_view): Likewise.
	(take_while_view::_Iterator): Likewise.
	(drop_while_view): Likewise.
	(drop_while_view::_Iterator): Likewise.
	(join_view): Likewise.
	(split_view::_OuterIter::__current): Adjust after changing the
	type of _M_current.
	(split_view::_M_current): Wrap it in a __non_propagating_cache.
	(split_view::split_view): Constrain the default ctor.
	(common_view): Constrain the default ctor.
	(reverse_view): Likewise.
	(elements_view): Likewise.
	* include/std/span (enable_view<span<_ElementType, _Extent>>):
	Define this partial specialization to true unconditionally.
	* include/std/version (__cpp_lib_ranges): Adjust value.
	* testsuite/24_iterators/back_insert_iterator/constexpr.cc:
	Don't attempt to default construct a back_insert_iterator.
	* testsuite/24_iterators/front_insert_iterator/constexpr.cc:
	Don't attempt to default construct a front_insert_iterator.
	* testsuite/24_iterators/insert_iterator/constexpr.cc:
	Don't attempt to default construct an insert_iterator.
	* testsuite/24_iterators/ostream_iterator/requirements/constexpr.cc:
	Remove this test for default constructibility of ostream_iterator.
	* testsuite/std/ranges/97600.cc: Don't attempt to default
	construct a basic_istream_view.
	* testsuite/std/ranges/adaptors/detail/semiregular_box.cc:
	Rename to ...
	* testsuite/std/ranges/adaptors/detail/copyable_box.cc: ... this.
	(test02): Adjust now that __box is copyable-box not
	semiregular-box.
	(test03): New test.
	* testsuite/std/ranges/p2325.cc: New test.
	* testsuite/std/ranges/single_view.cc (test06): New test.
	* testsuite/std/ranges/view.cc: Adjust now that view doesn't
	require default_initializable.
---
 libstdc++-v3/include/bits/iterator_concepts.h |   3 +-
 libstdc++-v3/include/bits/ranges_base.h       |   3 +-
 libstdc++-v3/include/bits/ranges_util.h       |   2 +-
 libstdc++-v3/include/bits/stl_iterator.h      |  16 +-
 libstdc++-v3/include/bits/stream_iterator.h   |   5 -
 libstdc++-v3/include/std/ranges               | 160 ++++++++++++------
 libstdc++-v3/include/std/span                 |   3 +-
 libstdc++-v3/include/std/version              |   2 +-
 .../back_insert_iterator/constexpr.cc         |   3 +-
 .../front_insert_iterator/constexpr.cc        |   3 +-
 .../24_iterators/insert_iterator/constexpr.cc |   3 +-
 .../requirements/constexpr.cc                 |  24 ---
 libstdc++-v3/testsuite/std/ranges/97600.cc    |   3 +-
 .../{semiregular_box.cc => copyable_box.cc}   |  51 +++++-
 libstdc++-v3/testsuite/std/ranges/p2325.cc    | 155 +++++++++++++++++
 .../testsuite/std/ranges/single_view.cc       |  15 ++
 libstdc++-v3/testsuite/std/ranges/view.cc     |   2 +-
 17 files changed, 335 insertions(+), 118 deletions(-)
 delete mode 100644 libstdc++-v3/testsuite/24_iterators/ostream_iterator/requirements/constexpr.cc
 rename libstdc++-v3/testsuite/std/ranges/adaptors/detail/{semiregular_box.cc => copyable_box.cc} (70%)
 create mode 100644 libstdc++-v3/testsuite/std/ranges/p2325.cc

diff --git a/libstdc++-v3/include/bits/iterator_concepts.h b/libstdc++-v3/include/bits/iterator_concepts.h
index 11748e5ed7b..c273056c204 100644
--- a/libstdc++-v3/include/bits/iterator_concepts.h
+++ b/libstdc++-v3/include/bits/iterator_concepts.h
@@ -594,8 +594,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   /// Requirements on types that can be incremented with ++.
   template<typename _Iter>
-    concept weakly_incrementable = default_initializable<_Iter>
-      && movable<_Iter>
+    concept weakly_incrementable = movable<_Iter>
       && requires(_Iter __i)
       {
 	typename iter_difference_t<_Iter>;
diff --git a/libstdc++-v3/include/bits/ranges_base.h b/libstdc++-v3/include/bits/ranges_base.h
index 25af4b742a6..9d749c8d9b7 100644
--- a/libstdc++-v3/include/bits/ranges_base.h
+++ b/libstdc++-v3/include/bits/ranges_base.h
@@ -619,8 +619,7 @@ namespace ranges
   /// [range.view] The ranges::view concept.
   template<typename _Tp>
     concept view
-      = range<_Tp> && movable<_Tp> && default_initializable<_Tp>
-	&& enable_view<_Tp>;
+      = range<_Tp> && movable<_Tp> && enable_view<_Tp>;
 
   // [range.refinements]
 
diff --git a/libstdc++-v3/include/bits/ranges_util.h b/libstdc++-v3/include/bits/ranges_util.h
index dd829ed957f..d7b12b3d985 100644
--- a/libstdc++-v3/include/bits/ranges_util.h
+++ b/libstdc++-v3/include/bits/ranges_util.h
@@ -241,7 +241,7 @@ namespace ranges
       [[no_unique_address]] _Size<__size_type> _M_size = {};
 
     public:
-      subrange() = default;
+      subrange() requires default_initializable<_It> = default;
 
       constexpr
       subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s)
diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h
index 8768624b7d1..6ec046b597b 100644
--- a/libstdc++-v3/include/bits/stl_iterator.h
+++ b/libstdc++-v3/include/bits/stl_iterator.h
@@ -639,8 +639,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       typedef _Container          container_type;
 #if __cplusplus > 201703L
       using difference_type = ptrdiff_t;
-
-      constexpr back_insert_iterator() noexcept : container(nullptr) { }
 #endif
 
       /// The only way to create this %iterator is with a container.
@@ -742,8 +740,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       typedef _Container          container_type;
 #if __cplusplus > 201703L
       using difference_type = ptrdiff_t;
-
-      constexpr front_insert_iterator() noexcept : container(nullptr) { }
 #endif
 
       /// The only way to create this %iterator is with a container.
@@ -843,17 +839,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
 #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;
-
+#endif
     protected:
       _Container* container;
       _Iter iter;
-#endif
 
     public:
       /// A nested typedef for the type of whatever container you used.
@@ -861,8 +852,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
 #if __cplusplus > 201703L && defined __cpp_lib_concepts
       using difference_type = ptrdiff_t;
-
-      insert_iterator() = default;
 #endif
 
       /**
@@ -1740,6 +1729,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     constexpr
     common_iterator()
     noexcept(is_nothrow_default_constructible_v<_It>)
+    requires default_initializable<_It>
     : _M_it(), _M_index(0)
     { }
 
@@ -2117,7 +2107,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       // iterator_concept defined in __counted_iter_concept
       // iterator_category defined in __counted_iter_cat
 
-      constexpr counted_iterator() = default;
+      constexpr counted_iterator() requires default_initializable<_It> = default;
 
       constexpr
       counted_iterator(_It __i, iter_difference_t<_It> __n)
diff --git a/libstdc++-v3/include/bits/stream_iterator.h b/libstdc++-v3/include/bits/stream_iterator.h
index fd8920b8d01..d07474d4996 100644
--- a/libstdc++-v3/include/bits/stream_iterator.h
+++ b/libstdc++-v3/include/bits/stream_iterator.h
@@ -192,11 +192,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       const _CharT*	_M_string;
 
     public:
-#if __cplusplus > 201703L
-      constexpr ostream_iterator() noexcept
-      : _M_stream(nullptr), _M_string(nullptr) { }
-#endif
-
       /// Construct from an ostream.
       ostream_iterator(ostream_type& __s)
       : _M_stream(std::__addressof(__s)), _M_string(0) {}
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 73c35dafd29..f96adf63d10 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -113,10 +113,13 @@ namespace ranges
 	noexcept(is_nothrow_copy_constructible_v<_Tp>)
 	requires (!copyable<_Tp>)
 	{
-	  if ((bool)__that)
-	    this->emplace(*__that);
-	  else
-	    this->reset();
+	  if (this != std::__addressof(__that))
+	    {
+	      if ((bool)__that)
+		this->emplace(*__that);
+	      else
+		this->reset();
+	    }
 	  return *this;
 	}
 
@@ -125,37 +128,42 @@ namespace ranges
 	noexcept(is_nothrow_move_constructible_v<_Tp>)
 	requires (!movable<_Tp>)
 	{
-	  if ((bool)__that)
-	    this->emplace(std::move(*__that));
-	  else
-	    this->reset();
+	  if (this != std::__addressof(__that))
+	    {
+	      if ((bool)__that)
+		this->emplace(std::move(*__that));
+	      else
+		this->reset();
+	    }
 	  return *this;
 	}
       };
 
-    // For types which are already semiregular, this specialization of the
-    // semiregular wrapper stores the object directly without going through
+    // For types which are already copyable, this specialization of the
+    // copyable wrapper stores the object directly without going through
     // std::optional.  It provides just the subset of the primary template's
     // API that we currently use.
-    template<__boxable _Tp> requires semiregular<_Tp>
+    template<__boxable _Tp>
+      requires copyable<_Tp> || (is_nothrow_move_constructible_v<_Tp>
+				 && is_nothrow_copy_constructible_v<_Tp>)
       struct __box<_Tp>
       {
       private:
 	[[no_unique_address]] _Tp _M_value = _Tp();
 
       public:
-	__box() = default;
+	__box() requires default_initializable<_Tp> = default;
 
 	constexpr explicit
 	__box(const _Tp& __t)
 	noexcept(is_nothrow_copy_constructible_v<_Tp>)
-	: _M_value{__t}
+	: _M_value(__t)
 	{ }
 
 	constexpr explicit
 	__box(_Tp&& __t)
 	noexcept(is_nothrow_move_constructible_v<_Tp>)
-	: _M_value{std::move(__t)}
+	: _M_value(std::move(__t))
 	{ }
 
 	template<typename... _Args>
@@ -166,6 +174,38 @@ namespace ranges
 	  : _M_value(std::forward<_Args>(__args)...)
 	  { }
 
+	__box(const __box&) = default;
+	__box(__box&&) = default;
+	__box& operator=(const __box&) requires copyable<_Tp> = default;
+	__box& operator=(__box&&) requires copyable<_Tp> = default;
+
+	// When _Tp is nothrow_copy_constructible but not copy_assignable,
+	// copy assignment is implemented via destroy-then-copy-construct.
+	constexpr __box&
+	operator=(const __box& __that) noexcept
+	{
+	  static_assert(is_nothrow_copy_constructible_v<_Tp>);
+	  if (this != std::__addressof(__that))
+	    {
+	      _M_value.~_Tp();
+	      std::construct_at(std::__addressof(_M_value), *__that);
+	    }
+	  return *this;
+	}
+
+	// Likewise for move assignment.
+	constexpr __box&
+	operator=(__box&& __that) noexcept
+	{
+	  static_assert(is_nothrow_move_constructible_v<_Tp>);
+	  if (this != std::__addressof(__that))
+	    {
+	      _M_value.~_Tp();
+	      std::construct_at(std::__addressof(_M_value), std::move(*__that));
+	    }
+	  return *this;
+	}
+
 	constexpr bool
 	has_value() const noexcept
 	{ return true; };
@@ -193,7 +233,7 @@ namespace ranges
     class single_view : public view_interface<single_view<_Tp>>
     {
     public:
-      single_view() = default;
+      single_view() requires default_initializable<_Tp> = default;
 
       constexpr explicit
       single_view(const _Tp& __t)
@@ -308,7 +348,7 @@ namespace ranges
   template<weakly_incrementable _Winc,
 	   semiregular _Bound = unreachable_sentinel_t>
     requires std::__detail::__weakly_eq_cmp_with<_Winc, _Bound>
-      && semiregular<_Winc>
+      && copyable<_Winc>
     class iota_view : public view_interface<iota_view<_Winc, _Bound>>
     {
     private:
@@ -337,7 +377,7 @@ namespace ranges
 	using value_type = _Winc;
 	using difference_type = __detail::__iota_diff_t<_Winc>;
 
-	_Iterator() = default;
+	_Iterator() requires default_initializable<_Winc> = default;
 
 	constexpr explicit
 	_Iterator(_Winc __value)
@@ -534,7 +574,7 @@ namespace ranges
       [[no_unique_address]] _Bound _M_bound = _Bound();
 
     public:
-      iota_view() = default;
+      iota_view() requires default_initializable<_Winc> = default;
 
       constexpr explicit
       iota_view(_Winc __value)
@@ -643,8 +683,6 @@ namespace views
     : public view_interface<basic_istream_view<_Val, _CharT, _Traits>>
     {
     public:
-      basic_istream_view() = default;
-
       constexpr explicit
       basic_istream_view(basic_istream<_CharT, _Traits>& __stream)
 	: _M_stream(std::__addressof(__stream))
@@ -653,8 +691,7 @@ namespace views
       constexpr auto
       begin()
       {
-	if (_M_stream != nullptr)
-	  *_M_stream >> _M_object;
+	*_M_stream >> _M_object;
 	return _Iterator{this};
       }
 
@@ -663,8 +700,8 @@ namespace views
       { return default_sentinel; }
 
     private:
-      basic_istream<_CharT, _Traits>* _M_stream = nullptr;
-      _Val _M_object = _Val();
+      basic_istream<_CharT, _Traits>* _M_stream;
+      _Val _M_object;
 
       struct _Iterator
       {
@@ -673,8 +710,6 @@ namespace views
 	using difference_type = ptrdiff_t;
 	using value_type = _Val;
 
-	_Iterator() = default;
-
 	constexpr explicit
 	_Iterator(basic_istream_view* __parent) noexcept
 	  : _M_parent(__parent)
@@ -688,7 +723,6 @@ namespace views
 	_Iterator&
 	operator++()
 	{
-	  __glibcxx_assert(_M_parent->_M_stream != nullptr);
 	  *_M_parent->_M_stream >> _M_parent->_M_object;
 	  return *this;
 	}
@@ -699,21 +733,18 @@ namespace views
 
 	_Val&
 	operator*() const
-	{
-	  __glibcxx_assert(_M_parent->_M_stream != nullptr);
-	  return _M_parent->_M_object;
-	}
+	{ return _M_parent->_M_object; }
 
 	friend bool
 	operator==(const _Iterator& __x, default_sentinel_t)
 	{ return __x._M_at_end(); }
 
       private:
-	basic_istream_view* _M_parent = nullptr;
+	basic_istream_view* _M_parent;
 
 	bool
 	_M_at_end() const
-	{ return _M_parent == nullptr || !*_M_parent->_M_stream; }
+	{ return !*_M_parent->_M_stream; }
       };
 
       friend _Iterator;
@@ -1017,15 +1048,12 @@ namespace views::__adaptor
     class ref_view : public view_interface<ref_view<_Range>>
     {
     private:
-      _Range* _M_r = nullptr;
+      _Range* _M_r;
 
       static void _S_fun(_Range&); // not defined
       static void _S_fun(_Range&&) = delete;
 
     public:
-      constexpr
-      ref_view() noexcept = default;
-
       template<__detail::__not_same_as<ref_view> _Tp>
 	requires convertible_to<_Tp, _Range&>
 	  && requires { _S_fun(declval<_Tp>()); }
@@ -1205,6 +1233,16 @@ namespace views::__adaptor
 	  return *this;
 	}
 
+	constexpr __non_propagating_cache&
+	operator=(_Tp __val)
+	{
+	  this->_M_reset();
+	  std::construct_at(std::__addressof(this->_M_payload._M_payload),
+			    std::in_place, std::move(__val));
+	  this->_M_payload._M_engaged = true;
+	  return *this;
+	}
+
 	constexpr _Tp&
 	operator*() noexcept
 	{ return this->_M_get(); }
@@ -1382,7 +1420,7 @@ namespace views::__adaptor
 	using value_type = range_value_t<_Vp>;
 	using difference_type = range_difference_t<_Vp>;
 
-	_Iterator() = default;
+	_Iterator() requires default_initializable<_Vp_iter> = default;
 
 	constexpr
 	_Iterator(filter_view* __parent, _Vp_iter __current)
@@ -1494,7 +1532,9 @@ namespace views::__adaptor
       _Vp _M_base = _Vp();
 
     public:
-      filter_view() = default;
+      filter_view() requires (default_initializable<_Vp>
+			      && default_initializable<_Pred>)
+	= default;
 
       constexpr
       filter_view(_Vp __base, _Pred __pred)
@@ -1643,7 +1683,7 @@ namespace views::__adaptor
 	    = remove_cvref_t<invoke_result_t<_Fp&, range_reference_t<_Base>>>;
 	  using difference_type = range_difference_t<_Base>;
 
-	  _Iterator() = default;
+	  _Iterator() requires default_initializable<_Base_iter> = default;
 
 	  constexpr
 	  _Iterator(_Parent* __parent, _Base_iter __current)
@@ -1858,7 +1898,9 @@ namespace views::__adaptor
       _Vp _M_base = _Vp();
 
     public:
-      transform_view() = default;
+      transform_view() requires (default_initializable<_Vp>
+				 && default_initializable<_Fp>)
+	= default;
 
       constexpr
       transform_view(_Vp __base, _Fp __fun)
@@ -1993,7 +2035,7 @@ namespace views::__adaptor
       _Vp _M_base = _Vp();
 
     public:
-      take_view() = default;
+      take_view() requires default_initializable<_Vp> = default;
 
       constexpr
       take_view(_Vp base, range_difference_t<_Vp> __count)
@@ -2177,7 +2219,9 @@ namespace views::__adaptor
       _Vp _M_base = _Vp();
 
     public:
-      take_while_view() = default;
+      take_while_view() requires (default_initializable<_Vp>
+				  && default_initializable<_Pred>)
+	= default;
 
       constexpr
       take_while_view(_Vp base, _Pred __pred)
@@ -2265,7 +2309,7 @@ namespace views::__adaptor
 				      _M_cached_begin;
 
     public:
-      drop_view() = default;
+      drop_view() requires default_initializable<_Vp> = default;
 
       constexpr
       drop_view(_Vp __base, range_difference_t<_Vp> __count)
@@ -2381,7 +2425,9 @@ namespace views::__adaptor
       _Vp _M_base = _Vp();
 
     public:
-      drop_while_view() = default;
+      drop_while_view() requires (default_initializable<_Vp>
+				  && default_initializable<_Pred>)
+	= default;
 
       constexpr
       drop_while_view(_Vp __base, _Pred __pred)
@@ -2571,7 +2617,9 @@ namespace views::__adaptor
 	    = common_type_t<range_difference_t<_Base>,
 			    range_difference_t<range_reference_t<_Base>>>;
 
-	  _Iterator() = default;
+	  _Iterator() requires (default_initializable<_Outer_iter>
+				&& default_initializable<_Inner_iter>)
+	    = default;
 
 	  constexpr
 	  _Iterator(_Parent* __parent, _Outer_iter __outer)
@@ -2724,7 +2772,7 @@ namespace views::__adaptor
       _Vp _M_base = _Vp();
 
     public:
-      join_view() = default;
+      join_view() requires default_initializable<_Vp> = default;
 
       constexpr explicit
       join_view(_Vp __base)
@@ -2891,7 +2939,7 @@ namespace views::__adaptor
 	    if constexpr (forward_range<_Vp>)
 	      return _M_current;
 	    else
-	      return _M_parent->_M_current;
+	      return *_M_parent->_M_current;
 	  }
 
 	  constexpr auto&
@@ -2900,7 +2948,7 @@ namespace views::__adaptor
 	    if constexpr (forward_range<_Vp>)
 	      return _M_current;
 	    else
-	      return _M_parent->_M_current;
+	      return *_M_parent->_M_current;
 	  }
 
 	  _Parent* _M_parent = nullptr;
@@ -3146,12 +3194,14 @@ namespace views::__adaptor
       // XXX: _M_current is "present only if !forward_range<V>"
       [[no_unique_address]]
 	__detail::__maybe_present_t<!forward_range<_Vp>,
-				    iterator_t<_Vp>> _M_current;
+	  __detail::__non_propagating_cache<iterator_t<_Vp>>> _M_current;
       _Vp _M_base = _Vp();
 
 
     public:
-      split_view() = default;
+      split_view() requires (default_initializable<_Vp>
+			     && default_initializable<_Pattern>)
+	= default;
 
       constexpr
       split_view(_Vp __base, _Pattern __pattern)
@@ -3282,7 +3332,7 @@ namespace views::__adaptor
       _Vp _M_base = _Vp();
 
     public:
-      common_view() = default;
+      common_view() requires default_initializable<_Vp> = default;
 
       constexpr explicit
       common_view(_Vp __r)
@@ -3413,7 +3463,7 @@ namespace views::__adaptor
       _Vp _M_base = _Vp();
 
     public:
-      reverse_view() = default;
+      reverse_view() requires default_initializable<_Vp> = default;
 
       constexpr explicit
       reverse_view(_Vp __r)
@@ -3555,7 +3605,7 @@ namespace views::__adaptor
     class elements_view : public view_interface<elements_view<_Vp, _Nm>>
     {
     public:
-      elements_view() = default;
+      elements_view() requires default_initializable<_Vp> = default;
 
       constexpr explicit
       elements_view(_Vp base)
@@ -3676,7 +3726,7 @@ namespace views::__adaptor
 	    = remove_cvref_t<tuple_element_t<_Nm, range_value_t<_Base>>>;
 	  using difference_type = range_difference_t<_Base>;
 
-	  _Iterator() = default;
+	  _Iterator() requires default_initializable<iterator_t<_Base>> = default;
 
 	  constexpr explicit
 	  _Iterator(iterator_t<_Base> current)
diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span
index 09bdcd69afb..63f0a8f6279 100644
--- a/libstdc++-v3/include/std/span
+++ b/libstdc++-v3/include/std/span
@@ -447,8 +447,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     // Opt-in to view concept
     template<typename _ElementType, size_t _Extent>
       inline constexpr bool
-	enable_view<span<_ElementType, _Extent>>
-	  = _Extent == 0 || _Extent == dynamic_extent;
+	enable_view<span<_ElementType, _Extent>> = true;
   }
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version
index 8d0b2b95f34..e3ab9b4c7c0 100644
--- a/libstdc++-v3/include/std/version
+++ b/libstdc++-v3/include/std/version
@@ -240,7 +240,7 @@
 #define __cpp_lib_math_constants 201907L
 #define __cpp_lib_polymorphic_allocator 201902L
 #if __cpp_lib_concepts
-# define __cpp_lib_ranges 201911L
+# define __cpp_lib_ranges 202106L
 #endif
 #if __cpp_lib_atomic_wait || _GLIBCXX_HAVE_POSIX_SEMAPHORE
 # define __cpp_lib_semaphore 201907L
diff --git a/libstdc++-v3/testsuite/24_iterators/back_insert_iterator/constexpr.cc b/libstdc++-v3/testsuite/24_iterators/back_insert_iterator/constexpr.cc
index bef2289ba79..27acd071df1 100644
--- a/libstdc++-v3/testsuite/24_iterators/back_insert_iterator/constexpr.cc
+++ b/libstdc++-v3/testsuite/24_iterators/back_insert_iterator/constexpr.cc
@@ -42,8 +42,7 @@ constexpr bool
 test01()
 {
   container c;
-  std::back_insert_iterator<container> iter;
-  iter = std::back_inserter(c);
+  std::back_insert_iterator<container> iter = std::back_inserter(c);
   *iter++ = 1;
   int i = 2;
   *iter = i;
diff --git a/libstdc++-v3/testsuite/24_iterators/front_insert_iterator/constexpr.cc b/libstdc++-v3/testsuite/24_iterators/front_insert_iterator/constexpr.cc
index 7b4c990b107..cff7f6a4524 100644
--- a/libstdc++-v3/testsuite/24_iterators/front_insert_iterator/constexpr.cc
+++ b/libstdc++-v3/testsuite/24_iterators/front_insert_iterator/constexpr.cc
@@ -42,8 +42,7 @@ constexpr bool
 test01()
 {
   container c;
-  std::front_insert_iterator<container> iter;
-  iter = std::front_inserter(c);
+  std::front_insert_iterator<container> iter = std::front_inserter(c);
   *iter++ = 1;
   int i = 2;
   *iter = i;
diff --git a/libstdc++-v3/testsuite/24_iterators/insert_iterator/constexpr.cc b/libstdc++-v3/testsuite/24_iterators/insert_iterator/constexpr.cc
index e74df3eb5d5..e326b01d534 100644
--- a/libstdc++-v3/testsuite/24_iterators/insert_iterator/constexpr.cc
+++ b/libstdc++-v3/testsuite/24_iterators/insert_iterator/constexpr.cc
@@ -51,8 +51,7 @@ constexpr bool
 test01()
 {
   container c;
-  std::insert_iterator<container> iter;
-  iter = std::inserter(c, c.begin());
+  std::insert_iterator<container> iter = std::inserter(c, c.begin());
   *iter++ = 1;
   int i = 2;
   *iter = i;
diff --git a/libstdc++-v3/testsuite/24_iterators/ostream_iterator/requirements/constexpr.cc b/libstdc++-v3/testsuite/24_iterators/ostream_iterator/requirements/constexpr.cc
deleted file mode 100644
index 4edaaa8aebb..00000000000
--- a/libstdc++-v3/testsuite/24_iterators/ostream_iterator/requirements/constexpr.cc
+++ /dev/null
@@ -1,24 +0,0 @@
-// Copyright (C) 2019-2021 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 compile { target c++2a } }
-
-#include <iterator>
-
-constexpr std::ostream_iterator<int> iter1;
-constexpr std::ostream_iterator<int> iter2{};
diff --git a/libstdc++-v3/testsuite/std/ranges/97600.cc b/libstdc++-v3/testsuite/std/ranges/97600.cc
index 7435de022cd..c642b9d22d0 100644
--- a/libstdc++-v3/testsuite/std/ranges/97600.cc
+++ b/libstdc++-v3/testsuite/std/ranges/97600.cc
@@ -24,9 +24,8 @@
 #include <ranges>
 
 void
-test01()
+test01(std::ranges::basic_istream_view<int, char, std::char_traits<char>> v)
 {
-  std::ranges::basic_istream_view<int, char, std::char_traits<char>> v;
   v.begin();
   static_assert(std::ranges::range<decltype(v)>);
 }
diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/detail/semiregular_box.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/detail/copyable_box.cc
similarity index 70%
rename from libstdc++-v3/testsuite/std/ranges/adaptors/detail/semiregular_box.cc
rename to libstdc++-v3/testsuite/std/ranges/adaptors/detail/copyable_box.cc
index ed694e04fd1..fa6d4d56816 100644
--- a/libstdc++-v3/testsuite/std/ranges/adaptors/detail/semiregular_box.cc
+++ b/libstdc++-v3/testsuite/std/ranges/adaptors/detail/copyable_box.cc
@@ -82,9 +82,10 @@ test01()
 }
 static_assert(test01());
 
-template<bool make_semiregular>
+template<bool make_copyable>
   struct A {
-    A() requires make_semiregular;
+    A(const A&) = default;
+    A& operator=(const A&) requires make_copyable;
     A(int, int);
     A(std::initializer_list<int>) = delete;
   };
@@ -93,9 +94,51 @@ void
 test02()
 {
   // PR libstdc++/100475
-  static_assert(std::semiregular<A<true>>);
+  static_assert(std::copyable<A<true>>);
   __box<A<true>> x2(std::in_place, 0, 0);
 
-  static_assert(!std::semiregular<A<false>>);
+  static_assert(!std::copyable<A<false>>);
   __box<A<false>> x1(std::in_place, 0, 0);
 }
+
+constexpr bool
+test03()
+{
+  // Verify correctness of the non-defaulted operator= for the partial
+  // specialization of __box.
+  struct B {
+    constexpr B(int* p) : p(p) { }
+    constexpr ~B() { ++*p; };
+    B(const B&) = default;
+    B& operator=(const B&) = delete;
+    int* p;
+  };
+  static_assert(!std::copyable<B>);
+  static_assert(std::is_nothrow_copy_constructible_v<B>);
+  static_assert(sizeof(__box<B>) == sizeof(B));
+
+  int m = 0;
+  __box<B> x(std::in_place, &m);
+  __glibcxx_assert(m == 0);
+  x = x;
+  __glibcxx_assert(m == 0);
+  x = std::move(x);
+  __glibcxx_assert(m == 0);
+
+  int n = 0;
+  __box<B> y(std::in_place, &n);
+  auto z = x;
+  x = y;
+  __glibcxx_assert(m == 1);
+  __glibcxx_assert(n == 0);
+  __glibcxx_assert(x->p == &n);
+  __glibcxx_assert(y->p == &n);
+  y = std::move(z);
+  __glibcxx_assert(m == 1);
+  __glibcxx_assert(n == 1);
+  __glibcxx_assert(y->p == &m);
+  __glibcxx_assert(z->p == &m);
+
+  return true;
+}
+static_assert(test03());
diff --git a/libstdc++-v3/testsuite/std/ranges/p2325.cc b/libstdc++-v3/testsuite/std/ranges/p2325.cc
new file mode 100644
index 00000000000..df6cde29e4d
--- /dev/null
+++ b/libstdc++-v3/testsuite/std/ranges/p2325.cc
@@ -0,0 +1,155 @@
+// { dg-options "-std=gnu++20" }
+// { dg-do compile { target c++20 } }
+// P2325R3 "Views should not be required to be default constructible"
+
+#include <ranges>
+#include <iterator>
+#include <span>
+#include <sstream>
+#include <vector>
+#include <testsuite_iterators.h>
+
+using namespace std;
+
+template<default_initializable T> void f();
+template<typename T> requires weakly_incrementable<T> || ranges::view<T> void f();
+
+void
+test01()
+{
+  // Verify neither std::weakly_incrementable nor ranges::view require
+  // default_initializable.
+  f<int>(); // { dg-error "ambiguous" }
+}
+
+void
+test02()
+{
+  // Verify these iterators are not default constructible.
+  static_assert(!default_initializable<insert_iterator<vector<int>>>);
+  static_assert(!default_initializable<front_insert_iterator<vector<int>>>);
+  static_assert(!default_initializable<back_insert_iterator<vector<int>>>);
+  static_assert(!default_initializable<ostream_iterator<int>>);
+
+  using iter = ostream_iterator<int>;
+
+  // Verify common_iterator is conditionally default constructible.
+  static_assert(!default_initializable<common_iterator<iter, unreachable_sentinel_t>>);
+  static_assert(default_initializable<common_iterator<int*, unreachable_sentinel_t>>);
+
+  // Verify counted_iterator is conditionally default constructible.
+  static_assert(!default_initializable<counted_iterator<iter>>);
+  static_assert(default_initializable<counted_iterator<int*>>);
+}
+
+void
+test03()
+{
+  using iter = ostream_iterator<int>;
+
+  // Verify iota_view is conditionally default constructible.
+  static_assert(!default_initializable<ranges::iota_view<iter>>);
+  static_assert(!default_initializable<decltype(declval<ranges::iota_view<iter>>().begin())>);
+  static_assert(default_initializable<ranges::iota_view<int>>);
+  static_assert(default_initializable<decltype(declval<ranges::iota_view<int>>().begin())>);
+
+  // Verify subrange is conditionally default constructible.
+  static_assert(!default_initializable<ranges::subrange<iter, unreachable_sentinel_t>>);
+  static_assert(default_initializable<ranges::subrange<int*, unreachable_sentinel_t>>);
+
+  // Verify single_view is conditionally default constructible.
+  static_assert(!default_initializable<ranges::single_view<iter>>);
+  static_assert(default_initializable<ranges::single_view<int*>>);
+}
+
+void
+test04()
+{
+  // Verify basic_istream_view is not default constructible.
+  using type = ranges::basic_istream_view<int, char, char_traits<char>>;
+  static_assert(!default_initializable<type>);
+  static_assert(!default_initializable<decltype(declval<type>().begin())>);
+}
+
+void
+test05()
+{
+  // Verify ref_view is not default constructible.
+  static_assert(!default_initializable<ranges::ref_view<int[5]>>);
+}
+
+template<auto adaptor>
+void
+test06()
+{
+  auto f1 = [] (auto) { return true; };
+  auto f2 = [i=0] (auto) { return true; };
+  static_assert(default_initializable<decltype(views::single(0) | adaptor(f1))>);
+  static_assert(!default_initializable<decltype(views::single(0) | adaptor(f2))>);
+
+  struct S { S() = delete; };
+  static_assert(!default_initializable<decltype(views::single(declval<S>()) | adaptor(f1))>);
+  static_assert(!default_initializable<decltype(views::single(declval<S>()) | adaptor(f2))>);
+}
+
+// Verify filter_view, transform_view, take_while_view and drop_while_view are
+// conditionally default constructible.
+template void test06<views::filter>();
+template void test06<views::transform>();
+template void test06<views::take_while>();
+template void test06<views::drop_while>();
+
+void
+test07()
+{
+  // Verify join_view is conditionally default constructible.
+  struct S { S() = delete; };
+  using type1 = ranges::join_view<ranges::single_view<ranges::single_view<S>>>;
+  static_assert(!default_initializable<type1>);
+  using type2 = ranges::join_view<ranges::single_view<ranges::single_view<int>>>;
+  static_assert(default_initializable<type2>);
+}
+
+void
+test08()
+{
+  // Verify split_view is conditionally default constructible.
+  using type1 = ranges::split_view<ranges::ref_view<int[2]>, ranges::single_view<int>>;
+  static_assert(!default_initializable<type1>);
+  using type2 = ranges::split_view<ranges::single_view<int>, ranges::ref_view<int[2]>>;
+  static_assert(!default_initializable<type2>);
+  using type3 = ranges::split_view<ranges::ref_view<int[2]>, ranges::ref_view<int[2]>>;
+  static_assert(!default_initializable<type3>);
+  using type4 = ranges::split_view<ranges::single_view<int>, ranges::single_view<int>>;
+  static_assert(default_initializable<type4>);
+}
+
+void
+test09()
+{
+  // Verify common_view is conditionally default constructible.
+  using type1 = ranges::common_view<ranges::iota_view<ostream_iterator<int>>>;
+  static_assert(!default_initializable<type1>);
+  using type2 = ranges::common_view<ranges::iota_view<int*>>;
+  static_assert(default_initializable<type2>);
+}
+
+void
+test10()
+{
+  // Verify reverse_view is conditionally default constructible.
+  using type1 = ranges::reverse_view<ranges::ref_view<int[2]>>;
+  static_assert(!default_initializable<type1>);
+  using type2 = ranges::reverse_view<ranges::single_view<int>>;
+  static_assert(default_initializable<type2>);
+}
+
+void
+test11()
+{
+  // Verify elements_view is conditionally default constructible.
+  using type1 = ranges::elements_view<ranges::ref_view<pair<int,int>[2]>, 0>;
+  static_assert(!default_initializable<type1>);
+  using type2 = ranges::elements_view<ranges::single_view<pair<int,int>>, 0>;
+  static_assert(default_initializable<type2>);
+}
diff --git a/libstdc++-v3/testsuite/std/ranges/single_view.cc b/libstdc++-v3/testsuite/std/ranges/single_view.cc
index c036fc8976a..f1d8e103715 100644
--- a/libstdc++-v3/testsuite/std/ranges/single_view.cc
+++ b/libstdc++-v3/testsuite/std/ranges/single_view.cc
@@ -96,6 +96,20 @@ test05()
   static_assert(noexcept(cs.empty())); // view_interface::empty()
 }
 
+void
+test06()
+{
+  // PR libstdc++/100475 comment #7
+  struct S {
+    S() = default;
+    S(std::initializer_list<S>) = delete;
+    S(const S&) {}
+  };
+  S obj;
+  auto x = std::views::single(obj);
+  auto y = std::views::single(std::move(obj));
+}
+
 int main()
 {
   test01();
@@ -103,4 +117,5 @@ int main()
   test03();
   test04();
   test05();
+  test06();
 }
diff --git a/libstdc++-v3/testsuite/std/ranges/view.cc b/libstdc++-v3/testsuite/std/ranges/view.cc
index d8972ab3e46..dd8258220ed 100644
--- a/libstdc++-v3/testsuite/std/ranges/view.cc
+++ b/libstdc++-v3/testsuite/std/ranges/view.cc
@@ -31,7 +31,7 @@
 
 static_assert(std::ranges::view<std::span<int>>);
 static_assert(std::ranges::view<std::span<int, 0>>);
-static_assert(!std::ranges::view<std::span<int, 1>>);
+static_assert(std::ranges::view<std::span<int, 1>>); // Changed with P2325R3
 static_assert(std::ranges::view<std::string_view>);
 static_assert(std::ranges::view<std::experimental::string_view>);
 
-- 
2.32.0.93.g670b81a890



More information about the Gcc-patches mailing list