1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++03, c++11, c++14
12 // template<class charT,
14 // class Allocator = allocator<charT>
16 // basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())
17 // -> basic_string<charT, traits, Allocator>;
19 // The deduction guide shall not participate in overload resolution if Allocator
20 // is a type that does not qualify as an allocator.
23 #include <string_view>
26 #include <type_traits>
30 #include "test_macros.h"
31 #include "test_allocator.h"
32 #include "min_allocator.h"
34 template <class StringView
, class Allocator
, class = void>
35 struct CanDeduce
: std::false_type
{};
37 template <class StringView
, class Allocator
>
38 struct CanDeduce
<StringView
,
40 decltype((void)std::basic_string
{std::declval
<StringView
>(), std::declval
<Allocator
>()})>
43 struct NotAnAllocator
{};
44 static_assert(CanDeduce
<std::string_view
, std::allocator
<char>>::value
);
45 static_assert(!CanDeduce
<std::string_view
, NotAnAllocator
>::value
);
47 TEST_CONSTEXPR_CXX20
bool test() {
49 std::string_view sv
= "12345678901234";
50 std::basic_string
s1(sv
);
51 using S
= decltype(s1
); // what type did we get?
52 static_assert(std::is_same_v
<S::value_type
, char>, "");
53 static_assert(std::is_same_v
<S::traits_type
, std::char_traits
<char>>, "");
54 static_assert(std::is_same_v
<S::allocator_type
, std::allocator
<char>>, "");
55 assert(s1
.size() == sv
.size());
56 assert(s1
.compare(0, s1
.size(), sv
.data(), s1
.size()) == 0);
60 std::string_view sv
= "12345678901234";
61 std::basic_string s1
{sv
, std::allocator
<char>{}};
62 using S
= decltype(s1
); // what type did we get?
63 static_assert(std::is_same_v
<S::value_type
, char>, "");
64 static_assert(std::is_same_v
<S::traits_type
, std::char_traits
<char>>, "");
65 static_assert(std::is_same_v
<S::allocator_type
, std::allocator
<char>>, "");
66 assert(s1
.size() == sv
.size());
67 assert(s1
.compare(0, s1
.size(), sv
.data(), s1
.size()) == 0);
69 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
71 std::wstring_view sv
= L
"12345678901234";
72 std::basic_string s1
{sv
, test_allocator
<wchar_t>{}};
73 using S
= decltype(s1
); // what type did we get?
74 static_assert(std::is_same_v
<S::value_type
, wchar_t>, "");
75 static_assert(std::is_same_v
<S::traits_type
, std::char_traits
<wchar_t>>, "");
76 static_assert(std::is_same_v
<S::allocator_type
, test_allocator
<wchar_t>>, "");
77 assert(s1
.size() == sv
.size());
78 assert(s1
.compare(0, s1
.size(), sv
.data(), s1
.size()) == 0);
81 #ifndef TEST_HAS_NO_CHAR8_T
83 std::u8string_view sv
= u8
"12345678901234";
84 std::basic_string s1
{sv
, min_allocator
<char8_t
>{}};
85 using S
= decltype(s1
); // what type did we get?
86 static_assert(std::is_same_v
<S::value_type
, char8_t
>, "");
87 static_assert(std::is_same_v
<S::traits_type
, std::char_traits
<char8_t
>>, "");
88 static_assert(std::is_same_v
<S::allocator_type
, min_allocator
<char8_t
>>, "");
89 assert(s1
.size() == sv
.size());
90 assert(s1
.compare(0, s1
.size(), sv
.data(), s1
.size()) == 0);
94 std::u16string_view sv
= u
"12345678901234";
95 std::basic_string s1
{sv
, min_allocator
<char16_t
>{}};
96 using S
= decltype(s1
); // what type did we get?
97 static_assert(std::is_same_v
<S::value_type
, char16_t
>, "");
98 static_assert(std::is_same_v
<S::traits_type
, std::char_traits
<char16_t
>>, "");
99 static_assert(std::is_same_v
<S::allocator_type
, min_allocator
<char16_t
>>, "");
100 assert(s1
.size() == sv
.size());
101 assert(s1
.compare(0, s1
.size(), sv
.data(), s1
.size()) == 0);
104 std::u32string_view sv
= U
"12345678901234";
105 std::basic_string s1
{sv
, explicit_allocator
<char32_t
>{}};
106 using S
= decltype(s1
); // what type did we get?
107 static_assert(std::is_same_v
<S::value_type
, char32_t
>, "");
108 static_assert(std::is_same_v
<S::traits_type
, std::char_traits
<char32_t
>>, "");
109 static_assert(std::is_same_v
<S::allocator_type
, explicit_allocator
<char32_t
>>, "");
110 assert(s1
.size() == sv
.size());
111 assert(s1
.compare(0, s1
.size(), sv
.data(), s1
.size()) == 0);
117 int main(int, char**) {
119 #if TEST_STD_VER > 17
120 static_assert(test());