xref: /llvm-project/libcxx/test/std/containers/sequences/vector/vector.cons/deduct.pass.cpp (revision 17bbb224f99cf6fde83aa68e2e22e70fe9ed77be)
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 // <vector>
10 // UNSUPPORTED: c++03, c++11, c++14
11 
12 // template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
13 //    vector(InputIterator, InputIterator, Allocator = Allocator())
14 //    -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>;
15 //
16 // template<ranges::input_range R, class Allocator = allocator<ranges::range_value_t<R>>>
17 //   vector(from_range_t, R&&, Allocator = Allocator())
18 //     -> vector<ranges::range_value_t<R>, Allocator>; // C++23
19 
20 #include <algorithm>
21 #include <array>
22 #include <cassert>
23 #include <climits> // INT_MAX
24 #include <cstddef>
25 #include <iterator>
26 #include <type_traits>
27 #include <vector>
28 
29 #include "deduction_guides_sfinae_checks.h"
30 #include "test_macros.h"
31 #include "test_iterators.h"
32 #include "test_allocator.h"
33 
34 struct A {};
35 
tests()36 TEST_CONSTEXPR_CXX20 bool tests() {
37 
38 //  Test the explicit deduction guides
39     {
40     const int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
41     std::vector vec(std::begin(arr), std::end(arr));
42 
43     static_assert(std::is_same_v<decltype(vec), std::vector<int>>, "");
44     assert(std::equal(vec.begin(), vec.end(), std::begin(arr), std::end(arr)));
45     }
46 
47     {
48     const long arr[] = {INT_MAX, 1L, 2L, 3L };
49     std::vector vec(std::begin(arr), std::end(arr), std::allocator<long>());
50     static_assert(std::is_same_v<decltype(vec)::value_type, long>, "");
51     assert(vec.size() == 4);
52     assert(vec[0] == INT_MAX);
53     assert(vec[1] == 1L);
54     assert(vec[2] == 2L);
55     }
56 
57 //  Test the implicit deduction guides
58 
59     {
60 //  We don't expect this one to work.
61 //  std::vector vec(std::allocator<int>()); // vector (allocator &)
62     }
63 
64     {
65     std::vector vec(1, A{}); // vector (size_type, T)
66     static_assert(std::is_same_v<decltype(vec)::value_type, A>, "");
67     static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<A>>, "");
68     assert(vec.size() == 1);
69     }
70 
71     {
72     std::vector vec(1, A{}, test_allocator<A>()); // vector (size_type, T, allocator)
73     static_assert(std::is_same_v<decltype(vec)::value_type, A>, "");
74     static_assert(std::is_same_v<decltype(vec)::allocator_type, test_allocator<A>>, "");
75     assert(vec.size() == 1);
76     }
77 
78     {
79     std::vector vec{1U, 2U, 3U, 4U, 5U}; // vector(initializer-list)
80     static_assert(std::is_same_v<decltype(vec)::value_type, unsigned>, "");
81     assert(vec.size() == 5);
82     assert(vec[2] == 3U);
83     }
84 
85     {
86     std::vector vec({1.0, 2.0, 3.0, 4.0}, test_allocator<double>()); // vector(initializer-list, allocator)
87     static_assert(std::is_same_v<decltype(vec)::value_type, double>, "");
88     static_assert(std::is_same_v<decltype(vec)::allocator_type, test_allocator<double>>, "");
89     assert(vec.size() == 4);
90     assert(vec[3] == 4.0);
91     }
92 
93     {
94     std::vector<long double> source;
95     std::vector vec(source); // vector(vector &)
96     static_assert(std::is_same_v<decltype(vec)::value_type, long double>, "");
97     static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<long double>>, "");
98     assert(vec.size() == 0);
99     }
100 
101 #if TEST_STD_VER >= 23
102     {
103       {
104         std::vector c(std::from_range, std::array<int, 0>());
105         static_assert(std::is_same_v<decltype(c), std::vector<int>>);
106       }
107 
108       {
109         using Alloc = test_allocator<int>;
110         std::vector c(std::from_range, std::array<int, 0>(), Alloc());
111         static_assert(std::is_same_v<decltype(c), std::vector<int, Alloc>>);
112       }
113     }
114 #endif
115 
116 //  A couple of vector<bool> tests, too!
117     {
118     std::vector vec(3, true); // vector(initializer-list)
119     static_assert(std::is_same_v<decltype(vec)::value_type, bool>, "");
120     static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<bool>>, "");
121     assert(vec.size() == 3);
122     assert(vec[0] && vec[1] && vec[2]);
123     }
124 
125     {
126     std::vector<bool> source;
127     std::vector vec(source); // vector(vector &)
128     static_assert(std::is_same_v<decltype(vec)::value_type, bool>, "");
129     static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<bool>>, "");
130     assert(vec.size() == 0);
131     }
132 
133     {
134         typedef test_allocator<short> Alloc;
135         typedef test_allocator<int> ConvertibleToAlloc;
136 
137         {
138         std::vector<short, Alloc> source;
139         std::vector vec(source, Alloc(2));
140         static_assert(std::is_same_v<decltype(vec), decltype(source)>);
141         }
142 
143         {
144         std::vector<short, Alloc> source;
145         std::vector vec(source, ConvertibleToAlloc(2));
146         static_assert(std::is_same_v<decltype(vec), decltype(source)>);
147         }
148 
149         {
150         std::vector<short, Alloc> source;
151         std::vector vec(std::move(source), Alloc(2));
152         static_assert(std::is_same_v<decltype(vec), decltype(source)>);
153         }
154 
155         {
156         std::vector<short, Alloc> source;
157         std::vector vec(std::move(source), ConvertibleToAlloc(2));
158         static_assert(std::is_same_v<decltype(vec), decltype(source)>);
159         }
160     }
161 
162     SequenceContainerDeductionGuidesSfinaeAway<std::vector, std::vector<int>>();
163 
164     return true;
165 }
166 
main(int,char **)167 int main(int, char**) {
168     tests();
169 #if TEST_STD_VER > 17
170     static_assert(tests());
171 #endif
172     return 0;
173 }
174