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 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
18//
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.
22
23#include <cassert>
24#include <cstddef>
25#include <iterator>
26#include <string>
27#include <type_traits>
28
29#include "test_macros.h"
30#include "test_allocator.h"
31#include "min_allocator.h"
32#include "asan_testing.h"
33
34class NotAnIterator {};
35using NotAnInputIterator = std::back_insert_iterator<std::basic_string<char16_t>>;
36
37template <typename T>
38struct NotAnAllocator {
39 typedef T value_type;
40};
41
42template <class Iter, class Alloc, class = void>
43struct CanDeduce : std::false_type {};
44
45template <class Iter, class Alloc>
46struct CanDeduce<Iter,
47 Alloc,
48 decltype((void)std::basic_string{std::declval<Iter>(), std::declval<Iter>(), std::declval<Alloc>()})>
49 : std::true_type {};
50
51static_assert(CanDeduce<char*, std::allocator<char>>::value);
52static_assert(!CanDeduce<NotAnIterator, std::allocator<char>>::value);
53static_assert(!CanDeduce<NotAnInputIterator, std::allocator<char16_t>>::value);
54static_assert(!CanDeduce<char*, NotAnAllocator<char>>::value);
55#ifndef TEST_HAS_NO_WIDE_CHARACTERS
56static_assert(CanDeduce<wchar_t*, std::allocator<wchar_t>>::value);
57static_assert(!CanDeduce<wchar_t const*, NotAnAllocator<wchar_t>>::value);
58#endif
59
60TEST_CONSTEXPR_CXX20 bool test() {
61 {
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));
71 }
72 {
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));
82 }
83#ifndef TEST_HAS_NO_WIDE_CHARACTERS
84 {
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));
94 }
95#endif
96 {
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));
106 }
107 {
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));
117 }
118
119 return true;
120}
121
122int main(int, char**) {
123 test();
124#if TEST_STD_VER > 17
125 static_assert(test());
126#endif
127
128 return 0;
129}
130

source code of libcxx/test/std/strings/basic.string/string.cons/iter_alloc_deduction.pass.cpp