]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/range_c++20.cc
1 // Copyright (C) 2021-2024 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do run { target c++23 } }
20 #include <string_view>
21 #include <testsuite_hooks.h>
28 const wchar_t* begin() const
31 const wchar_t* end() const
32 { return str
+ std::char_traits
<wchar_t>::length(str
); }
34 const wchar_t* str
= L
"Home on the range";
38 std::wstring_view s
{r
};
40 VERIFY( s
.data() == std::ranges::data(r
) );
41 VERIFY( s
.size() == std::ranges::size(r
) );
47 operator std::wstring_view() const { return L
"Out of range"; }
49 static_assert( std::ranges::contiguous_range
<R2
> );
50 static_assert( std::ranges::sized_range
<R2
> );
52 std::wstring_view
s2(r2
); // uses conversion to wstring_view
53 VERIFY( s2
== L
"Out of range" );
54 VERIFY( std::wstring_view(const_cast<const R2
&>(r2
)) == s2
);
56 // And again using copy-initialization instead of direct-initialization.
57 std::wstring_view s2_implicit
= r2
; // uses conversion to wstring_view
58 VERIFY( s2_implicit
== L
"Out of range" );
59 VERIFY( std::wstring_view(const_cast<const R2
&>(r2
)) == s2_implicit
);
65 operator const wchar_t*() { return L
"Orange"; }
67 static_assert( std::ranges::contiguous_range
<R3
> );
68 static_assert( std::ranges::sized_range
<R3
> );
70 std::wstring_view
s3(r3
); // uses conversion to const wchar_t*
71 VERIFY( s3
== L
"Orange" );
72 s3
= std::wstring_view(const_cast<const R3
&>(r3
)); // uses range constructor
73 VERIFY( s3
== L
"Home on the range" );
79 operator std::wstring_view() { return L
"Strange"; }
81 static_assert( std::ranges::contiguous_range
<R4
> );
82 static_assert( std::ranges::sized_range
<R4
> );
84 std::wstring_view s4
= r4
; // Uses conversion to wstring_view
85 VERIFY( s4
== L
"Strange" );
86 // Cannot construct from const R4 because of non-const conversion op:
87 static_assert( ! std::is_constructible_v
<std::wstring_view
, const R4
&> );
93 operator std::wstring_view() && { return L
"Stranger"; }
95 static_assert( std::ranges::contiguous_range
<R5
> );
96 static_assert( std::ranges::sized_range
<R5
> );
98 std::wstring_view
s5(r5
); // Uses range constructor
99 VERIFY( s5
== r5
.str
);
100 s5
= std::wstring_view(std::move(r5
)); // In C++20 this used conversion op.
101 VERIFY( s5
== r5
.str
); // In C++23 it uses range constructor.
103 wchar_t arr
[] = L
"arrangement\0with\0nulls";
104 std::wstring_view sa
= arr
; // Does not use range constructor
105 VERIFY( sa
.data() == arr
);
106 VERIFY( sa
== L
"arrangement" );
107 VERIFY( std::end(sa
) != std::end(arr
) );
113 using V1
= std::basic_string_view
<char>;
114 // range_value_t<V1> is not the right type
115 static_assert( ! std::is_constructible_v
<std::wstring_view
, V1
> );
117 using V2
= std::basic_string_view
<wchar_t, __gnu_cxx::char_traits
<wchar_t>>;
119 // basic_string_view should allow explicit conversion when only traits vary
120 static_assert( std::is_constructible_v
<std::wstring_view
, V2
> );
121 static_assert( ! std::is_convertible_v
<V2
, std::wstring_view
> );
129 wchar_t* begin() { return nullptr; }
130 const wchar_t* begin() const noexcept
{ return nullptr; }
132 wchar_t* end() { return nullptr; }
133 const wchar_t* end() const noexcept
{ return nullptr; }
136 static_assert( ! noexcept(std::wstring_view(std::declval
<R
&>())) );
137 static_assert( noexcept(std::wstring_view(std::declval
<const R
&>())) );
145 const wchar_t* begin() const { return nullptr; }
146 const wchar_t* end() const { return nullptr; }
150 std::basic_string_view
s(r
); // Use deduction guide.
152 static_assert( std::is_same_v
<decltype(s
), std::wstring_view
> );
160 const wchar_t* begin() const { return nullptr; }
161 const wchar_t* end() const { return nullptr; }
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::wstring_view
> );