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 InputIterator,
13 // class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
14 // basic_string(InputIterator, InputIterator, Allocator = Allocator())
15 // -> basic_string<typename iterator_traits<InputIterator>::value_type,
16 // char_traits<typename iterator_traits<InputIterator>::value_type>,
17 // Allocator>; // constexpr since C++20
19 // The deduction guide shall not participate in overload resolution if InputIterator
20 // is a type that does not qualify as an input iterator, or if Allocator is a type
21 // that does not qualify as an allocator.
27 #include <type_traits>
29 #include "test_macros.h"
30 #include "test_allocator.h"
31 #include "min_allocator.h"
32 #include "asan_testing.h"
34 class NotAnIterator
{};
35 using NotAnInputIterator
= std::back_insert_iterator
<std::basic_string
<char16_t
>>;
38 struct NotAnAllocator
{
42 template <class Iter
, class Alloc
, class = void>
43 struct CanDeduce
: std::false_type
{};
45 template <class Iter
, class Alloc
>
46 struct CanDeduce
<Iter
,
48 decltype((void)std::basic_string
{std::declval
<Iter
>(), std::declval
<Iter
>(), std::declval
<Alloc
>()})>
51 static_assert(CanDeduce
<char*, std::allocator
<char>>::value
);
52 static_assert(!CanDeduce
<NotAnIterator
, std::allocator
<char>>::value
);
53 static_assert(!CanDeduce
<NotAnInputIterator
, std::allocator
<char16_t
>>::value
);
54 static_assert(!CanDeduce
<char*, NotAnAllocator
<char>>::value
);
55 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
56 static_assert(CanDeduce
<wchar_t*, std::allocator
<wchar_t>>::value
);
57 static_assert(!CanDeduce
<wchar_t const*, NotAnAllocator
<wchar_t>>::value
);
60 TEST_CONSTEXPR_CXX20
bool test() {
62 const char* s
= "12345678901234";
63 std::basic_string
s1(s
, s
+ 10); // Can't use {} here
64 using S
= decltype(s1
); // what type did we get?
65 static_assert(std::is_same_v
<S::value_type
, char>, "");
66 static_assert(std::is_same_v
<S::traits_type
, std::char_traits
<char>>, "");
67 static_assert(std::is_same_v
<S::allocator_type
, std::allocator
<char>>, "");
68 assert(s1
.size() == 10);
69 assert(s1
.compare(0, s1
.size(), s
, s1
.size()) == 0);
70 LIBCPP_ASSERT(is_string_asan_correct(s1
));
73 const char* s
= "12345678901234";
74 std::basic_string s1
{s
, s
+ 10, std::allocator
<char>{}};
75 using S
= decltype(s1
); // what type did we get?
76 static_assert(std::is_same_v
<S::value_type
, char>, "");
77 static_assert(std::is_same_v
<S::traits_type
, std::char_traits
<char>>, "");
78 static_assert(std::is_same_v
<S::allocator_type
, std::allocator
<char>>, "");
79 assert(s1
.size() == 10);
80 assert(s1
.compare(0, s1
.size(), s
, s1
.size()) == 0);
81 LIBCPP_ASSERT(is_string_asan_correct(s1
));
83 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
85 const wchar_t* s
= L
"12345678901234";
86 std::basic_string s1
{s
, s
+ 10, test_allocator
<wchar_t>{}};
87 using S
= decltype(s1
); // what type did we get?
88 static_assert(std::is_same_v
<S::value_type
, wchar_t>, "");
89 static_assert(std::is_same_v
<S::traits_type
, std::char_traits
<wchar_t>>, "");
90 static_assert(std::is_same_v
<S::allocator_type
, test_allocator
<wchar_t>>, "");
91 assert(s1
.size() == 10);
92 assert(s1
.compare(0, s1
.size(), s
, s1
.size()) == 0);
93 LIBCPP_ASSERT(is_string_asan_correct(s1
));
97 const char16_t
* s
= u
"12345678901234";
98 std::basic_string s1
{s
, s
+ 10, min_allocator
<char16_t
>{}};
99 using S
= decltype(s1
); // what type did we get?
100 static_assert(std::is_same_v
<S::value_type
, char16_t
>, "");
101 static_assert(std::is_same_v
<S::traits_type
, std::char_traits
<char16_t
>>, "");
102 static_assert(std::is_same_v
<S::allocator_type
, min_allocator
<char16_t
>>, "");
103 assert(s1
.size() == 10);
104 assert(s1
.compare(0, s1
.size(), s
, s1
.size()) == 0);
105 LIBCPP_ASSERT(is_string_asan_correct(s1
));
108 const char32_t
* s
= U
"12345678901234";
109 std::basic_string s1
{s
, s
+ 10, explicit_allocator
<char32_t
>{}};
110 using S
= decltype(s1
); // what type did we get?
111 static_assert(std::is_same_v
<S::value_type
, char32_t
>, "");
112 static_assert(std::is_same_v
<S::traits_type
, std::char_traits
<char32_t
>>, "");
113 static_assert(std::is_same_v
<S::allocator_type
, explicit_allocator
<char32_t
>>, "");
114 assert(s1
.size() == 10);
115 assert(s1
.compare(0, s1
.size(), s
, s1
.size()) == 0);
116 LIBCPP_ASSERT(is_string_asan_correct(s1
));
122 int main(int, char**) {
124 #if TEST_STD_VER > 17
125 static_assert(test());