| 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::begin)); |
| 25 |
|
| 26 |
using std::same_as; |
| 27 |
|
| 28 |
void |
| 29 |
test01() |
| 30 |
{ |
| 31 |
int a[2] = {}; |
| 32 |
|
| 33 |
static_assert(same_as<decltype(std::ranges::begin(a)), decltype(a + 0)>); |
| 34 |
static_assert(noexcept(std::ranges::begin(a))); |
| 35 |
VERIFY( std::ranges::begin(a) == (a + 0) ); |
| 36 |
|
| 37 |
constexpr long b[2] = { }; |
| 38 |
static_assert( std::ranges::begin(b) == (b + 0) ); |
| 39 |
|
| 40 |
struct X { }; |
| 41 |
using A = X[]; // unbounded array |
| 42 |
extern A& f(); |
| 43 |
static_assert( same_as<decltype(std::ranges::begin(f())), X*> ); |
| 44 |
} |
| 45 |
|
| 46 |
void |
| 47 |
test02() |
| 48 |
{ |
| 49 |
using __gnu_test::test_range; |
| 50 |
using __gnu_test::random_access_iterator_wrapper; |
| 51 |
using __gnu_test::input_iterator_wrapper; |
| 52 |
using __gnu_test::output_iterator_wrapper; |
| 53 |
|
| 54 |
int a[] = { 0, 1 }; |
| 55 |
|
| 56 |
test_range<int, random_access_iterator_wrapper> r(a); |
| 57 |
static_assert(same_as<decltype(std::ranges::begin(r)), decltype(r.begin())>); |
| 58 |
VERIFY( std::ranges::begin(r) == r.begin() ); |
| 59 |
|
| 60 |
test_range<int, input_iterator_wrapper> i(a); |
| 61 |
static_assert(same_as<decltype(std::ranges::begin(i)), decltype(i.begin())>); |
| 62 |
VERIFY( std::ranges::begin(i) == i.begin() ); |
| 63 |
|
| 64 |
test_range<int, output_iterator_wrapper> o(a); |
| 65 |
static_assert(same_as<decltype(std::ranges::begin(o)), decltype(o.begin())>); |
| 66 |
*std::ranges::begin(o) = 99; |
| 67 |
VERIFY( a[0] == 99 ); |
| 68 |
} |
| 69 |
|
| 70 |
struct R |
| 71 |
{ |
| 72 |
int a[4] = { 0, 1, 2, 3 }; |
| 73 |
|
| 74 |
friend int* begin(R& r) { return r.a + 0; } |
| 75 |
friend int* begin(R&& r); // this overload is not defined |
| 76 |
friend const int* begin(const R& r) noexcept { return r.a + 2; } |
| 77 |
friend const int* begin(const R&& r) noexcept; // not defined |
| 78 |
}; |
| 79 |
|
| 80 |
struct RV // view on an R |
| 81 |
{ |
| 82 |
R& r; |
| 83 |
|
| 84 |
friend int* begin(RV& rv) { return begin(rv.r); } |
| 85 |
friend const int* begin(const RV& rv) noexcept { return begin(rv.r); } |
| 86 |
}; |
| 87 |
|
| 88 |
// Allow ranges::begin to work with RV&& |
| 89 |
template<> constexpr bool std::ranges::enable_borrowed_range<RV> = true; |
| 90 |
|
| 91 |
void |
| 92 |
test03() |
| 93 |
{ |
| 94 |
R r; |
| 95 |
const R& c = r; |
| 96 |
|
| 97 |
static_assert(same_as<decltype(std::ranges::begin(r)), decltype(begin(r))>); |
| 98 |
static_assert(!noexcept(std::ranges::begin(r))); |
| 99 |
VERIFY( std::ranges::begin(r) == begin(r) ); |
| 100 |
|
| 101 |
static_assert(same_as<decltype(std::ranges::begin(c)), decltype(begin(c))>); |
| 102 |
static_assert(noexcept(std::ranges::begin(c))); |
| 103 |
VERIFY( std::ranges::begin(c) == begin(c) ); |
| 104 |
|
| 105 |
RV v{r}; |
| 106 |
// enable_borrowed_range<RV> allows ranges::begin to work for rvalues, |
| 107 |
// but it will call v.begin() or begin(v) on an lvalue: |
| 108 |
static_assert(same_as<decltype(std::ranges::begin(std::move(v))), |
| 109 |
decltype(begin(v))>); |
| 110 |
static_assert(!noexcept(std::ranges::begin(std::move(v)))); |
| 111 |
VERIFY( std::ranges::begin(std::move(v)) == begin(v) ); |
| 112 |
|
| 113 |
const RV cv{r}; |
| 114 |
static_assert(same_as<decltype(std::ranges::begin(std::move(cv))), |
| 115 |
decltype(begin(cv))>); |
| 116 |
static_assert(noexcept(std::ranges::begin(std::move(cv)))); |
| 117 |
VERIFY( std::ranges::begin(std::move(cv)) == begin(cv) ); |
| 118 |
} |
| 119 |
|
| 120 |
struct RR |
| 121 |
{ |
| 122 |
short s = 0; |
| 123 |
long l = 0; |
| 124 |
int a[4] = { 0, 1, 2, 3 }; |
| 125 |
|
| 126 |
short* begin() noexcept { return &s; } |
| 127 |
const long* begin() const { return &l; } |
| 128 |
|
| 129 |
friend int* begin(RR& r) noexcept { return r.a + 0; } |
| 130 |
friend int* begin(RR&& r); // not defined |
| 131 |
friend const int* begin(const RR& r) { return r.a + 2; } |
| 132 |
friend const int* begin(const RR&& r) noexcept; // not defined |
| 133 |
}; |
| 134 |
|
| 135 |
// N.B. this is a lie, begin on an RR rvalue will return a dangling pointer. |
| 136 |
template<> constexpr bool std::ranges::enable_borrowed_range<RR> = true; |
| 137 |
|
| 138 |
void |
| 139 |
test04() |
| 140 |
{ |
| 141 |
RR r; |
| 142 |
const RR& c = r; |
| 143 |
VERIFY( std::ranges::begin(r) == &r.s ); |
| 144 |
static_assert(noexcept(std::ranges::begin(r))); |
| 145 |
|
| 146 |
// calls r.begin() on an lvalue, not rvalue |
| 147 |
VERIFY( std::ranges::begin(std::move(r)) == std::ranges::begin(r) ); |
| 148 |
static_assert(noexcept(std::ranges::begin(std::move(r)))); |
| 149 |
|
| 150 |
VERIFY( std::ranges::begin(c) == &r.l ); |
| 151 |
static_assert(!noexcept(std::ranges::begin(c))); |
| 152 |
|
| 153 |
// calls r.begin() on a const lvalue, not rvalue |
| 154 |
VERIFY( std::ranges::begin(std::move(c)) == std::ranges::begin(c) ); |
| 155 |
static_assert(!noexcept(std::ranges::begin(std::move(c)))); |
| 156 |
} |
| 157 |
|
| 158 |
int |
| 159 |
main() |
| 160 |
{ |
| 161 |
test01(); |
| 162 |
test02(); |
| 163 |
test03(); |
| 164 |
test04(); |
| 165 |
} |