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