Skip to content

Commit 4b4f566

Browse files
author
Patrick Palka
committed
libstdc++: Implement P2325 changes to default-constructibility of views
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. In passing, this fixes the undesirable list-init in the constructors of the partial specialization of __box as reported in PR100475 comment gcc-mirror#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.
1 parent 7d08043 commit 4b4f566

File tree

17 files changed

+335
-118
lines changed

17 files changed

+335
-118
lines changed

libstdc++-v3/include/bits/iterator_concepts.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
594594

595595
/// Requirements on types that can be incremented with ++.
596596
template<typename _Iter>
597-
concept weakly_incrementable = default_initializable<_Iter>
598-
&& movable<_Iter>
597+
concept weakly_incrementable = movable<_Iter>
599598
&& requires(_Iter __i)
600599
{
601600
typename iter_difference_t<_Iter>;

libstdc++-v3/include/bits/ranges_base.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,7 @@ namespace ranges
619619
/// [range.view] The ranges::view concept.
620620
template<typename _Tp>
621621
concept view
622-
= range<_Tp> && movable<_Tp> && default_initializable<_Tp>
623-
&& enable_view<_Tp>;
622+
= range<_Tp> && movable<_Tp> && enable_view<_Tp>;
624623

625624
// [range.refinements]
626625

libstdc++-v3/include/bits/ranges_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ namespace ranges
241241
[[no_unique_address]] _Size<__size_type> _M_size = {};
242242

243243
public:
244-
subrange() = default;
244+
subrange() requires default_initializable<_It> = default;
245245

246246
constexpr
247247
subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s)

libstdc++-v3/include/bits/stl_iterator.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
639639
typedef _Container container_type;
640640
#if __cplusplus > 201703L
641641
using difference_type = ptrdiff_t;
642-
643-
constexpr back_insert_iterator() noexcept : container(nullptr) { }
644642
#endif
645643

646644
/// The only way to create this %iterator is with a container.
@@ -742,8 +740,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
742740
typedef _Container container_type;
743741
#if __cplusplus > 201703L
744742
using difference_type = ptrdiff_t;
745-
746-
constexpr front_insert_iterator() noexcept : container(nullptr) { }
747743
#endif
748744

749745
/// The only way to create this %iterator is with a container.
@@ -843,26 +839,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
843839
{
844840
#if __cplusplus > 201703L && defined __cpp_lib_concepts
845841
using _Iter = std::__detail::__range_iter_t<_Container>;
846-
847-
protected:
848-
_Container* container = nullptr;
849-
_Iter iter = _Iter();
850842
#else
851843
typedef typename _Container::iterator _Iter;
852-
844+
#endif
853845
protected:
854846
_Container* container;
855847
_Iter iter;
856-
#endif
857848

858849
public:
859850
/// A nested typedef for the type of whatever container you used.
860851
typedef _Container container_type;
861852

862853
#if __cplusplus > 201703L && defined __cpp_lib_concepts
863854
using difference_type = ptrdiff_t;
864-
865-
insert_iterator() = default;
866855
#endif
867856

868857
/**
@@ -1740,6 +1729,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
17401729
constexpr
17411730
common_iterator()
17421731
noexcept(is_nothrow_default_constructible_v<_It>)
1732+
requires default_initializable<_It>
17431733
: _M_it(), _M_index(0)
17441734
{ }
17451735

@@ -2117,7 +2107,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
21172107
// iterator_concept defined in __counted_iter_concept
21182108
// iterator_category defined in __counted_iter_cat
21192109

2120-
constexpr counted_iterator() = default;
2110+
constexpr counted_iterator() requires default_initializable<_It> = default;
21212111

21222112
constexpr
21232113
counted_iterator(_It __i, iter_difference_t<_It> __n)

libstdc++-v3/include/bits/stream_iterator.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
192192
const _CharT* _M_string;
193193

194194
public:
195-
#if __cplusplus > 201703L
196-
constexpr ostream_iterator() noexcept
197-
: _M_stream(nullptr), _M_string(nullptr) { }
198-
#endif
199-
200195
/// Construct from an ostream.
201196
ostream_iterator(ostream_type& __s)
202197
: _M_stream(std::__addressof(__s)), _M_string(0) {}

0 commit comments

Comments
 (0)