1 // Copyright (C) 2019-2022 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
22 #include <testsuite_hooks.h>
27 using I
= std::common_iterator
<int*, const int*>;
28 static_assert( std::is_default_constructible_v
<I
> );
29 static_assert( std::is_copy_constructible_v
<I
> );
30 static_assert( std::is_copy_assignable_v
<I
> );
31 static_assert( std::is_constructible_v
<I
, int*> );
32 static_assert( std::is_constructible_v
<I
, const int*> );
34 struct sentinel
{ operator int*() const { return nullptr; } };
35 using K
= std::common_iterator
<int*, sentinel
>;
36 static_assert( std::is_constructible_v
<I
, const K
&> );
37 static_assert( std::is_assignable_v
<I
, const K
&> );
42 sentinel2(const int* p
= 0) : p(p
) { }
43 bool operator==(const int* p
) const { return p
== this->p
; }
46 using J
= std::common_iterator
<const int*, sentinel2
>;
47 static_assert( std::is_constructible_v
<J
, const I
&> );
48 static_assert( std::is_convertible_v
<const I
&, J
> );
54 struct sentinel
{ int limit
; };
58 using iterator_category
= std::input_iterator_tag
;
59 using value_type
= int;
60 using difference_type
= std::ptrdiff_t;
61 using reference
= const int&;
63 const int& operator*() const { return counter
; }
65 iterator
& operator++() { ++counter
; return *this; }
67 iterator
operator++(int) { auto i
= *this; ++counter
; return i
; }
69 bool operator==(sentinel s
) const { return counter
== s
.limit
; }
74 static_assert( std::sentinel_for
<sentinel
, iterator
> );
77 std::common_iterator
<int*, const int*> obegin
= std::begin(out
);
78 std::common_iterator
<int*, const int*> oend
= std::cend(out
);
82 std::common_iterator
<iterator
, sentinel
> begin
= i
, end
= s
;
86 VERIFY(obegin
== oend
);
88 VERIFY( i
== (&i
- out
) );
94 int arr
[2] = { 1, 2 };
95 std::common_iterator
<int*, const int*> i
= std::ranges::begin(arr
);
96 std::common_iterator
<int*, const int*> end
= std::ranges::cend(arr
);
98 VERIFY( (end
- i
) == 2 );
99 VERIFY( (i
- end
) == -2 );
102 VERIFY( (j
- i
) == 0 );
106 j
= std::ranges::next(i
);
109 VERIFY( (end
- j
) == 1 );
110 VERIFY( (j
- i
) == 1 );
111 VERIFY( (i
- j
) == -1 );
114 VERIFY( (end
- j
) == 0 );
117 VERIFY( (j
- end
) == -2 );
118 VERIFY( (j
- i
) == 0 );
122 struct S
{ operator const int*() const { throw 1; } };
123 i
= std::common_iterator
<int*, S
>(S
{});
137 X(X
&& x
) : i(x
.i
) { x
.i
= -1; }
138 X
& operator=(X
&& x
) { i
= x
.i
; x
.i
= 0; return *this; }
143 std::common_iterator
<X
*, const X
*> i(arr
), j(arr
+1);
144 std::ranges::iter_swap(i
, j
);
145 VERIFY( arr
[0].i
== 2 );
146 VERIFY( arr
[1].i
== 1 );
148 X x
= std::ranges::iter_move(i
);
149 VERIFY( arr
[0].i
== -1 );