xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/iter_alloc_deduction.pass.cpp (revision baf6f91851edcdd72e3a6214299f295ec58337f6)
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 
33 class NotAnIterator {};
34 using NotAnInputIterator = std::back_insert_iterator<std::basic_string<char16_t>>;
35 
36 template <typename T>
37 struct NotAnAllocator { typedef T value_type; };
38 
39 template <class Iter, class Alloc, class = void>
40 struct CanDeduce : std::false_type { };
41 
42 template <class Iter, class Alloc>
43 struct CanDeduce<Iter, Alloc, decltype((void)
44   std::basic_string{std::declval<Iter>(), std::declval<Iter>(), std::declval<Alloc>()}
45 )> : std::true_type { };
46 
47 static_assert( CanDeduce<char*, std::allocator<char>>::value);
48 static_assert(!CanDeduce<NotAnIterator, std::allocator<char>>::value);
49 static_assert(!CanDeduce<NotAnInputIterator, std::allocator<char16_t>>::value);
50 static_assert(!CanDeduce<char*, NotAnAllocator<char>>::value);
51 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
52 static_assert( CanDeduce<wchar_t*, std::allocator<wchar_t>>::value);
53 static_assert(!CanDeduce<wchar_t const*, NotAnAllocator<wchar_t>>::value);
54 #endif
55 
56 TEST_CONSTEXPR_CXX20 bool test() {
57   {
58     const char* s = "12345678901234";
59     std::basic_string s1(s, s+10);  // Can't use {} here
60     using S = decltype(s1); // what type did we get?
61     static_assert(std::is_same_v<S::value_type,                      char>,  "");
62     static_assert(std::is_same_v<S::traits_type,    std::char_traits<char>>, "");
63     static_assert(std::is_same_v<S::allocator_type,   std::allocator<char>>, "");
64     assert(s1.size() == 10);
65     assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
66   }
67   {
68     const char* s = "12345678901234";
69     std::basic_string s1{s, s+10, std::allocator<char>{}};
70     using S = decltype(s1); // what type did we get?
71     static_assert(std::is_same_v<S::value_type,                      char>,  "");
72     static_assert(std::is_same_v<S::traits_type,    std::char_traits<char>>, "");
73     static_assert(std::is_same_v<S::allocator_type,   std::allocator<char>>, "");
74     assert(s1.size() == 10);
75     assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
76   }
77 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
78   {
79     const wchar_t* s = L"12345678901234";
80     std::basic_string s1{s, s+10, test_allocator<wchar_t>{}};
81     using S = decltype(s1); // what type did we get?
82     static_assert(std::is_same_v<S::value_type,                      wchar_t>,  "");
83     static_assert(std::is_same_v<S::traits_type,    std::char_traits<wchar_t>>, "");
84     static_assert(std::is_same_v<S::allocator_type,   test_allocator<wchar_t>>, "");
85     assert(s1.size() == 10);
86     assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
87   }
88 #endif
89   {
90     const char16_t* s = u"12345678901234";
91     std::basic_string s1{s, s+10, min_allocator<char16_t>{}};
92     using S = decltype(s1); // what type did we get?
93     static_assert(std::is_same_v<S::value_type,                      char16_t>,  "");
94     static_assert(std::is_same_v<S::traits_type,    std::char_traits<char16_t>>, "");
95     static_assert(std::is_same_v<S::allocator_type,    min_allocator<char16_t>>, "");
96     assert(s1.size() == 10);
97     assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
98   }
99   {
100     const char32_t* s = U"12345678901234";
101     std::basic_string s1{s, s+10, explicit_allocator<char32_t>{}};
102     using S = decltype(s1); // what type did we get?
103     static_assert(std::is_same_v<S::value_type,                        char32_t>,  "");
104     static_assert(std::is_same_v<S::traits_type,      std::char_traits<char32_t>>, "");
105     static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, "");
106     assert(s1.size() == 10);
107     assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
108   }
109 
110   return true;
111 }
112 
113 int main(int, char**)
114 {
115   test();
116 #if TEST_STD_VER > 17
117   static_assert(test());
118 #endif
119 
120   return 0;
121 }
122