xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/string_view_deduction.pass.cpp (revision 76b26852b6be6e54c86741c7c80ca6b5d74eab2e)
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <string>
11 // UNSUPPORTED: c++98, c++03, c++11, c++14
12 // XFAIL: libcpp-no-deduction-guides
13 
14 // template<class InputIterator>
15 //   basic_string(InputIterator begin, InputIterator end,
16 //   const Allocator& a = Allocator());
17 
18 // template<class charT,
19 //          class traits,
20 //          class Allocator = allocator<charT>
21 //          >
22 // basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())
23 //   -> basic_string<charT, traits, Allocator>;
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 
29 #include <string>
30 #include <string_view>
31 #include <iterator>
32 #include <memory>
33 #include <type_traits>
34 #include <cassert>
35 #include <cstddef>
36 
37 #include "test_macros.h"
38 #include "test_allocator.h"
39 #include "../input_iterator.h"
40 #include "min_allocator.h"
41 
42 int main()
43 {
44     {
45     std::string_view sv = "12345678901234";
46     std::basic_string s1(sv);
47     using S = decltype(s1); // what type did we get?
48     static_assert(std::is_same_v<S::value_type,                      char>,  "");
49     static_assert(std::is_same_v<S::traits_type,    std::char_traits<char>>, "");
50     static_assert(std::is_same_v<S::allocator_type,   std::allocator<char>>, "");
51     assert(s1.size() == sv.size());
52     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
53     }
54 
55     {
56     std::string_view sv = "12345678901234";
57     std::basic_string s1{sv, std::allocator<char>{}};
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() == sv.size());
63     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
64     }
65     {
66     std::wstring_view sv = L"12345678901234";
67     std::basic_string s1{sv, test_allocator<wchar_t>{}};
68     using S = decltype(s1); // what type did we get?
69     static_assert(std::is_same_v<S::value_type,                      wchar_t>,  "");
70     static_assert(std::is_same_v<S::traits_type,    std::char_traits<wchar_t>>, "");
71     static_assert(std::is_same_v<S::allocator_type,   test_allocator<wchar_t>>, "");
72     assert(s1.size() == sv.size());
73     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
74     }
75     {
76     std::u16string_view sv = u"12345678901234";
77     std::basic_string s1{sv, min_allocator<char16_t>{}};
78     using S = decltype(s1); // what type did we get?
79     static_assert(std::is_same_v<S::value_type,                      char16_t>,  "");
80     static_assert(std::is_same_v<S::traits_type,    std::char_traits<char16_t>>, "");
81     static_assert(std::is_same_v<S::allocator_type,    min_allocator<char16_t>>, "");
82     assert(s1.size() == sv.size());
83     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
84     }
85     {
86     std::u32string_view sv = U"12345678901234";
87     std::basic_string s1{sv, explicit_allocator<char32_t>{}};
88     using S = decltype(s1); // what type did we get?
89     static_assert(std::is_same_v<S::value_type,                        char32_t>,  "");
90     static_assert(std::is_same_v<S::traits_type,      std::char_traits<char32_t>>, "");
91     static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, "");
92     assert(s1.size() == sv.size());
93     assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
94     }
95 }
96