| 1 | // Copyright (C) 2021-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++23 } } |
| 19 | |
| 20 | #include <string_view> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | void |
| 24 | test01() |
| 25 | { |
| 26 | struct R |
| 27 | { |
| 28 | const char* begin() const |
| 29 | { return str; } |
| 30 | |
| 31 | const char* end() const |
| 32 | { return str + std::char_traits<char>::length(str); } |
| 33 | |
| 34 | const char* str = "Home on the range"; |
| 35 | }; |
| 36 | |
| 37 | R r; |
| 38 | std::string_view s{r}; |
| 39 | VERIFY( s == r.str ); |
| 40 | VERIFY( s.data() == std::ranges::data(r) ); |
| 41 | VERIFY( s.size() == std::ranges::size(r) ); |
| 42 | |
| 43 | struct R2 : R |
| 44 | { |
| 45 | using R::begin; |
| 46 | using R::end; |
| 47 | operator std::string_view() const { return "Out of range"; } |
| 48 | }; |
| 49 | static_assert( std::ranges::contiguous_range<R2> ); |
| 50 | static_assert( std::ranges::sized_range<R2> ); |
| 51 | R2 r2; |
| 52 | std::string_view s2(r2); // uses conversion to string_view |
| 53 | VERIFY( s2 == "Out of range" ); |
| 54 | VERIFY( std::string_view(const_cast<const R2&>(r2)) == s2 ); |
| 55 | |
| 56 | // And again using copy-initialization instead of direct-initialization. |
| 57 | std::string_view s2_implicit = r2; // uses conversion to string_view |
| 58 | VERIFY( s2_implicit == "Out of range" ); |
| 59 | VERIFY( std::string_view(const_cast<const R2&>(r2)) == s2_implicit ); |
| 60 | |
| 61 | struct R3 : R |
| 62 | { |
| 63 | using R::begin; |
| 64 | using R::end; |
| 65 | operator const char*() { return "Orange"; } |
| 66 | }; |
| 67 | static_assert( std::ranges::contiguous_range<R3> ); |
| 68 | static_assert( std::ranges::sized_range<R3> ); |
| 69 | R3 r3; |
| 70 | std::string_view s3(r3); // uses conversion to const char* |
| 71 | VERIFY( s3 == "Orange" ); |
| 72 | s3 = std::string_view(const_cast<const R3&>(r3)); // uses range constructor |
| 73 | VERIFY( s3 == "Home on the range" ); |
| 74 | |
| 75 | struct R4 : R |
| 76 | { |
| 77 | using R::begin; |
| 78 | using R::end; |
| 79 | operator std::string_view() { return "Strange"; } |
| 80 | }; |
| 81 | static_assert( std::ranges::contiguous_range<R4> ); |
| 82 | static_assert( std::ranges::sized_range<R4> ); |
| 83 | R4 r4; |
| 84 | std::string_view s4 = r4; // Uses conversion to string_view |
| 85 | VERIFY( s4 == "Strange" ); |
| 86 | // Cannot construct from const R4 because of non-const conversion op: |
| 87 | static_assert( ! std::is_constructible_v<std::string_view, const R4&> ); |
| 88 | |
| 89 | struct R5 : R |
| 90 | { |
| 91 | using R::begin; |
| 92 | using R::end; |
| 93 | operator std::string_view() && { return "Stranger"; } |
| 94 | }; |
| 95 | static_assert( std::ranges::contiguous_range<R5> ); |
| 96 | static_assert( std::ranges::sized_range<R5> ); |
| 97 | R5 r5; |
| 98 | std::string_view s5(r5); // Uses range constructor |
| 99 | VERIFY( s5 == r5.str ); |
| 100 | s5 = std::string_view(std::move(r5)); // In C++20 this used conversion op. |
| 101 | VERIFY( s5 == r5.str ); // In C++23 it uses range constructor. |
| 102 | |
| 103 | char arr[] = "arrangement\0with\0nulls"; |
| 104 | std::string_view sa = arr; // Does not use range constructor |
| 105 | VERIFY( sa.data() == arr ); |
| 106 | VERIFY( sa == "arrangement" ); |
| 107 | VERIFY( std::end(sa) != std::end(arr) ); |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | test02() |
| 112 | { |
| 113 | using V1 = std::basic_string_view<signed char>; |
| 114 | // range_value_t<V1> is not the right type |
| 115 | static_assert( ! std::is_constructible_v<std::string_view, V1> ); |
| 116 | |
| 117 | using V2 = std::basic_string_view<char, __gnu_cxx::char_traits<char>>; |
| 118 | // LWG 3857 |
| 119 | // basic_string_view should allow explicit conversion when only traits vary |
| 120 | static_assert( std::is_constructible_v<std::string_view, V2> ); |
| 121 | static_assert( ! std::is_convertible_v<V2, std::string_view> ); |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | test03() |
| 126 | { |
| 127 | struct R |
| 128 | { |
| 129 | char* begin() { return nullptr; } |
| 130 | const char* begin() const noexcept { return nullptr; } |
| 131 | |
| 132 | char* end() { return nullptr; } |
| 133 | const char* end() const noexcept { return nullptr; } |
| 134 | }; |
| 135 | |
| 136 | static_assert( ! noexcept(std::string_view(std::declval<R&>())) ); |
| 137 | static_assert( noexcept(std::string_view(std::declval<const R&>())) ); |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | test04() |
| 142 | { |
| 143 | struct R |
| 144 | { |
| 145 | const char* begin() const { return nullptr; } |
| 146 | const char* end() const { return nullptr; } |
| 147 | }; |
| 148 | |
| 149 | R r; |
| 150 | std::basic_string_view s(r); // Use deduction guide. |
| 151 | |
| 152 | static_assert( std::is_same_v<decltype(s), std::string_view> ); |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | test05() |
| 157 | { |
| 158 | struct R |
| 159 | { |
| 160 | const char* begin() const { return nullptr; } |
| 161 | const char* end() const { return nullptr; } |
| 162 | }; |
| 163 | |
| 164 | // P2499R0 string_view range constructor should be explicit |
| 165 | // P2516R0 string_view is implicitly convertible from what? |
| 166 | static_assert( ! std::is_convertible_v<R, std::string_view> ); |
| 167 | } |
| 168 | |
| 169 | int main() |
| 170 | { |
| 171 | test01(); |
| 172 | test02(); |
| 173 | test03(); |
| 174 | test04(); |
| 175 | test05(); |
| 176 | } |