| 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::cdata)); |
| 25 | |
| 26 | template<typename T> |
| 27 | concept has_cdata |
| 28 | = requires (T&& t) { std::ranges::cdata(std::forward<T>(t)); }; |
| 29 | |
| 30 | void |
| 31 | test01() |
| 32 | { |
| 33 | struct R |
| 34 | { |
| 35 | int i = 0; |
| 36 | int j = 0; |
| 37 | |
| 38 | #if __cpp_lib_ranges_as_const |
| 39 | // These overloads mean that range<R> and range<const R> are satisfied. |
| 40 | const int* begin() const { throw; } |
| 41 | const int* end() const { throw; } |
| 42 | #endif |
| 43 | |
| 44 | int* data() { return &j; } |
| 45 | const R* data() const noexcept { return nullptr; } |
| 46 | }; |
| 47 | static_assert( has_cdata<R&> ); |
| 48 | static_assert( has_cdata<const R&> ); |
| 49 | R r; |
| 50 | VERIFY( std::ranges::cdata(r) == (R*)nullptr ); |
| 51 | static_assert( noexcept(std::ranges::cdata(r)) ); |
| 52 | const R& c = r; |
| 53 | VERIFY( std::ranges::cdata(c) == (R*)nullptr ); |
| 54 | static_assert( noexcept(std::ranges::cdata(c)) ); |
| 55 | |
| 56 | // not lvalues and not borrowed ranges |
| 57 | static_assert( !has_cdata<R> ); |
| 58 | static_assert( !has_cdata<const R> ); |
| 59 | |
| 60 | struct R2 |
| 61 | { |
| 62 | #if __cpp_lib_ranges_as_const |
| 63 | // These overloads mean that range<R2> and range<const R2> are satisfied. |
| 64 | const int* begin() const { throw; } |
| 65 | const int* end() const { throw; } |
| 66 | #endif |
| 67 | |
| 68 | int i = 0; |
| 69 | int j = 0; |
| 70 | int* data() { return &j; } |
| 71 | const R2* data() const noexcept { return nullptr; } |
| 72 | }; |
| 73 | static_assert( has_cdata<R2&> ); |
| 74 | static_assert( has_cdata<const R2&> ); |
| 75 | R2 r2; |
| 76 | VERIFY( std::ranges::cdata(r2) == (R2*)nullptr ); |
| 77 | static_assert( noexcept(std::ranges::cdata(r2)) ); |
| 78 | const R2& c2 = r2; |
| 79 | VERIFY( std::ranges::cdata(c2) == (R2*)nullptr ); |
| 80 | static_assert( noexcept(std::ranges::cdata(c2)) ); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | test02() |
| 85 | { |
| 86 | int a[] = { 0, 1 }; |
| 87 | VERIFY( std::ranges::cdata(a) == a + 0 ); |
| 88 | |
| 89 | static_assert( has_cdata<int(&)[2]> ); |
| 90 | static_assert( !has_cdata<int(&&)[2]> ); |
| 91 | } |
| 92 | |
| 93 | struct R3 |
| 94 | { |
| 95 | static inline int i = 0; |
| 96 | static inline long l = 0; |
| 97 | |
| 98 | int* data() &; // this function is not defined |
| 99 | friend long* begin(R3&& r); // not defined |
| 100 | friend const long* begin(const R3& r) { return &r.l; } |
| 101 | friend const short* begin(const R3&&); // not defined |
| 102 | |
| 103 | #if __cpp_lib_ranges_as_const |
| 104 | // C++23 needs these so that range<R3> is satisfied and so that |
| 105 | // possibly-const-range<R3> is not the same type as R3. |
| 106 | friend long* begin(R3&); |
| 107 | friend long* end(R3&); |
| 108 | friend const long* end(const R3& r); |
| 109 | #endif |
| 110 | }; |
| 111 | |
| 112 | template<> constexpr bool std::ranges::enable_borrowed_range<R3> = true; |
| 113 | |
| 114 | void |
| 115 | test03() |
| 116 | { |
| 117 | static_assert( has_cdata<R3&> ); |
| 118 | static_assert( has_cdata<R3> ); // borrowed range |
| 119 | static_assert( has_cdata<const R3&> ); |
| 120 | static_assert( has_cdata<const R3> ); // borrowed range |
| 121 | |
| 122 | R3 r; |
| 123 | const R3& c = r; |
| 124 | VERIFY( std::ranges::cdata(r) == std::ranges::data(c) ); |
| 125 | VERIFY( std::ranges::cdata(std::move(r)) == std::ranges::data(c) ); |
| 126 | VERIFY( std::ranges::cdata(std::move(c)) == std::ranges::begin(c) ); |
| 127 | } |
| 128 | |
| 129 | int |
| 130 | main() |
| 131 | { |
| 132 | test01(); |
| 133 | test02(); |
| 134 | test03(); |
| 135 | } |