| 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 <utility> // as_const |
| 22 |
#include <testsuite_hooks.h> |
| 23 |
#include <testsuite_iterators.h> |
| 24 |
|
| 25 |
static_assert(__gnu_test::is_customization_point_object(std::ranges::cbegin)); |
| 26 |
|
| 27 |
using std::same_as; |
| 28 |
|
| 29 |
void |
| 30 |
test01() |
| 31 |
{ |
| 32 |
int a[2] = {}; |
| 33 |
|
| 34 |
static_assert(same_as<decltype(std::ranges::cbegin(a)), const int*>); |
| 35 |
static_assert(noexcept(std::ranges::cbegin(a))); |
| 36 |
VERIFY( std::ranges::cbegin(a) == (a + 0) ); |
| 37 |
|
| 38 |
constexpr long b[2] = {}; |
| 39 |
static_assert( std::ranges::cbegin(b) == (b + 0) ); |
| 40 |
} |
| 41 |
|
| 42 |
struct R |
| 43 |
{ |
| 44 |
int a[4] = { 0, 1, 2, 3 }; |
| 45 |
|
| 46 |
friend int* begin(R& r) { return r.a + 0; } |
| 47 |
friend int* begin(R&&); // this function is not defined |
| 48 |
friend const int* begin(const R& r) noexcept { return r.a + 2; } |
| 49 |
friend const int* begin(const R&&); // this function is not defined |
| 50 |
|
| 51 |
#if __cpp_lib_ranges_as_const |
| 52 |
friend const int* end(const R&) noexcept; // C++23 requires this. |
| 53 |
#endif |
| 54 |
}; |
| 55 |
|
| 56 |
struct RV // view on an R |
| 57 |
{ |
| 58 |
R& r; |
| 59 |
|
| 60 |
friend int* begin(RV&); // this function is not defined |
| 61 |
friend const int* begin(const RV& rv) noexcept { return begin(std::as_const(rv.r)); } |
| 62 |
|
| 63 |
#if __cpp_lib_ranges_as_const |
| 64 |
friend const int* end(const RV&) noexcept; // C++23 requires this. |
| 65 |
#endif |
| 66 |
}; |
| 67 |
|
| 68 |
// Allow ranges::begin to work with RV&& |
| 69 |
template<> constexpr bool std::ranges::enable_borrowed_range<RV> = true; |
| 70 |
|
| 71 |
void |
| 72 |
test03() |
| 73 |
{ |
| 74 |
R r; |
| 75 |
const R& c = r; |
| 76 |
VERIFY(std::ranges::cbegin(r) == std::ranges::begin(c)); |
| 77 |
VERIFY(std::ranges::cbegin(c) == std::ranges::begin(c)); |
| 78 |
|
| 79 |
RV v{r}; |
| 80 |
VERIFY(std::ranges::cbegin(std::move(v)) == std::ranges::begin(c)); |
| 81 |
const RV cv{r}; |
| 82 |
VERIFY(std::ranges::cbegin(std::move(cv)) == std::ranges::begin(c)); |
| 83 |
} |
| 84 |
|
| 85 |
struct RR |
| 86 |
{ |
| 87 |
short s = 0; |
| 88 |
long l = 0; |
| 89 |
int a[4] = { 0, 1, 2, 3 }; |
| 90 |
|
| 91 |
short* begin() noexcept { return &s; } |
| 92 |
const long* begin() const { return &l; } |
| 93 |
|
| 94 |
friend int* begin(RR& r) { return r.a + 0; } |
| 95 |
friend int* begin(RR&& r) { return r.a + 1; } |
| 96 |
friend const int* begin(const RR& r) { return r.a + 2; } |
| 97 |
friend const int* begin(const RR&& r) noexcept { return r.a + 3; } |
| 98 |
|
| 99 |
#if __cpp_lib_ranges_as_const |
| 100 |
short* end() noexcept { return &s + 1; } // C++23 requires this. |
| 101 |
const long* end() const { return &l + 1; } // C++23 requires this. |
| 102 |
#endif |
| 103 |
}; |
| 104 |
|
| 105 |
// N.B. this is a lie, cbegin on an RR rvalue will return a dangling pointer. |
| 106 |
template<> constexpr bool std::ranges::enable_borrowed_range<RR> = true; |
| 107 |
|
| 108 |
void |
| 109 |
test04() |
| 110 |
{ |
| 111 |
RR r; |
| 112 |
const RR& c = r; |
| 113 |
VERIFY(std::ranges::cbegin(r) == std::ranges::begin(c)); |
| 114 |
VERIFY(std::ranges::cbegin(std::move(r)) == std::ranges::begin(c)); |
| 115 |
VERIFY(std::ranges::cbegin(c) == std::ranges::begin(c)); |
| 116 |
VERIFY(std::ranges::cbegin(std::move(c)) == std::ranges::begin(c)); |
| 117 |
} |
| 118 |
|
| 119 |
void |
| 120 |
test05() |
| 121 |
{ |
| 122 |
// LWG 4027 - possibly-const-range should prefer returning const R& |
| 123 |
auto r = std::views::single(0) |
| 124 |
| std::views::transform([](int) { return 0; }); |
| 125 |
using C1 = decltype(std::ranges::cbegin(r)); |
| 126 |
using C1 = decltype(std::cbegin(r)); |
| 127 |
|
| 128 |
[] (auto x) { |
| 129 |
auto r = std::views::single(x) | std::views::lazy_split(0); |
| 130 |
static_assert(!requires { (*std::ranges::cbegin(r)).front() = 42; }); |
| 131 |
static_assert(!requires { (*std::cbegin(r)).front() = 42; }); |
| 132 |
}(0); |
| 133 |
} |
| 134 |
|
| 135 |
int |
| 136 |
main() |
| 137 |
{ |
| 138 |
test01(); |
| 139 |
test03(); |
| 140 |
test04(); |
| 141 |
test05(); |
| 142 |
} |