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 | // <stack> |
10 | // UNSUPPORTED: c++03, c++11, c++14 |
11 | |
12 | // template<class Container> |
13 | // stack(Container) -> stack<typename Container::value_type, Container>; |
14 | // |
15 | // template<class Container, class Allocator> |
16 | // stack(Container, Allocator) -> stack<typename Container::value_type, Container>; |
17 | // |
18 | // template<ranges::input_range R> |
19 | // stack(from_range_t, R&&) -> stack<ranges::range_value_t<R>>; // since C++23 |
20 | // |
21 | // template<ranges::input_range R, class Allocator> |
22 | // stack(from_range_t, R&&, Allocator) |
23 | // -> stack<ranges::range_value_t<R>, deque<ranges::range_value_t<R>, Allocator>>; // since C++23 |
24 | |
25 | #include <array> |
26 | #include <stack> |
27 | #include <deque> |
28 | #include <vector> |
29 | #include <list> |
30 | #include <iterator> |
31 | #include <cassert> |
32 | #include <cstddef> |
33 | #include <climits> // INT_MAX |
34 | |
35 | #include "deduction_guides_sfinae_checks.h" |
36 | #include "test_macros.h" |
37 | #include "test_iterators.h" |
38 | #include "test_allocator.h" |
39 | |
40 | struct A {}; |
41 | |
42 | int main(int, char**) |
43 | { |
44 | |
45 | // Test the explicit deduction guides |
46 | { |
47 | std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
48 | std::stack stk(v); |
49 | |
50 | static_assert(std::is_same_v<decltype(stk), std::stack<int, std::vector<int>>>, "" ); |
51 | assert(stk.size() == v.size()); |
52 | assert(stk.top() == v.back()); |
53 | } |
54 | |
55 | { |
56 | std::list<long, test_allocator<long>> l{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; |
57 | std::stack stk(l, test_allocator<long>(0,2)); // different allocator |
58 | static_assert(std::is_same_v<decltype(stk)::container_type, std::list<long, test_allocator<long>>>, "" ); |
59 | static_assert(std::is_same_v<decltype(stk)::value_type, long>, "" ); |
60 | assert(stk.size() == 10); |
61 | assert(stk.top() == 19); |
62 | // I'd like to assert that we've gotten the right allocator in the stack, but |
63 | // I don't know how to get at the underlying container. |
64 | } |
65 | |
66 | // Test the implicit deduction guides |
67 | |
68 | { |
69 | // We don't expect this one to work - no way to implicitly get value_type |
70 | // std::stack stk(std::allocator<int>()); // stack (allocator &) |
71 | } |
72 | |
73 | { |
74 | std::stack<A> source; |
75 | std::stack stk(source); // stack(stack &) |
76 | static_assert(std::is_same_v<decltype(stk)::value_type, A>, "" ); |
77 | static_assert(std::is_same_v<decltype(stk)::container_type, std::deque<A>>, "" ); |
78 | assert(stk.size() == 0); |
79 | } |
80 | |
81 | { |
82 | typedef short T; |
83 | typedef test_allocator<T> Alloc; |
84 | typedef std::list<T, Alloc> Cont; |
85 | typedef test_allocator<int> ConvertibleToAlloc; |
86 | static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> && |
87 | !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>); |
88 | |
89 | { |
90 | Cont cont; |
91 | std::stack stk(cont, Alloc(2)); |
92 | static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); |
93 | } |
94 | |
95 | { |
96 | Cont cont; |
97 | std::stack stk(cont, ConvertibleToAlloc(2)); |
98 | static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); |
99 | } |
100 | |
101 | { |
102 | Cont cont; |
103 | std::stack stk(std::move(cont), Alloc(2)); |
104 | static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); |
105 | } |
106 | |
107 | { |
108 | Cont cont; |
109 | std::stack stk(std::move(cont), ConvertibleToAlloc(2)); |
110 | static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); |
111 | } |
112 | } |
113 | |
114 | { |
115 | typedef short T; |
116 | typedef test_allocator<T> Alloc; |
117 | typedef std::list<T, Alloc> Cont; |
118 | typedef test_allocator<int> ConvertibleToAlloc; |
119 | static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> && |
120 | !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>); |
121 | |
122 | { |
123 | std::stack<T, Cont> source; |
124 | std::stack stk(source, Alloc(2)); |
125 | static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); |
126 | } |
127 | |
128 | { |
129 | std::stack<T, Cont> source; |
130 | std::stack stk(source, ConvertibleToAlloc(2)); |
131 | static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); |
132 | } |
133 | |
134 | { |
135 | std::stack<T, Cont> source; |
136 | std::stack stk(std::move(source), Alloc(2)); |
137 | static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); |
138 | } |
139 | |
140 | { |
141 | std::stack<T, Cont> source; |
142 | std::stack stk(std::move(source), ConvertibleToAlloc(2)); |
143 | static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); |
144 | } |
145 | } |
146 | |
147 | #if TEST_STD_VER >= 23 |
148 | { |
149 | typedef short T; |
150 | typedef test_allocator<T> Alloc; |
151 | std::list<T> a; |
152 | { |
153 | std::stack s(a.begin(), a.end()); |
154 | static_assert(std::is_same_v<decltype(s), std::stack<T>>); |
155 | } |
156 | { |
157 | std::stack s(a.begin(), a.end(), Alloc()); |
158 | static_assert(std::is_same_v<decltype(s), std::stack<T, std::deque<T, Alloc>>>); |
159 | } |
160 | } |
161 | |
162 | { |
163 | { |
164 | std::stack c(std::from_range, std::array<int, 0>()); |
165 | static_assert(std::is_same_v<decltype(c), std::stack<int>>); |
166 | } |
167 | |
168 | { |
169 | using Alloc = test_allocator<int>; |
170 | std::stack c(std::from_range, std::array<int, 0>(), Alloc()); |
171 | static_assert(std::is_same_v<decltype(c), std::stack<int, std::deque<int, Alloc>>>); |
172 | } |
173 | } |
174 | #endif |
175 | |
176 | ContainerAdaptorDeductionGuidesSfinaeAway<std::stack, std::stack<int>>(); |
177 | |
178 | return 0; |
179 | } |
180 | |