libstdc++: Remove precondition checks from ranges::subrange
The assertion in the subrange constructor causes semantic changes, because the call to ranges::distance performs additional operations that are not part of the constructor's specification. That will fail to compile if the iterator is move-only, because the argument to ranges::distance is passed by value. It will modify the subrange if the iterator is not a forward iterator, because incrementing the copy also affects the _M_begin member. Those problems could be prevented by using if-constexpr to only do the assertion for copyable forward iterators, but the call to ranges::distance can also prevent the constructor being usable in constant expressions. If the member initializers are usable in constant expressions, but iterator increments of equality comparisons are not, then the checks done by __glibcxx_assert might make constant evaluation fail. This change removes the assertion. Additionally, a new typedef is introduced to simplify the declarations using __make_unsigned_like_t on the iterator's difference type. Signed-off-by: Jonathan Wakely <jwakely@redhat.com> libstdc++-v3/ChangeLog: * include/bits/ranges_util.h (subrange): Add __size_type typedef and use it to simplify declarations. (subrange(i, s, n)): Remove assertion. * testsuite/std/ranges/subrange/constexpr.cc: New test.
This commit is contained in:
parent
cb326a6442
commit
a88fc03ba7
2 changed files with 37 additions and 11 deletions
|
@ -205,15 +205,18 @@ namespace ranges
|
|||
_It _M_begin = _It();
|
||||
[[no_unique_address]] _Sent _M_end = _Sent();
|
||||
|
||||
using __size_type
|
||||
= __detail::__make_unsigned_like_t<iter_difference_t<_It>>;
|
||||
|
||||
template<typename, bool = _S_store_size>
|
||||
struct _Size
|
||||
{ };
|
||||
|
||||
template<typename _Tp>
|
||||
struct _Size<_Tp, true>
|
||||
{ __detail::__make_unsigned_like_t<_Tp> _M_size; };
|
||||
{ _Tp _M_size; };
|
||||
|
||||
[[no_unique_address]] _Size<iter_difference_t<_It>> _M_size = {};
|
||||
[[no_unique_address]] _Size<__size_type> _M_size = {};
|
||||
|
||||
public:
|
||||
subrange() = default;
|
||||
|
@ -226,12 +229,10 @@ namespace ranges
|
|||
|
||||
constexpr
|
||||
subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s,
|
||||
__detail::__make_unsigned_like_t<iter_difference_t<_It>> __n)
|
||||
__size_type __n)
|
||||
requires (_Kind == subrange_kind::sized)
|
||||
: _M_begin(std::move(__i)), _M_end(__s)
|
||||
{
|
||||
using __detail::__to_unsigned_like;
|
||||
__glibcxx_assert(__n == __to_unsigned_like(ranges::distance(__i, __s)));
|
||||
if constexpr (_S_store_size)
|
||||
_M_size._M_size = __n;
|
||||
}
|
||||
|
@ -258,8 +259,7 @@ namespace ranges
|
|||
requires __detail::__convertible_to_non_slicing<iterator_t<_Rng>, _It>
|
||||
&& convertible_to<sentinel_t<_Rng>, _Sent>
|
||||
constexpr
|
||||
subrange(_Rng&& __r,
|
||||
__detail::__make_unsigned_like_t<iter_difference_t<_It>> __n)
|
||||
subrange(_Rng&& __r, __size_type __n)
|
||||
requires (_Kind == subrange_kind::sized)
|
||||
: subrange{ranges::begin(__r), ranges::end(__r), __n}
|
||||
{ }
|
||||
|
@ -267,9 +267,9 @@ namespace ranges
|
|||
template<__detail::__not_same_as<subrange> _PairLike>
|
||||
requires __detail::__pair_like_convertible_from<_PairLike, const _It&,
|
||||
const _Sent&>
|
||||
constexpr
|
||||
operator _PairLike() const
|
||||
{ return _PairLike(_M_begin, _M_end); }
|
||||
constexpr
|
||||
operator _PairLike() const
|
||||
{ return _PairLike(_M_begin, _M_end); }
|
||||
|
||||
constexpr _It
|
||||
begin() const requires copyable<_It>
|
||||
|
@ -283,7 +283,7 @@ namespace ranges
|
|||
|
||||
constexpr bool empty() const { return _M_begin == _M_end; }
|
||||
|
||||
constexpr __detail::__make_unsigned_like_t<iter_difference_t<_It>>
|
||||
constexpr __size_type
|
||||
size() const requires (_Kind == subrange_kind::sized)
|
||||
{
|
||||
if constexpr (_S_store_size)
|
||||
|
|
26
libstdc++-v3/testsuite/std/ranges/subrange/constexpr.cc
Normal file
26
libstdc++-v3/testsuite/std/ranges/subrange/constexpr.cc
Normal file
|
@ -0,0 +1,26 @@
|
|||
// { dg-options "-std=gnu++20" }
|
||||
// { dg-do compile { target c++20 } }
|
||||
|
||||
#include <ranges>
|
||||
|
||||
struct iterator
|
||||
{
|
||||
using difference_type = int;
|
||||
|
||||
int i;
|
||||
|
||||
int operator*() const { return i; }
|
||||
|
||||
// These are intentionally not constexpr:
|
||||
iterator& operator++() { ++i; return *this; }
|
||||
iterator operator++(int) { return {i++}; }
|
||||
bool operator==(const iterator& it) const { return i == it.i; }
|
||||
};
|
||||
|
||||
constexpr iterator begin(1), end(2);
|
||||
|
||||
using std::ranges::subrange;
|
||||
using std::ranges::subrange_kind;
|
||||
|
||||
// This used to fail due to using operator++ and operator== in an assertion:
|
||||
constexpr subrange<iterator, iterator, subrange_kind::sized> s(begin, end, 1);
|
Loading…
Add table
Reference in a new issue