2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
19 template <class T, class Container = deque<T>>
23 typedef Container container_type;
24 typedef typename container_type::value_type value_type;
25 typedef typename container_type::reference reference;
26 typedef typename container_type::const_reference const_reference;
27 typedef typename container_type::size_type size_type;
36 stack(const stack& q) = default;
37 stack(stack&& q) = default;
39 stack& operator=(const stack& q) = default;
40 stack& operator=(stack&& q) = default;
42 explicit stack(const container_type& c);
43 explicit stack(container_type&& c);
44 template <class InputIterator> stack(InputIterator first, InputIterator last); // since C++23
45 template<container-compatible-range<T> R> stack(from_range_t, R&& rg); // since C++23
46 template <class Alloc> explicit stack(const Alloc& a);
47 template <class Alloc> stack(const container_type& c, const Alloc& a);
48 template <class Alloc> stack(container_type&& c, const Alloc& a);
49 template <class Alloc> stack(const stack& c, const Alloc& a);
50 template <class Alloc> stack(stack&& c, const Alloc& a);
51 template<class InputIterator, class Alloc>
52 stack(InputIterator first, InputIterator last, const Alloc&); // since C++23
53 template<container-compatible-range<T> R, class Alloc>
54 stack(from_range_t, R&& rg, const Alloc&); // since C++23
57 size_type size() const;
59 const_reference top() const;
61 void push(const value_type& x);
62 void push(value_type&& x);
63 template<container-compatible-range<T> R>
64 void push_range(R&& rg); // C++23
65 template <class... Args> reference emplace(Args&&... args); // reference in C++17
68 void swap(stack& c) noexcept(is_nothrow_swappable_v<Container>)
71 template<class Container>
72 stack(Container) -> stack<typename Container::value_type, Container>; // C++17
74 template<class InputIterator>
75 stack(InputIterator, InputIterator) -> stack<iter-value-type<InputIterator>>; // since C++23
77 template<ranges::input_range R>
78 stack(from_range_t, R&&) -> stack<ranges::range_value_t<R>>; // since C++23
80 template<class Container, class Allocator>
81 stack(Container, Allocator) -> stack<typename Container::value_type, Container>; // C++17
83 template<class InputIterator, class Allocator>
84 stack(InputIterator, InputIterator, Allocator)
85 -> stack<iter-value-type<InputIterator>,
86 deque<iter-value-type<InputIterator>, Allocator>>; // since C++23
88 template<ranges::input_range R, class Allocator>
89 stack(from_range_t, R&&, Allocator)
90 -> stack<ranges::range_value_t<R>, deque<ranges::range_value_t<R>, Allocator>>; // since C++23
92 template <class T, class Container>
93 bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
94 template <class T, class Container>
95 bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
96 template <class T, class Container>
97 bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
98 template <class T, class Container>
99 bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
100 template <class T, class Container>
101 bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
102 template <class T, class Container>
103 bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
104 template<class T, three_way_comparable Container>
105 compare_three_way_result_t<Container>
106 operator<=>(const stack<T, Container>& x, const stack<T, Container>& y); // since C++20
108 template <class T, class Container>
109 void swap(stack<T, Container>& x, stack<T, Container>& y)
110 noexcept(noexcept(x.swap(y)));
116 #include <__algorithm/ranges_copy.h>
118 #include <__fwd/stack.h>
119 #include <__iterator/back_insert_iterator.h>
120 #include <__iterator/iterator_traits.h>
121 #include <__memory/uses_allocator.h>
122 #include <__ranges/access.h>
123 #include <__ranges/concepts.h>
124 #include <__ranges/container_compatible_range.h>
125 #include <__ranges/from_range.h>
126 #include <__type_traits/is_same.h>
127 #include <__utility/forward.h>
131 // standard-mandated includes
135 #include <initializer_list>
137 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
138 # pragma GCC system_header
142 #include <__undef_macros>
144 _LIBCPP_BEGIN_NAMESPACE_STD
146 template <class _Tp, class _Container>
147 _LIBCPP_HIDE_FROM_ABI bool operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
149 template <class _Tp, class _Container>
150 _LIBCPP_HIDE_FROM_ABI bool operator<(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
152 template <class _Tp, class _Container /*= deque<_Tp>*/>
153 class _LIBCPP_TEMPLATE_VIS stack {
155 typedef _Container container_type;
156 typedef typename container_type::value_type value_type;
157 typedef typename container_type::reference reference;
158 typedef typename container_type::const_reference const_reference;
159 typedef typename container_type::size_type size_type;
160 static_assert(is_same<_Tp, value_type>::value, "");
166 _LIBCPP_HIDE_FROM_ABI stack() _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value) : c() {}
168 _LIBCPP_HIDE_FROM_ABI stack(const stack& __q) : c(__q.c) {}
170 _LIBCPP_HIDE_FROM_ABI stack& operator=(const stack& __q) {
175 #ifndef _LIBCPP_CXX03_LANG
176 _LIBCPP_HIDE_FROM_ABI stack(stack&& __q) noexcept(is_nothrow_move_constructible<container_type>::value)
177 : c(std::move(__q.c)) {}
179 _LIBCPP_HIDE_FROM_ABI stack& operator=(stack&& __q) noexcept(is_nothrow_move_assignable<container_type>::value) {
180 c = std::move(__q.c);
184 _LIBCPP_HIDE_FROM_ABI explicit stack(container_type&& __c) : c(std::move(__c)) {}
185 #endif // _LIBCPP_CXX03_LANG
187 _LIBCPP_HIDE_FROM_ABI explicit stack(const container_type& __c) : c(__c) {}
189 template <class _Alloc>
190 _LIBCPP_HIDE_FROM_ABI explicit stack(const _Alloc& __a,
191 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
193 template <class _Alloc>
194 _LIBCPP_HIDE_FROM_ABI
195 stack(const container_type& __c, const _Alloc& __a, __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
197 template <class _Alloc>
198 _LIBCPP_HIDE_FROM_ABI
199 stack(const stack& __s, const _Alloc& __a, __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
201 #ifndef _LIBCPP_CXX03_LANG
202 template <class _Alloc>
203 _LIBCPP_HIDE_FROM_ABI
204 stack(container_type&& __c, const _Alloc& __a, __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
205 : c(std::move(__c), __a) {}
206 template <class _Alloc>
207 _LIBCPP_HIDE_FROM_ABI
208 stack(stack&& __s, const _Alloc& __a, __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
209 : c(std::move(__s.c), __a) {}
210 #endif // _LIBCPP_CXX03_LANG
212 #if _LIBCPP_STD_VER >= 23
213 template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>
214 _LIBCPP_HIDE_FROM_ABI stack(_InputIterator __first, _InputIterator __last) : c(__first, __last) {}
216 template <_ContainerCompatibleRange<_Tp> _Range>
217 _LIBCPP_HIDE_FROM_ABI stack(from_range_t, _Range&& __range) : c(from_range, std::forward<_Range>(__range)) {}
219 template <class _InputIterator,
221 __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0,
222 __enable_if_t<uses_allocator<container_type, _Alloc>::value, int> = 0>
223 _LIBCPP_HIDE_FROM_ABI stack(_InputIterator __first, _InputIterator __last, const _Alloc& __alloc)
224 : c(__first, __last, __alloc) {}
226 template <_ContainerCompatibleRange<_Tp> _Range,
228 __enable_if_t<uses_allocator<container_type, _Alloc>::value, int> = 0>
229 _LIBCPP_HIDE_FROM_ABI stack(from_range_t, _Range&& __range, const _Alloc& __alloc)
230 : c(from_range, std::forward<_Range>(__range), __alloc) {}
234 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool empty() const { return c.empty(); }
235 _LIBCPP_HIDE_FROM_ABI size_type size() const { return c.size(); }
236 _LIBCPP_HIDE_FROM_ABI reference top() { return c.back(); }
237 _LIBCPP_HIDE_FROM_ABI const_reference top() const { return c.back(); }
239 _LIBCPP_HIDE_FROM_ABI void push(const value_type& __v) { c.push_back(__v); }
240 #ifndef _LIBCPP_CXX03_LANG
241 _LIBCPP_HIDE_FROM_ABI void push(value_type&& __v) { c.push_back(std::move(__v)); }
243 # if _LIBCPP_STD_VER >= 23
244 template <_ContainerCompatibleRange<_Tp> _Range>
245 _LIBCPP_HIDE_FROM_ABI void push_range(_Range&& __range) {
246 if constexpr (requires(container_type& __c) { __c.append_range(std::forward<_Range>(__range)); }) {
247 c.append_range(std::forward<_Range>(__range));
249 ranges::copy(std::forward<_Range>(__range), std::back_inserter(c));
254 template <class... _Args>
255 _LIBCPP_HIDE_FROM_ABI
256 # if _LIBCPP_STD_VER >= 17
258 emplace(_Args&&... __args) {
259 return c.emplace_back(std::forward<_Args>(__args)...);
263 emplace(_Args&&... __args) {
264 c.emplace_back(std::forward<_Args>(__args)...);
267 #endif // _LIBCPP_CXX03_LANG
269 _LIBCPP_HIDE_FROM_ABI void pop() { c.pop_back(); }
271 _LIBCPP_HIDE_FROM_ABI void swap(stack& __s) _NOEXCEPT_(__is_nothrow_swappable_v<container_type>) {
276 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const _Container& __get_container() const { return c; }
278 template <class _T1, class _OtherContainer>
279 friend bool operator==(const stack<_T1, _OtherContainer>& __x, const stack<_T1, _OtherContainer>& __y);
281 template <class _T1, class _OtherContainer>
282 friend bool operator<(const stack<_T1, _OtherContainer>& __x, const stack<_T1, _OtherContainer>& __y);
285 #if _LIBCPP_STD_VER >= 17
286 template <class _Container, class = enable_if_t<!__is_allocator<_Container>::value> >
287 stack(_Container) -> stack<typename _Container::value_type, _Container>;
289 template <class _Container,
291 class = enable_if_t<!__is_allocator<_Container>::value>,
292 class = enable_if_t<uses_allocator<_Container, _Alloc>::value> >
293 stack(_Container, _Alloc) -> stack<typename _Container::value_type, _Container>;
296 #if _LIBCPP_STD_VER >= 23
297 template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>
298 stack(_InputIterator, _InputIterator) -> stack<__iter_value_type<_InputIterator>>;
300 template <ranges::input_range _Range>
301 stack(from_range_t, _Range&&) -> stack<ranges::range_value_t<_Range>>;
303 template <class _InputIterator,
305 __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0,
306 __enable_if_t<__is_allocator<_Alloc>::value, int> = 0>
307 stack(_InputIterator,
309 _Alloc) -> stack<__iter_value_type<_InputIterator>, deque<__iter_value_type<_InputIterator>, _Alloc>>;
311 template <ranges::input_range _Range, class _Alloc, __enable_if_t<__is_allocator<_Alloc>::value, int> = 0>
314 _Alloc) -> stack<ranges::range_value_t<_Range>, deque<ranges::range_value_t<_Range>, _Alloc>>;
318 template <class _Tp, class _Container>
319 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
320 return __x.c == __y.c;
323 template <class _Tp, class _Container>
324 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
325 return __x.c < __y.c;
328 template <class _Tp, class _Container>
329 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
330 return !(__x == __y);
333 template <class _Tp, class _Container>
334 inline _LIBCPP_HIDE_FROM_ABI bool operator>(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
338 template <class _Tp, class _Container>
339 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
343 template <class _Tp, class _Container>
344 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
348 #if _LIBCPP_STD_VER >= 20
350 template <class _Tp, three_way_comparable _Container>
351 _LIBCPP_HIDE_FROM_ABI compare_three_way_result_t<_Container>
352 operator<=>(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
353 // clang 16 bug: declaring `friend operator<=>` causes "use of overloaded operator '*' is ambiguous" errors
354 return __x.__get_container() <=> __y.__get_container();
359 template <class _Tp, class _Container, __enable_if_t<__is_swappable_v<_Container>, int> = 0>
360 inline _LIBCPP_HIDE_FROM_ABI void swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
361 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
365 template <class _Tp, class _Container, class _Alloc>
366 struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc> : public uses_allocator<_Container, _Alloc> {
369 _LIBCPP_END_NAMESPACE_STD
373 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
375 # include <functional>
376 # include <type_traits>
379 #endif // _LIBCPP_STACK