xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/string_view_deduction.pass.cpp (revision e85018b7dd3a89dda6b22319b03b5906f8a85aed)
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 //   basic_string(InputIterator begin, InputIterator end,
14 //   const Allocator& a = Allocator());
15 
16 // template<class charT,
17 //          class traits,
18 //          class Allocator = allocator<charT>
19 //          >
20 // basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())
21 //   -> basic_string<charT, traits, Allocator>;
22 //
23 //  The deduction guide shall not participate in overload resolution if Allocator
24 //  is a type that does not qualify as an allocator.
25 
26 #include <string>
27 #include <string_view>
28 #include <iterator>
29 #include <memory>
30 #include <type_traits>
31 #include <cassert>
32 #include <cstddef>
33 
34 #include "test_macros.h"
35 #include "test_allocator.h"
36 #include "../cpp17_input_iterator.h"
37 #include "min_allocator.h"
38 
39 bool test() {
40   {
41     std::string_view sv = "12345678901234";
42     std::basic_string s1(sv);
43     using S = decltype(s1); // what type did we get?
44     static_assert(std::is_same_v<S::value_type,                      char>,  "");
45     static_assert(std::is_same_v<S::traits_type,    std::char_traits<char>>, "");
46     static_assert(std::is_same_v<S::allocator_type,   std::allocator<char>>, "");
47     assert(s1.size() == sv.size());
48     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
49   }
50 
51   {
52     std::string_view sv = "12345678901234";
53     std::basic_string s1{sv, std::allocator<char>{}};
54     using S = decltype(s1); // what type did we get?
55     static_assert(std::is_same_v<S::value_type,                      char>,  "");
56     static_assert(std::is_same_v<S::traits_type,    std::char_traits<char>>, "");
57     static_assert(std::is_same_v<S::allocator_type,   std::allocator<char>>, "");
58     assert(s1.size() == sv.size());
59     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
60   }
61 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
62   {
63     std::wstring_view sv = L"12345678901234";
64     std::basic_string s1{sv, test_allocator<wchar_t>{}};
65     using S = decltype(s1); // what type did we get?
66     static_assert(std::is_same_v<S::value_type,                      wchar_t>,  "");
67     static_assert(std::is_same_v<S::traits_type,    std::char_traits<wchar_t>>, "");
68     static_assert(std::is_same_v<S::allocator_type,   test_allocator<wchar_t>>, "");
69     assert(s1.size() == sv.size());
70     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
71   }
72 #endif
73 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
74   {
75     std::u8string_view sv = u8"12345678901234";
76     std::basic_string s1{sv, min_allocator<char8_t>{}};
77     using S = decltype(s1); // what type did we get?
78     static_assert(std::is_same_v<S::value_type,                      char8_t>,  "");
79     static_assert(std::is_same_v<S::traits_type,    std::char_traits<char8_t>>, "");
80     static_assert(std::is_same_v<S::allocator_type,    min_allocator<char8_t>>, "");
81     assert(s1.size() == sv.size());
82     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
83   }
84 #endif
85   {
86     std::u16string_view sv = u"12345678901234";
87     std::basic_string s1{sv, min_allocator<char16_t>{}};
88     using S = decltype(s1); // what type did we get?
89     static_assert(std::is_same_v<S::value_type,                      char16_t>,  "");
90     static_assert(std::is_same_v<S::traits_type,    std::char_traits<char16_t>>, "");
91     static_assert(std::is_same_v<S::allocator_type,    min_allocator<char16_t>>, "");
92     assert(s1.size() == sv.size());
93     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
94   }
95   {
96     std::u32string_view sv = U"12345678901234";
97     std::basic_string s1{sv, explicit_allocator<char32_t>{}};
98     using S = decltype(s1); // what type did we get?
99     static_assert(std::is_same_v<S::value_type,                        char32_t>,  "");
100     static_assert(std::is_same_v<S::traits_type,      std::char_traits<char32_t>>, "");
101     static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, "");
102     assert(s1.size() == sv.size());
103     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
104   }
105 
106   return true;
107 }
108 
109 int main(int, char**)
110 {
111   test();
112 #if TEST_STD_VER > 17
113   // static_assert(test());
114 #endif
115 
116   return 0;
117 }
118