Commit 5586e506 by Patrick Palka

libstdc++: Forward second argument of views::iota using the correct type

We are forwarding the second argument of views::iota using the wrong type,
causing compile errors when calling views::iota with a value and bound of
different types, like in the test case below.

libstdc++-v3/ChangeLog:

	* include/std/ranges (iota_view): Forward declare _Sentinel.
	(iota_view::_Iterator): Befriend _Sentinel.
	(iota_view::_Sentinel::_M_equal): New member function.
	(iota_view::_Sentinel::operator==): Use it.
	(views::_Iota::operator()): Forward __f using the correct type.
	* testsuite/std/ranges/access/ssize.cc (test06): Don't call views::iota
	with integers of different signedness, to appease iota_view's deduction
	guide.
	* testsuite/std/ranges/iota/iota_view.cc: Augment test.
parent e6f24f82
2020-02-20 Patrick Palka <ppalka@redhat.com>
* include/std/ranges (iota_view): Forward declare _Sentinel.
(iota_view::_Iterator): Befriend _Sentinel.
(iota_view::_Sentinel::_M_equal): New member function.
(iota_view::_Sentinel::operator==): Use it.
(views::_Iota::operator()): Forward __f using the correct type.
* testsuite/std/ranges/access/ssize.cc (test06): Don't call views::iota
with integers of different signedness, to appease iota_view's deduction
guide.
* testsuite/std/ranges/iota/iota_view.cc: Augment test.
2020-02-20 Jonathan Wakely <jwakely@redhat.com> 2020-02-20 Jonathan Wakely <jwakely@redhat.com>
* include/bits/range_access.h (ranges::begin): Reject array of * include/bits/range_access.h (ranges::begin): Reject array of
......
...@@ -635,6 +635,8 @@ namespace ranges ...@@ -635,6 +635,8 @@ namespace ranges
class iota_view : public view_interface<iota_view<_Winc, _Bound>> class iota_view : public view_interface<iota_view<_Winc, _Bound>>
{ {
private: private:
struct _Sentinel;
struct _Iterator struct _Iterator
{ {
private: private:
...@@ -811,11 +813,17 @@ namespace ranges ...@@ -811,11 +813,17 @@ namespace ranges
private: private:
_Winc _M_value = _Winc(); _Winc _M_value = _Winc();
friend _Sentinel;
}; };
struct _Sentinel struct _Sentinel
{ {
private: private:
constexpr bool
_M_equal(const _Iterator& __x) const
{ return __x._M_value == _M_bound; }
_Bound _M_bound = _Bound(); _Bound _M_bound = _Bound();
public: public:
...@@ -827,7 +835,7 @@ namespace ranges ...@@ -827,7 +835,7 @@ namespace ranges
friend constexpr bool friend constexpr bool
operator==(const _Iterator& __x, const _Sentinel& __y) operator==(const _Iterator& __x, const _Sentinel& __y)
{ return __x._M_value == __y._M_bound; } { return __y._M_equal(__x); }
friend constexpr iter_difference_t<_Winc> friend constexpr iter_difference_t<_Winc>
operator-(const _Iterator& __x, const _Sentinel& __y) operator-(const _Iterator& __x, const _Sentinel& __y)
...@@ -933,7 +941,7 @@ namespace views ...@@ -933,7 +941,7 @@ namespace views
template<typename _Tp, typename _Up> template<typename _Tp, typename _Up>
constexpr auto constexpr auto
operator()(_Tp&& __e, _Up&& __f) const operator()(_Tp&& __e, _Up&& __f) const
{ return iota_view{std::forward<_Tp>(__e), std::forward<_Tp>(__f)}; } { return iota_view{std::forward<_Tp>(__e), std::forward<_Up>(__f)}; }
}; };
inline constexpr _Iota iota{}; inline constexpr _Iota iota{};
......
...@@ -75,7 +75,7 @@ test05() ...@@ -75,7 +75,7 @@ test05()
void void
test06() test06()
{ {
auto i = std::views::iota(1ull, 5); auto i = std::views::iota(1ull, 5u);
auto s = std::ranges::ssize(i); auto s = std::ranges::ssize(i);
using R = std::ranges::range_difference_t<decltype(i)>; using R = std::ranges::range_difference_t<decltype(i)>;
static_assert( std::same_as<decltype(s), R> ); static_assert( std::same_as<decltype(s), R> );
......
...@@ -61,10 +61,27 @@ test03() ...@@ -61,10 +61,27 @@ test03()
VERIFY( it == v.end() ); VERIFY( it == v.end() );
} }
void
test04()
{
int x[] = {1,2,3};
auto v = std::ranges::views::iota(std::counted_iterator(x, 3),
std::default_sentinel);
auto it = v.begin();
VERIFY( (*it).base() == x );
++it;
VERIFY( (*it).base() == x+1 );
++it;
VERIFY( (*it).base() == x+2 );
++it;
VERIFY( it == v.end() );
}
int int
main() main()
{ {
test01(); test01();
test02(); test02();
test03(); test03();
test04();
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment