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