| 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 C> |
| 24 | using input_iterator_seq |
| 25 | = __gnu_test::test_container<C, __gnu_test::input_iterator_wrapper>; |
| 26 | |
| 27 | template<typename T, typename U> struct require_same; |
| 28 | template<typename T> struct require_same<T, T> { using type = void; }; |
| 29 | |
| 30 | template<typename T, typename U> |
| 31 | typename require_same<T, U>::type |
| 32 | check_type(U&) { } |
| 33 | |
| 34 | void |
| 35 | test01() |
| 36 | { |
| 37 | std::string s0; |
| 38 | std::allocator<char> a; |
| 39 | |
| 40 | std::basic_string s1 = s0; |
| 41 | check_type<std::string>(s1); |
| 42 | |
| 43 | std::basic_string s2 = std::move(s0); |
| 44 | check_type<std::string>(s2); |
| 45 | |
| 46 | const std::basic_string s3 = s0; |
| 47 | check_type<const std::string>(s3); |
| 48 | |
| 49 | const std::basic_string s4 = s3; |
| 50 | check_type<const std::string>(s4); |
| 51 | |
| 52 | #if _GLIBCXX_USE_CXX11_ABI |
| 53 | std::basic_string s5(s0, a); |
| 54 | check_type<std::string>(s5); |
| 55 | |
| 56 | std::basic_string s6(std::move(s0), a); |
| 57 | check_type<std::string>(s6); |
| 58 | #endif |
| 59 | |
| 60 | std::basic_string s7(s0, 0, 0); |
| 61 | check_type<std::string>(s7); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | test02() |
| 66 | { |
| 67 | char a[1] = {}; |
| 68 | input_iterator_seq<char> seq(a); |
| 69 | |
| 70 | std::basic_string s1(seq.begin(), seq.end()); |
| 71 | check_type<std::string>(s1); |
| 72 | |
| 73 | std::basic_string s2(seq.begin(), seq.end(), std::allocator<char>()); |
| 74 | check_type<std::string>(s2); |
| 75 | |
| 76 | std::basic_string s3((char)1, 'a'); |
| 77 | check_type<std::string>(s3); |
| 78 | |
| 79 | std::basic_string s4((char)1, 'a', std::allocator<char>()); |
| 80 | check_type<std::string>(s4); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | test03() |
| 85 | { |
| 86 | char16_t a[1] = {}; |
| 87 | input_iterator_seq<char16_t> seq(a); |
| 88 | |
| 89 | std::basic_string s1(seq.begin(), seq.end()); |
| 90 | check_type<std::u16string>(s1); |
| 91 | |
| 92 | std::basic_string s2(seq.begin(), seq.end(), std::allocator<char16_t>()); |
| 93 | check_type<std::u16string>(s2); |
| 94 | |
| 95 | std::basic_string s3((char16_t)1, u'a'); |
| 96 | check_type<std::u16string>(s3); |
| 97 | |
| 98 | std::basic_string s4((char16_t)1, u'a', std::allocator<char16_t>()); |
| 99 | check_type<std::u16string>(s4); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | test04() |
| 104 | { |
| 105 | char32_t a[1] = {}; |
| 106 | input_iterator_seq<char32_t> seq(a); |
| 107 | |
| 108 | std::basic_string s1(seq.begin(), seq.end()); |
| 109 | check_type<std::u32string>(s1); |
| 110 | |
| 111 | std::basic_string s2(seq.begin(), seq.end(), std::allocator<char32_t>()); |
| 112 | check_type<std::u32string>(s2); |
| 113 | |
| 114 | std::basic_string s3((char32_t)1, U'a'); |
| 115 | check_type<std::u32string>(s3); |
| 116 | |
| 117 | std::basic_string s4((char32_t)1, U'a', std::allocator<char32_t>()); |
| 118 | check_type<std::u32string>(s4); |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | test05() |
| 123 | { |
| 124 | #ifdef _GLIBCXX_USE_CHAR8_T |
| 125 | char8_t a[1] = {}; |
| 126 | input_iterator_seq<char8_t> seq(a); |
| 127 | |
| 128 | std::basic_string s1(seq.begin(), seq.end()); |
| 129 | check_type<std::u8string>(s1); |
| 130 | |
| 131 | std::basic_string s2(seq.begin(), seq.end(), std::allocator<char8_t>()); |
| 132 | check_type<std::u8string>(s2); |
| 133 | |
| 134 | std::basic_string s3((char8_t)1, u8'a'); |
| 135 | check_type<std::u8string>(s3); |
| 136 | |
| 137 | std::basic_string s4((char8_t)1, u8'a', std::allocator<char8_t>()); |
| 138 | check_type<std::u8string>(s4); |
| 139 | #endif |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | test06() |
| 144 | { |
| 145 | // LWG 3075 basic_string needs deduction guides from basic_string_view |
| 146 | std::string_view sv{"A View to a Kill"}; |
| 147 | const std::allocator<char> a; |
| 148 | |
| 149 | std::basic_string s1(sv); |
| 150 | check_type<std::string>(s1); |
| 151 | |
| 152 | std::basic_string s2(sv, a); |
| 153 | check_type<std::string>(s2); |
| 154 | |
| 155 | std::basic_string s3(sv, 2u, 6u); |
| 156 | check_type<std::string>(s3); |
| 157 | |
| 158 | std::basic_string s4(sv, 2u, 6u, a); |
| 159 | check_type<std::string>(s4); |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | test07() |
| 164 | { |
| 165 | // LWG 3076 basic_string CTAD ambiguity |
| 166 | using namespace std; |
| 167 | string s0; |
| 168 | |
| 169 | basic_string s1(s0, 1, 1); |
| 170 | check_type<std::string>(s1); |
| 171 | |
| 172 | basic_string s2("cat"sv, 1, 1); |
| 173 | check_type<std::string>(s2); |
| 174 | |
| 175 | basic_string s3("cat", 1); |
| 176 | check_type<std::string>(s3); |
| 177 | } |