From: Jonathan Wakely Date: Mon, 21 Sep 2020 13:30:38 +0000 (+0100) Subject: libstdc++: Relax constraints on transform_view and elements_view iterators X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=2ec58cfcea146a61755516ce4ed160827fe0b4ff;p=gcc.git libstdc++: Relax constraints on transform_view and elements_view iterators libstdc++-v3/ChangeLog: * include/std/ranges (transform_view, elements_view): Relax constraints on operator- for iterators, as per LWG 3483. * testsuite/std/ranges/adaptors/elements.cc: Check that we can take the difference of two iterators from a non-random access range. * testsuite/std/ranges/adaptors/transform.cc: Likewise. --- diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 23a04d61174..005e89f94b2 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -1833,9 +1833,11 @@ namespace views requires random_access_range<_Base> { return {*__i._M_parent, __i._M_current - __n}; } + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3483. transform_view::iterator's difference is overconstrained friend constexpr difference_type operator-(const _Iterator& __x, const _Iterator& __y) - requires random_access_range<_Base> + requires sized_sentinel_for, iterator_t<_Base>> { return __x._M_current - __y._M_current; } friend constexpr decltype(auto) @@ -3538,9 +3540,11 @@ namespace views requires random_access_range<_Base> { return _Iterator{__x} -= __y; } + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3483. transform_view::iterator's difference is overconstrained friend constexpr difference_type operator-(const _Iterator& __x, const _Iterator& __y) - requires random_access_range<_Base> + requires sized_sentinel_for, iterator_t<_Base>> { return __x._M_current - __y._M_current; } friend _Sentinel<_Const>; diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/elements.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/elements.cc index 3026adf4f28..94dd7c94505 100644 --- a/libstdc++-v3/testsuite/std/ranges/adaptors/elements.cc +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/elements.cc @@ -66,9 +66,33 @@ test02() VERIFY( ranges::equal(v2, (std::pair[]){{1,2}}) ); } +struct X +{ + using Iter = __gnu_test::forward_iterator_wrapper>; + + friend auto operator-(Iter l, Iter r) { return l.ptr - r.ptr; } +}; + +void +test03() +{ + // LWG 3483 + std::pair x[3]; + __gnu_test::test_forward_range> r(x); + auto v = views::elements<1>(r); + auto b = begin(v); + static_assert( !ranges::random_access_range ); + static_assert( std::sized_sentinel_for ); + VERIFY( (next(b, 1) - b) == 1 ); + const auto v_const = v; + auto b_const = begin(v_const); + VERIFY( (next(b_const, 2) - b_const) == 2 ); +} + int main() { test01(); test02(); + test03(); } diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/transform.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/transform.cc index c14e36e0cef..41a7d3b3321 100644 --- a/libstdc++-v3/testsuite/std/ranges/adaptors/transform.cc +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/transform.cc @@ -122,6 +122,29 @@ test05() b = ranges::end(v); } +struct Y +{ + using Iter = __gnu_test::forward_iterator_wrapper; + + friend auto operator-(Iter l, Iter r) { return l.ptr - r.ptr; } +}; + +void +test06() +{ + // LWG 3483 + Y y[3]; + __gnu_test::test_forward_range r(y); + auto v = views::transform(r, std::identity{}); + auto b = begin(v); + static_assert( !ranges::random_access_range ); + static_assert( std::sized_sentinel_for ); + VERIFY( (next(b, 1) - b) == 1 ); + const auto v_const = v; + auto b_const = begin(v_const); + VERIFY( (next(b_const, 2) - b_const) == 2 ); +} + int main() { @@ -130,4 +153,5 @@ main() test03(); test04(); test05(); + test06(); }