Commit 7def9bd7 by Jonathan Wakely Committed by Jonathan Wakely

libstdc++: Add move_sentinel, common_iterator and counted_iterator

This implements most of the remaining C++20 additions to the <iterator>
header.

	* include/bits/iterator_concepts.h (ranges::iter_swap): Fix parameter
	types of poison pill overload. Use remove_reference_t when checking
	constraints.
	* include/bits/stl_iterator.h (move_sentinel): Define for C++20.
	(move_iterator): Adjust definitions of nested types for C++20. Add
	hidden friends for move_sentinel operations, iter_move and iter_swap.
	(common_iterator, counted_iterator): Define for C++20.
	* testsuite/24_iterators/move_iterator/cust.cc: New test.
	* testsuite/24_iterators/move_iterator/sentinel.cc: New test.
	* testsuite/24_iterators/common_iterator/1.cc: New test.
	* testsuite/24_iterators/counted_iterator/1.cc: New test.

From-SVN: r278698
parent d6039f5c
2019-11-25 Jonathan Wakely <jwakely@redhat.com> 2019-11-25 Jonathan Wakely <jwakely@redhat.com>
* include/bits/iterator_concepts.h (ranges::iter_swap): Fix parameter
types of poison pill overload. Use remove_reference_t when checking
constraints.
* include/bits/stl_iterator.h (move_sentinel): Define for C++20.
(move_iterator): Adjust definitions of nested types for C++20. Add
hidden friends for move_sentinel operations, iter_move and iter_swap.
(common_iterator, counted_iterator): Define for C++20.
* testsuite/24_iterators/move_iterator/cust.cc: New test.
* testsuite/24_iterators/move_iterator/sentinel.cc: New test.
* testsuite/24_iterators/common_iterator/1.cc: New test.
* testsuite/24_iterators/counted_iterator/1.cc: New test.
PR libstdc++/91786 PR libstdc++/91786
* include/bits/fs_path.h (filesystem_error): Move definition before * include/bits/fs_path.h (filesystem_error): Move definition before
the use in u8path. the use in u8path.
......
...@@ -700,7 +700,7 @@ namespace ranges ...@@ -700,7 +700,7 @@ namespace ranges
namespace __cust_iswap namespace __cust_iswap
{ {
template<typename _It1, typename _It2> template<typename _It1, typename _It2>
void iter_swap(_It1&, _It2&) = delete; void iter_swap(_It1, _It2) = delete;
template<typename _Tp, typename _Up> template<typename _Tp, typename _Up>
concept __adl_iswap concept __adl_iswap
...@@ -744,7 +744,8 @@ namespace ranges ...@@ -744,7 +744,8 @@ namespace ranges
public: public:
template<typename _Tp, typename _Up> template<typename _Tp, typename _Up>
requires __adl_iswap<_Tp, _Up> requires __adl_iswap<_Tp, _Up>
|| (readable<_Tp> && readable<_Up> || (readable<remove_reference_t<_Tp>>
&& readable<remove_reference_t<_Up>>
&& swappable_with<iter_reference_t<_Tp>, iter_reference_t<_Up>>) && swappable_with<iter_reference_t<_Tp>, iter_reference_t<_Up>>)
|| (indirectly_movable_storable<_Tp, _Up> || (indirectly_movable_storable<_Tp, _Up>
&& indirectly_movable_storable<_Up, _Tp>) && indirectly_movable_storable<_Up, _Tp>)
......
// Copyright (C) 2019 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 run { target c++2a } }
#include <iterator>
#include <testsuite_hooks.h>
void
test01()
{
using I = std::common_iterator<int*, const int*>;
static_assert( std::is_default_constructible_v<I> );
static_assert( std::is_copy_constructible_v<I> );
static_assert( std::is_copy_assignable_v<I> );
static_assert( std::is_constructible_v<I, int*> );
static_assert( std::is_constructible_v<I, const int*> );
struct sentinel { operator int*() const { return nullptr; } };
using K = std::common_iterator<int*, sentinel>;
static_assert( std::is_constructible_v<I, const K&> );
static_assert( std::is_assignable_v<I, const K&> );
struct sentinel2
{
const int* p;
sentinel2(const int* p = 0) : p(p) { }
bool operator==(const int* p) const { return p == this->p; }
};
using J = std::common_iterator<const int*, sentinel2>;
static_assert( std::is_constructible_v<J, const I&> );
static_assert( std::is_convertible_v<const I&, J> );
}
void
test02()
{
struct sentinel { int limit; };
struct iterator
{
using iterator_category = std::input_iterator_tag;
using value_type = int;
using difference_type = std::ptrdiff_t;
using reference = const int&;
const int& operator*() const { return counter; }
iterator& operator++() { ++counter; return *this; }
iterator operator++(int) { auto i = *this; ++counter; return i; }
bool operator==(sentinel s) const { return counter == s.limit; }
int counter = 0;
};
static_assert( std::sentinel_for<sentinel, iterator> );
int out[5] = { };
std::common_iterator<int*, const int*> obegin = std::begin(out);
std::common_iterator<int*, const int*> oend = std::cend(out);
iterator i;
sentinel s{5};
std::common_iterator<iterator, sentinel> begin = i, end = s;
while (begin != end)
*obegin++ = *begin++;
VERIFY(obegin == oend);
for (int& i : out)
VERIFY( i == (&i - out) );
}
void
test03()
{
int arr[2] = { 1, 2 };
std::common_iterator<int*, const int*> i = std::ranges::begin(arr);
std::common_iterator<int*, const int*> end = std::ranges::cend(arr);
VERIFY( i != end );
VERIFY( (end - i) == 2 );
VERIFY( (i - end) == -2 );
auto j = i;
VERIFY( j == i );
VERIFY( (j - i) == 0 );
j = end;
VERIFY( j != i );
VERIFY( j == end );
j = std::ranges::next(i);
VERIFY( j != i );
VERIFY( j != end );
VERIFY( (end - j) == 1 );
VERIFY( (j - i) == 1 );
VERIFY( (i - j) == -1 );
++j;
VERIFY( j == end );
VERIFY( (end - j) == 0 );
j = i;
VERIFY( j == i );
VERIFY( (j - end) == -2 );
VERIFY( (j - i) == 0 );
try
{
struct S { operator const int*() const { throw 1; } };
i = std::common_iterator<int*, S>(S{});
VERIFY( false );
}
catch (int)
{
}
}
void
test04()
{
struct X
{
X(int i) : i(i) { }
X(X&& x) : i(x.i) { x.i = -1; }
X& operator=(X&& x) { i = x.i; x.i = 0; return *this; }
int i;
};
X arr[] = { 1, 2 };
std::common_iterator<X*, const X*> i(arr), j(arr+1);
std::ranges::iter_swap(i, j);
VERIFY( arr[0].i == 2 );
VERIFY( arr[1].i == 1 );
X x = std::ranges::iter_move(i);
VERIFY( arr[0].i == -1 );
VERIFY( x.i == 2 );
}
int
main()
{
test01();
test02();
test03();
test04();
}
// Copyright (C) 2019 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 run { target c++2a } }
#include <iterator>
#include <testsuite_hooks.h>
void
test01()
{
using I = std::counted_iterator<int*>;
static_assert( std::is_default_constructible_v<I> );
static_assert( std::is_copy_constructible_v<I> );
static_assert( std::is_copy_assignable_v<I> );
static_assert( ! std::is_constructible_v<I, int*> );
static_assert( std::is_constructible_v<I, int*, std::ptrdiff_t> );
using J = std::counted_iterator<const int*>;
static_assert( std::is_constructible_v<J, const I&> );
static_assert( std::is_convertible_v<const I&, J> );
}
void
test02()
{
int in[3] = { 1, 2, 3 };
std::counted_iterator<const int*> in_iter(std::begin(in), std::ssize(in));
VERIFY( in_iter.base() == in );
VERIFY( (std::default_sentinel - in_iter) == 3 );
VERIFY( (in_iter - std::default_sentinel) == -3 );
int out[4] = { };
std::counted_iterator<int*> out_iter(std::begin(out), std::ssize(out));
VERIFY( out_iter.base() == out );
VERIFY( (std::default_sentinel - out_iter) == 4 );
VERIFY( (out_iter - std::default_sentinel) == -4 );
while (in_iter != std::default_sentinel && out_iter != std::default_sentinel)
*out_iter++ = *in_iter++;
VERIFY(in_iter == std::default_sentinel);
VERIFY(out_iter != std::default_sentinel);
VERIFY( out[0] == 1 );
VERIFY( out[1] == 2 );
VERIFY( out[2] == 3 );
VERIFY( out[3] == 0 );
auto out2 = out_iter;
out2 += 1;
VERIFY( out2 == std::default_sentinel );
VERIFY( (out2 <=> out_iter) == std::strong_ordering::greater );
out2 -= 3;
VERIFY( (out_iter - out2) == 2 );
VERIFY( (out2 <=> out_iter) == std::strong_ordering::less );
}
void
test03()
{
struct X
{
X(int i) : i(i) { }
X(X&& x) : i(x.i) { x.i = -1; }
X& operator=(X&& x) { i = x.i; x.i = 0; return *this; }
int i;
};
X arr[] = { 1, 2 };
std::counted_iterator<X*> i(arr, 2), j(arr + 1, 1);
std::ranges::iter_swap(i, j);
VERIFY( arr[0].i == 2 );
VERIFY( arr[1].i == 1 );
X x = std::ranges::iter_move(i);
VERIFY( arr[0].i == -1 );
VERIFY( x.i == 2 );
}
int
main()
{
test01();
test02();
test03();
}
// Copyright (C) 2019 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 run { target c++2a } }
#include <iterator>
#include <testsuite_hooks.h>
void
test01()
{
struct X
{
X(int i) : i(i) { }
X(X&& x) : i(x.i) { x.i = -1; }
X& operator=(X&& x) { i = x.i; x.i = 0; return *this; }
int i;
};
X arr[] = { 1, 2 };
std::move_iterator<X*> i(arr), j(arr + 1);
std::ranges::iter_swap(i, j);
VERIFY( arr[0].i == 2 );
VERIFY( arr[1].i == 1 );
X x = std::ranges::iter_move(i);
VERIFY( arr[0].i == -1 );
VERIFY( x.i == 2 );
}
int
main()
{
test01();
}
// Copyright (C) 2019 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 run { target c++2a } }
#include <iterator>
#include <testsuite_hooks.h>
void
test01()
{
using S = std::move_sentinel<const int*>;
using M = std::move_iterator<int*>;
static_assert( std::is_default_constructible_v<S> );
static_assert( std::is_copy_constructible_v<S> );
static_assert( std::is_copy_assignable_v<S> );
static_assert( std::is_constructible_v<S, std::move_sentinel<int*>> );
static_assert( std::is_assignable_v<S, std::move_sentinel<int*>> );
constexpr S s;
static_assert( s.base() == nullptr );
constexpr M m;
static_assert( m == s );
static_assert( s == m );
static_assert( !(m != s) );
static_assert( !(s != m) );
int i = 0;
M m2(&i);
VERIFY( m2 != s );
VERIFY( s != m2 );
VERIFY( !(m2 == s) );
VERIFY( !(s == m2) );
}
void
test02()
{
struct sentinel { int limit; };
struct iterator
{
using iterator_category = std::input_iterator_tag;
using value_type = int;
using difference_type = std::ptrdiff_t;
using reference = const int&;
const int& operator*() const { return counter; }
iterator& operator++() { ++counter; return *this; }
iterator operator++(int) { auto i = *this; ++counter; return i; }
bool operator==(sentinel s) const { return counter == s.limit; }
int counter = 0;
};
static_assert( std::sentinel_for<sentinel, iterator> );
iterator i;
sentinel s{5};
int count = 0;
for (auto m = std::make_move_iterator(i); m != std::move_sentinel{s}; ++m)
++count;
VERIFY( count == 5 );
}
int
main()
{
test01();
test02();
}
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