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

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