| 1 | // Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 2 | // |
| 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) |
| 7 | // any later version. |
| 8 | |
| 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. |
| 13 | |
| 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/>. |
| 17 | |
| 18 | // { dg-do run { target c++20 } } |
| 19 | |
| 20 | #include <ranges> |
| 21 | #include <testsuite_hooks.h> |
| 22 | #include <testsuite_iterators.h> |
| 23 | |
| 24 | static_assert(__gnu_test::is_customization_point_object(std::ranges::crbegin)); |
| 25 | |
| 26 | struct R1 |
| 27 | { |
| 28 | int i = 0; |
| 29 | int j = 0; |
| 30 | |
| 31 | #if __cpp_lib_ranges_as_const |
| 32 | const int *begin() const; |
| 33 | const int *end() const; |
| 34 | #endif |
| 35 | |
| 36 | const int* rbegin() const { return &i; } |
| 37 | friend const int* rbegin(const R1&& r) { return &r.j; } |
| 38 | }; |
| 39 | |
| 40 | struct R1V // view on an R1 |
| 41 | { |
| 42 | R1& r; |
| 43 | |
| 44 | #if __cpp_lib_ranges_as_const |
| 45 | const int *begin() const; |
| 46 | const int *end() const; |
| 47 | #endif |
| 48 | |
| 49 | friend const long* rbegin(R1V&) { return nullptr; } |
| 50 | friend const int* rbegin(const R1V& rv) noexcept { return rv.r.rbegin(); } |
| 51 | }; |
| 52 | |
| 53 | // Allow ranges::end to work with R1V&& |
| 54 | template<> constexpr bool std::ranges::enable_borrowed_range<R1V> = true; |
| 55 | |
| 56 | void |
| 57 | test01() |
| 58 | { |
| 59 | R1 r; |
| 60 | const R1& c = r; |
| 61 | VERIFY( std::ranges::crbegin(r) == std::ranges::rbegin(c) ); |
| 62 | VERIFY( std::ranges::crbegin(c) == std::ranges::rbegin(c) ); |
| 63 | |
| 64 | R1V v{r}; |
| 65 | VERIFY( std::ranges::crbegin(v) == std::ranges::rbegin(c) ); |
| 66 | VERIFY( std::ranges::crbegin(std::move(v)) == std::ranges::rbegin(c) ); |
| 67 | |
| 68 | const R1V cv{r}; |
| 69 | VERIFY( std::ranges::crbegin(cv) == std::ranges::rbegin(c) ); |
| 70 | VERIFY( std::ranges::crbegin(std::move(cv)) == std::ranges::rbegin(c) ); |
| 71 | } |
| 72 | |
| 73 | struct R2 |
| 74 | { |
| 75 | int a[2] = { }; |
| 76 | long l[2] = { }; |
| 77 | |
| 78 | const int* begin() const { return a; } |
| 79 | const int* end() const { return a + 2; } |
| 80 | |
| 81 | friend const long* begin(const R2&&); // not defined |
| 82 | friend const long* end(const R2&&); // not defined |
| 83 | }; |
| 84 | |
| 85 | // N.B. this is a lie, rbegin on an R2 rvalue will return a dangling pointer. |
| 86 | template<> constexpr bool std::ranges::enable_borrowed_range<R2> = true; |
| 87 | |
| 88 | void |
| 89 | test02() |
| 90 | { |
| 91 | R2 r; |
| 92 | const R2& c = r; |
| 93 | VERIFY( std::ranges::crbegin(r) == std::ranges::rbegin(c) ); |
| 94 | VERIFY( std::ranges::crbegin(c) == std::ranges::rbegin(c) ); |
| 95 | |
| 96 | VERIFY( std::ranges::crbegin(std::move(r)) == std::ranges::rbegin(c) ); |
| 97 | VERIFY( std::ranges::crbegin(std::move(c)) == std::ranges::rbegin(c) ); |
| 98 | } |
| 99 | |
| 100 | int |
| 101 | main() |
| 102 | { |
| 103 | test01(); |
| 104 | test02(); |
| 105 | } |