1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++03, c++11, c++14
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>;
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
23 #include <climits> // INT_MAX
26 #include <type_traits>
29 #include "deduction_guides_sfinae_checks.h"
30 #include "test_macros.h"
31 #include "test_iterators.h"
32 #include "test_allocator.h"
36 TEST_CONSTEXPR_CXX20
bool tests() {
38 // Test the explicit deduction guides
40 const int arr
[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
41 std::vector
vec(std::begin(arr
), std::end(arr
));
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
)));
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
);
57 // Test the implicit deduction guides
60 // We don't expect this one to work.
61 // std::vector vec(std::allocator<int>()); // vector (allocator &)
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);
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);
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);
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);
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);
101 #if TEST_STD_VER >= 23
104 std::vector
c(std::from_range
, std::array
<int, 0>());
105 static_assert(std::is_same_v
<decltype(c
), std::vector
<int>>);
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
>>);
116 // A couple of vector<bool> tests, too!
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]);
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);
134 typedef test_allocator
<short> Alloc
;
135 typedef test_allocator
<int> ConvertibleToAlloc
;
138 std::vector
<short, Alloc
> source
;
139 std::vector
vec(source
, Alloc(2));
140 static_assert(std::is_same_v
<decltype(vec
), decltype(source
)>);
144 std::vector
<short, Alloc
> source
;
145 std::vector
vec(source
, ConvertibleToAlloc(2));
146 static_assert(std::is_same_v
<decltype(vec
), decltype(source
)>);
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
)>);
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
)>);
162 SequenceContainerDeductionGuidesSfinaeAway
<std::vector
, std::vector
<int>>();
167 int main(int, char**) {
169 #if TEST_STD_VER > 17
170 static_assert(tests());