Debian Codegraph @

libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/deduction.cc

raw
1 // Copyright (C) 2017-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 compile { target c++17 } }
19
20 #include <string>
21 #include <testsuite_iterators.h>
22
23 template<typename T, typename U> struct require_same;
24 template<typename T> struct require_same<T, T> { using type = void; };
25
26 template<typename T, typename U>
27 typename require_same<T, U>::type
28 check_type(U&) { }
29
30 void
31 test01()
32 {
33 std::wstring s0;
34 std::allocator<wchar_t> a;
35
36 std::basic_string s1 = s0;
37 check_type<std::wstring>(s1);
38
39 std::basic_string s2 = std::move(s0);
40 check_type<std::wstring>(s2);
41
42 const std::basic_string s3 = s0;
43 check_type<const std::wstring>(s3);
44
45 const std::basic_string s4 = s2;
46 check_type<const std::wstring>(s4);
47
48 #if _GLIBCXX_USE_CXX11_ABI
49 std::basic_string s5(s0, a);
50 check_type<std::wstring>(s5);
51
52 std::basic_string s6(std::move(s0), a);
53 check_type<std::wstring>(s6);
54 #endif
55
56 std::basic_string s7(s0, 0, 0);
57 check_type<std::wstring>(s7);
58 }
59
60 void
61 test02()
62 {
63 using namespace __gnu_test;
64 wchar_t a[1] = {};
65 test_container<wchar_t, input_iterator_wrapper> seq(a);
66
67 std::basic_string s1(seq.begin(), seq.end());
68 check_type<std::wstring>(s1);
69
70 std::basic_string s2(seq.begin(), seq.end(), std::allocator<wchar_t>());
71 check_type<std::wstring>(s2);
72
73 std::basic_string s3((wchar_t)1, L'a');
74 check_type<std::wstring>(s3);
75
76 std::basic_string s4((wchar_t)1, L'a', std::allocator<wchar_t>());
77 check_type<std::wstring>(s4);
78 }
79
80 void
81 test05()
82 {
83 // LWG 3075 basic_string needs deduction guides from basic_string_view
84 std::wstring_view sv{L"A View to a Kill"};
85 const std::allocator<wchar_t> a;
86
87 std::basic_string s1(sv);
88 check_type<std::wstring>(s1);
89
90 std::basic_string s2(sv, a);
91 check_type<std::wstring>(s2);
92
93 std::basic_string s3(sv, 2u, 6u);
94 check_type<std::wstring>(s3);
95
96 std::basic_string s4(sv, 2u, 6u, a);
97 check_type<std::wstring>(s4);
98 }
99
100 void
101 test06()
102 {
103 // LWG 3076 basic_string CTAD ambiguity
104 using namespace std;
105 wstring s0;
106
107 basic_string s1(s0, 1, 1);
108 check_type<std::wstring>(s1);
109
110 basic_string s2(L"cat"sv, 1, 1);
111 check_type<std::wstring>(s2);
112
113 basic_string s3(L"cat", 1);
114 check_type<std::wstring>(s3);
115 }