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