1 // Copyright (C) 2019-2023 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>
23 #include <testsuite_iterators.h>
25 static_assert(__gnu_test::is_customization_point_object(std::ranges::rend
));
32 constexpr const int* rbegin() const { return &i
; }
33 constexpr const int* rend() const { return &i
+ 1; }
34 friend constexpr const int* rbegin(const R1
&&); // not defined
35 friend constexpr const int* rend(const R1
&&); // not defined
38 // N.B. this is a lie, rend on an R1 rvalue will return a dangling pointer.
39 template<> constexpr bool std::ranges::enable_borrowed_range
<R1
> = true;
46 // decay-copy(t.rend()) if it is a valid expression
47 // and its type S models sentinel_for<decltype(ranges::rbegin(E))>
49 static_assert( std::ranges::rend(r
) == &r
.i
+ 1 );
50 static_assert( std::ranges::rend(std::move(r
)) == &r
.i
+ 1 );
57 int* rbegin() noexcept
{ return &i
+ 1; }
58 long* rend() noexcept
{ return nullptr; } // not a sentinel for rbegin()
60 friend long* rbegin(R2
&) noexcept
{ return nullptr; }
61 friend int* rend(R2
& r
) { return &r
.i
; }
69 // Otherwise, decay-copy(rend(t)) if it is a valid expression
70 // and its type S models sentinel_for<decltype(ranges::rbegin(E))>
72 auto i1
= std::ranges::rbegin(r
);
74 static_assert( std::sentinel_for
<decltype(i2
), decltype(i1
)> );
75 VERIFY( std::ranges::rend(r
) == &r
.i
);
76 static_assert( !noexcept(std::ranges::rend(r
)) );
84 constexpr const int* begin() const { return a
; }
85 constexpr const int* end() const { return a
+ 2; }
87 friend constexpr const long* begin(const R3
&& r
) { return r
.l
; }
88 friend constexpr const long* end(const R3
&& r
) { return r
.l
+ 2; }
91 // N.B. this is a lie, begin/end on an R3 rvalue will return a dangling pointer.
92 template<> constexpr bool std::ranges::enable_borrowed_range
<R3
> = true;
99 // Otherwise, make_reverse_iterator(ranges::begin(t)) if both
100 // ranges::begin(t) and ranges::end(t) are valid expressions
101 // of the same type I which models bidirectional_iterator.
103 static_assert( std::ranges::rend(r
)
104 == std::make_reverse_iterator(std::ranges::begin(r
)) );
105 static_assert( std::ranges::rend(std::move(r
))
106 == std::make_reverse_iterator(std::ranges::begin(std::move(r
))) );
113 : __gnu_test::test_range
<int, __gnu_test::bidirectional_iterator_wrapper
>
115 R4(int (&a
)[2]) : test_range(a
) { }
117 using test_range::begin
;
119 // Replace test_range::end() to return same type as begin()
120 // so ranges::rend will wrap it in a reverse_iterator.
123 using __gnu_test::bidirectional_iterator_wrapper
;
124 return bidirectional_iterator_wrapper
<int>(bounds
.last
, &bounds
);
130 VERIFY( std::ranges::rend(r
) == std::make_reverse_iterator(std::ranges::begin(r
)) );