1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // XFAIL: LIBCXX-AIX-FIXME 10 11 // <string> 12 // UNSUPPORTED: c++03, c++11, c++14 13 14 // template<class charT, 15 // class traits, 16 // class Allocator = allocator<charT> 17 // > 18 // basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator()) 19 // -> basic_string<charT, traits, Allocator>; 20 // 21 // The deduction guide shall not participate in overload resolution if Allocator 22 // is a type that does not qualify as an allocator. 23 24 #include <string> 25 #include <string_view> 26 #include <iterator> 27 #include <memory> 28 #include <type_traits> 29 #include <cassert> 30 #include <cstddef> 31 32 #include "test_macros.h" 33 #include "test_allocator.h" 34 #include "min_allocator.h" 35 36 template <class StringView, class Allocator, class = void> 37 struct CanDeduce : std::false_type { }; 38 39 template <class StringView, class Allocator> 40 struct CanDeduce<StringView, Allocator, decltype((void) 41 std::basic_string{std::declval<StringView>(), std::declval<Allocator>()} 42 )> : std::true_type { }; 43 44 struct NotAnAllocator { }; 45 static_assert( CanDeduce<std::string_view, std::allocator<char>>::value); 46 static_assert(!CanDeduce<std::string_view, NotAnAllocator>::value); 47 48 TEST_CONSTEXPR_CXX20 bool test() { 49 { 50 std::string_view sv = "12345678901234"; 51 std::basic_string s1(sv); 52 using S = decltype(s1); // what type did we get? 53 static_assert(std::is_same_v<S::value_type, char>, ""); 54 static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, ""); 55 static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, ""); 56 assert(s1.size() == sv.size()); 57 assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0); 58 } 59 60 { 61 std::string_view sv = "12345678901234"; 62 std::basic_string s1{sv, std::allocator<char>{}}; 63 using S = decltype(s1); // what type did we get? 64 static_assert(std::is_same_v<S::value_type, char>, ""); 65 static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, ""); 66 static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, ""); 67 assert(s1.size() == sv.size()); 68 assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0); 69 } 70 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 71 { 72 std::wstring_view sv = L"12345678901234"; 73 std::basic_string s1{sv, test_allocator<wchar_t>{}}; 74 using S = decltype(s1); // what type did we get? 75 static_assert(std::is_same_v<S::value_type, wchar_t>, ""); 76 static_assert(std::is_same_v<S::traits_type, std::char_traits<wchar_t>>, ""); 77 static_assert(std::is_same_v<S::allocator_type, test_allocator<wchar_t>>, ""); 78 assert(s1.size() == sv.size()); 79 assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0); 80 } 81 #endif 82 #ifndef TEST_HAS_NO_CHAR8_T 83 { 84 std::u8string_view sv = u8"12345678901234"; 85 std::basic_string s1{sv, min_allocator<char8_t>{}}; 86 using S = decltype(s1); // what type did we get? 87 static_assert(std::is_same_v<S::value_type, char8_t>, ""); 88 static_assert(std::is_same_v<S::traits_type, std::char_traits<char8_t>>, ""); 89 static_assert(std::is_same_v<S::allocator_type, min_allocator<char8_t>>, ""); 90 assert(s1.size() == sv.size()); 91 assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0); 92 } 93 #endif 94 { 95 std::u16string_view sv = u"12345678901234"; 96 std::basic_string s1{sv, min_allocator<char16_t>{}}; 97 using S = decltype(s1); // what type did we get? 98 static_assert(std::is_same_v<S::value_type, char16_t>, ""); 99 static_assert(std::is_same_v<S::traits_type, std::char_traits<char16_t>>, ""); 100 static_assert(std::is_same_v<S::allocator_type, min_allocator<char16_t>>, ""); 101 assert(s1.size() == sv.size()); 102 assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0); 103 } 104 { 105 std::u32string_view sv = U"12345678901234"; 106 std::basic_string s1{sv, explicit_allocator<char32_t>{}}; 107 using S = decltype(s1); // what type did we get? 108 static_assert(std::is_same_v<S::value_type, char32_t>, ""); 109 static_assert(std::is_same_v<S::traits_type, std::char_traits<char32_t>>, ""); 110 static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, ""); 111 assert(s1.size() == sv.size()); 112 assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0); 113 } 114 115 return true; 116 } 117 118 int main(int, char**) 119 { 120 test(); 121 #if TEST_STD_VER > 17 122 static_assert(test()); 123 #endif 124 125 return 0; 126 } 127