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

source code of libcxx/test/std/containers/container.adaptors/stack/stack.cons/deduct.pass.cpp