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>
117 #include <__assert> // all public C++ headers provide the assertion handler
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
141 _LIBCPP_BEGIN_NAMESPACE_STD
143 template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
145 template <class _Tp, class _Container>
146 _LIBCPP_INLINE_VISIBILITY
148 operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
150 template <class _Tp, class _Container>
151 _LIBCPP_INLINE_VISIBILITY
153 operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y);
155 template <class _Tp, class _Container /*= deque<_Tp>*/>
156 class _LIBCPP_TEMPLATE_VIS stack
159 typedef _Container container_type;
160 typedef typename container_type::value_type value_type;
161 typedef typename container_type::reference reference;
162 typedef typename container_type::const_reference const_reference;
163 typedef typename container_type::size_type size_type;
164 static_assert((is_same<_Tp, value_type>::value), "" );
170 _LIBCPP_INLINE_VISIBILITY
172 _NOEXCEPT_(is_nothrow_default_constructible<container_type>::value)
175 _LIBCPP_INLINE_VISIBILITY
176 stack(const stack& __q) : c(__q.c) {}
178 _LIBCPP_INLINE_VISIBILITY
179 stack& operator=(const stack& __q) {c = __q.c; return *this;}
182 #ifndef _LIBCPP_CXX03_LANG
183 _LIBCPP_INLINE_VISIBILITY
185 _NOEXCEPT_(is_nothrow_move_constructible<container_type>::value)
186 : c(_VSTD::move(__q.c)) {}
188 _LIBCPP_INLINE_VISIBILITY
189 stack& operator=(stack&& __q)
190 _NOEXCEPT_(is_nothrow_move_assignable<container_type>::value)
191 {c = _VSTD::move(__q.c); return *this;}
193 _LIBCPP_INLINE_VISIBILITY
194 explicit stack(container_type&& __c) : c(_VSTD::move(__c)) {}
195 #endif // _LIBCPP_CXX03_LANG
197 _LIBCPP_INLINE_VISIBILITY
198 explicit stack(const container_type& __c) : c(__c) {}
200 template <class _Alloc>
201 _LIBCPP_INLINE_VISIBILITY
202 explicit stack(const _Alloc& __a,
203 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
205 template <class _Alloc>
206 _LIBCPP_INLINE_VISIBILITY
207 stack(const container_type& __c, const _Alloc& __a,
208 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
210 template <class _Alloc>
211 _LIBCPP_INLINE_VISIBILITY
212 stack(const stack& __s, const _Alloc& __a,
213 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
215 #ifndef _LIBCPP_CXX03_LANG
216 template <class _Alloc>
217 _LIBCPP_INLINE_VISIBILITY
218 stack(container_type&& __c, const _Alloc& __a,
219 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
220 : c(_VSTD::move(__c), __a) {}
221 template <class _Alloc>
222 _LIBCPP_INLINE_VISIBILITY
223 stack(stack&& __s, const _Alloc& __a,
224 __enable_if_t<uses_allocator<container_type, _Alloc>::value>* = 0)
225 : c(_VSTD::move(__s.c), __a) {}
226 #endif // _LIBCPP_CXX03_LANG
228 #if _LIBCPP_STD_VER >= 23
229 template <class _InputIterator,
230 class = __enable_if_t<__has_input_iterator_category<_InputIterator>::value>>
231 _LIBCPP_HIDE_FROM_ABI
232 stack(_InputIterator __first, _InputIterator __last) : c(__first, __last) {}
234 template <_ContainerCompatibleRange<_Tp> _Range>
235 _LIBCPP_HIDE_FROM_ABI
236 stack(from_range_t, _Range&& __range) : c(from_range, std::forward<_Range>(__range)) {}
238 template <class _InputIterator,
240 class = __enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
241 class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>>
242 _LIBCPP_HIDE_FROM_ABI
243 stack(_InputIterator __first, _InputIterator __last, const _Alloc& __alloc) : c(__first, __last, __alloc) {}
245 template <_ContainerCompatibleRange<_Tp> _Range,
247 class = __enable_if_t<uses_allocator<container_type, _Alloc>::value>>
248 _LIBCPP_HIDE_FROM_ABI
249 stack(from_range_t, _Range&& __range, const _Alloc& __alloc)
250 : c(from_range, std::forward<_Range>(__range), __alloc) {}
254 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
255 bool empty() const {return c.empty();}
256 _LIBCPP_INLINE_VISIBILITY
257 size_type size() const {return c.size();}
258 _LIBCPP_INLINE_VISIBILITY
259 reference top() {return c.back();}
260 _LIBCPP_INLINE_VISIBILITY
261 const_reference top() const {return c.back();}
263 _LIBCPP_INLINE_VISIBILITY
264 void push(const value_type& __v) {c.push_back(__v);}
265 #ifndef _LIBCPP_CXX03_LANG
266 _LIBCPP_INLINE_VISIBILITY
267 void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
269 #if _LIBCPP_STD_VER >= 23
270 template <_ContainerCompatibleRange<_Tp> _Range>
271 _LIBCPP_HIDE_FROM_ABI
272 void push_range(_Range&& __range) {
273 if constexpr (requires (container_type& __c) {
274 __c.append_range(std::forward<_Range>(__range));
276 c.append_range(std::forward<_Range>(__range));
278 ranges::copy(std::forward<_Range>(__range), std::back_inserter(c));
283 template <class... _Args>
284 _LIBCPP_INLINE_VISIBILITY
285 #if _LIBCPP_STD_VER >= 17
286 decltype(auto) emplace(_Args&&... __args)
287 { return c.emplace_back(_VSTD::forward<_Args>(__args)...);}
289 void emplace(_Args&&... __args)
290 { c.emplace_back(_VSTD::forward<_Args>(__args)...);}
292 #endif // _LIBCPP_CXX03_LANG
294 _LIBCPP_INLINE_VISIBILITY
295 void pop() {c.pop_back();}
297 _LIBCPP_INLINE_VISIBILITY
298 void swap(stack& __s)
299 _NOEXCEPT_(__is_nothrow_swappable<container_type>::value)
305 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI const _Container& __get_container() const { return c; }
307 template <class _T1, class _OtherContainer>
310 operator==(const stack<_T1, _OtherContainer>& __x, const stack<_T1, _OtherContainer>& __y);
312 template <class _T1, class _OtherContainer>
315 operator< (const stack<_T1, _OtherContainer>& __x, const stack<_T1, _OtherContainer>& __y);
318 #if _LIBCPP_STD_VER >= 17
319 template<class _Container,
320 class = enable_if_t<!__is_allocator<_Container>::value>
323 -> stack<typename _Container::value_type, _Container>;
325 template<class _Container,
327 class = enable_if_t<!__is_allocator<_Container>::value>,
328 class = enable_if_t<uses_allocator<_Container, _Alloc>::value>
330 stack(_Container, _Alloc)
331 -> stack<typename _Container::value_type, _Container>;
334 #if _LIBCPP_STD_VER >= 23
335 template<class _InputIterator,
336 class = __enable_if_t<__has_input_iterator_category<_InputIterator>::value>>
337 stack(_InputIterator, _InputIterator)
338 -> stack<__iter_value_type<_InputIterator>>;
340 template <ranges::input_range _Range>
341 stack(from_range_t, _Range&&) -> stack<ranges::range_value_t<_Range>>;
343 template<class _InputIterator,
345 class = __enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
346 class = __enable_if_t<__is_allocator<_Alloc>::value>>
347 stack(_InputIterator, _InputIterator, _Alloc)
348 -> stack<__iter_value_type<_InputIterator>, deque<__iter_value_type<_InputIterator>, _Alloc>>;
350 template <ranges::input_range _Range,
352 class = __enable_if_t<__is_allocator<_Alloc>::value>>
353 stack(from_range_t, _Range&&, _Alloc)
354 -> stack<ranges::range_value_t<_Range>, deque<ranges::range_value_t<_Range>, _Alloc>>;
358 template <class _Tp, class _Container>
359 inline _LIBCPP_INLINE_VISIBILITY
361 operator==(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
363 return __x.c == __y.c;
366 template <class _Tp, class _Container>
367 inline _LIBCPP_INLINE_VISIBILITY
369 operator< (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
371 return __x.c < __y.c;
374 template <class _Tp, class _Container>
375 inline _LIBCPP_INLINE_VISIBILITY
377 operator!=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
379 return !(__x == __y);
382 template <class _Tp, class _Container>
383 inline _LIBCPP_INLINE_VISIBILITY
385 operator> (const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
390 template <class _Tp, class _Container>
391 inline _LIBCPP_INLINE_VISIBILITY
393 operator>=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
398 template <class _Tp, class _Container>
399 inline _LIBCPP_INLINE_VISIBILITY
401 operator<=(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y)
406 #if _LIBCPP_STD_VER >= 20
408 template <class _Tp, three_way_comparable _Container>
409 _LIBCPP_HIDE_FROM_ABI compare_three_way_result_t<_Container>
410 operator<=>(const stack<_Tp, _Container>& __x, const stack<_Tp, _Container>& __y) {
411 // clang 16 bug: declaring `friend operator<=>` causes "use of overloaded operator '*' is ambiguous" errors
412 return __x.__get_container() <=> __y.__get_container();
417 template <class _Tp, class _Container, __enable_if_t<__is_swappable<_Container>::value, int> = 0>
418 inline _LIBCPP_INLINE_VISIBILITY
420 swap(stack<_Tp, _Container>& __x, stack<_Tp, _Container>& __y)
421 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
426 template <class _Tp, class _Container, class _Alloc>
427 struct _LIBCPP_TEMPLATE_VIS uses_allocator<stack<_Tp, _Container>, _Alloc>
428 : public uses_allocator<_Container, _Alloc>
432 _LIBCPP_END_NAMESPACE_STD
434 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
436 # include <functional>
437 # include <type_traits>
440 #endif // _LIBCPP_STACK