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"
33 class NotAnIterator
{};
34 using NotAnInputIterator
= std::back_insert_iterator
<std::basic_string
<char16_t
>>;
37 struct NotAnAllocator
{
41 template <class Iter
, class Alloc
, class = void>
42 struct CanDeduce
: std::false_type
{};
44 template <class Iter
, class Alloc
>
45 struct CanDeduce
<Iter
,
47 decltype((void)std::basic_string
{std::declval
<Iter
>(), std::declval
<Iter
>(), std::declval
<Alloc
>()})>
50 static_assert(CanDeduce
<char*, std::allocator
<char>>::value
);
51 static_assert(!CanDeduce
<NotAnIterator
, std::allocator
<char>>::value
);
52 static_assert(!CanDeduce
<NotAnInputIterator
, std::allocator
<char16_t
>>::value
);
53 static_assert(!CanDeduce
<char*, NotAnAllocator
<char>>::value
);
54 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
55 static_assert(CanDeduce
<wchar_t*, std::allocator
<wchar_t>>::value
);
56 static_assert(!CanDeduce
<wchar_t const*, NotAnAllocator
<wchar_t>>::value
);
59 TEST_CONSTEXPR_CXX20
bool test() {
61 const char* s
= "12345678901234";
62 std::basic_string
s1(s
, s
+ 10); // Can't use {} here
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() == 10);
68 assert(s1
.compare(0, s1
.size(), s
, s1
.size()) == 0);
71 const char* s
= "12345678901234";
72 std::basic_string s1
{s
, s
+ 10, std::allocator
<char>{}};
73 using S
= decltype(s1
); // what type did we get?
74 static_assert(std::is_same_v
<S::value_type
, char>, "");
75 static_assert(std::is_same_v
<S::traits_type
, std::char_traits
<char>>, "");
76 static_assert(std::is_same_v
<S::allocator_type
, std::allocator
<char>>, "");
77 assert(s1
.size() == 10);
78 assert(s1
.compare(0, s1
.size(), s
, s1
.size()) == 0);
80 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
82 const wchar_t* s
= L
"12345678901234";
83 std::basic_string s1
{s
, s
+ 10, test_allocator
<wchar_t>{}};
84 using S
= decltype(s1
); // what type did we get?
85 static_assert(std::is_same_v
<S::value_type
, wchar_t>, "");
86 static_assert(std::is_same_v
<S::traits_type
, std::char_traits
<wchar_t>>, "");
87 static_assert(std::is_same_v
<S::allocator_type
, test_allocator
<wchar_t>>, "");
88 assert(s1
.size() == 10);
89 assert(s1
.compare(0, s1
.size(), s
, s1
.size()) == 0);
93 const char16_t
* s
= u
"12345678901234";
94 std::basic_string s1
{s
, s
+ 10, 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() == 10);
100 assert(s1
.compare(0, s1
.size(), s
, s1
.size()) == 0);
103 const char32_t
* s
= U
"12345678901234";
104 std::basic_string s1
{s
, s
+ 10, 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() == 10);
110 assert(s1
.compare(0, s1
.size(), s
, s1
.size()) == 0);
116 int main(int, char**) {
118 #if TEST_STD_VER > 17
119 static_assert(test());