[stack]

# 23 Containers library [[containers]](./#containers)

## 23.6 Container adaptors [[container.adaptors]](container.adaptors#stack)

### 23.6.6 Class template stack [stack]

#### [23.6.6.1](#general) General [[stack.general]](stack.general)

[1](#general-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16546)

Any sequence container supporting operationsback(),push_back() andpop_back() can be used to instantiatestack[.](#general-1.sentence-1)

In particular,<vector>,<list> and<deque> can be used[.](#general-1.sentence-2)

#### [23.6.6.2](#defn) Definition [[stack.defn]](stack.defn)

namespace std {template<class T, class Container = deque<T>>class stack {public:using value_type = typename Container::value_type; using reference = typename Container::reference; using const_reference = typename Container::const_reference; using size_type = typename Container::size_type; using container_type = Container; protected: Container c; public:constexpr stack() : stack(Container()) {}constexpr explicit stack(const Container&); constexpr explicit stack(Container&&); template<class InputIterator> constexpr stack(InputIterator first, InputIterator last); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]")<T> R>constexpr stack(from_range_t, R&& rg); template<class Alloc> constexpr explicit stack(const Alloc&); template<class Alloc> constexpr stack(const Container&, const Alloc&); template<class Alloc> constexpr stack(Container&&, const Alloc&); template<class Alloc> constexpr stack(const stack&, const Alloc&); template<class Alloc> constexpr stack(stack&&, const Alloc&); template<class InputIterator, class Alloc>constexpr stack(InputIterator first, InputIterator last, const Alloc&); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]")<T> R, class Alloc>constexpr stack(from_range_t, R&& rg, const Alloc&); constexpr bool empty() const { return c.empty(); }constexpr size_type size() const { return c.size(); }constexpr reference top() { return c.back(); }constexpr const_reference top() const { return c.back(); }constexpr void push(const value_type& x) { c.push_back(x); }constexpr void push(value_type&& x) { c.push_back(std::move(x)); }template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]")<T> R>constexpr void push_range(R&& rg); template<class... Args>constexpr decltype(auto) emplace(Args&&... args){ return c.emplace_back(std::forward<Args>(args)...); }constexpr void pop() { c.pop_back(); }constexpr void swap(stack& s) noexcept(is_nothrow_swappable_v<Container>){ using std::swap; swap(c, s.c); }}; template<class Container> stack(Container) -> stack<typename Container::value_type, Container>; template<class InputIterator> stack(InputIterator, InputIterator) -> stack<*iter-value-type*<InputIterator>>; template<ranges::[input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]") R> stack(from_range_t, R&&) -> stack<ranges::range_value_t<R>>; template<class Container, class Allocator> stack(Container, Allocator) -> stack<typename Container::value_type, Container>; template<class InputIterator, class Allocator> stack(InputIterator, InputIterator, Allocator)-> stack<*iter-value-type*<InputIterator>, deque<*iter-value-type*<InputIterator>,
 Allocator>>; template<ranges::[input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]") R, class Allocator> stack(from_range_t, R&&, Allocator)-> stack<ranges::range_value_t<R>, deque<ranges::range_value_t<R>, Allocator>>; template<class T, class Container, class Alloc>struct uses_allocator<stack<T, Container>, Alloc>: uses_allocator<Container, Alloc>::type { };}

#### [23.6.6.3](#cons) Constructors [[stack.cons]](stack.cons)

[ð](#lib:stack,constructor)

`constexpr explicit stack(const Container& cont);
`

[1](#cons-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16646)

*Effects*: Initializes c with cont[.](#cons-1.sentence-1)

[ð](#lib:stack,constructor_)

`constexpr explicit stack(Container&& cont);
`

[2](#cons-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16657)

*Effects*: Initializes c with std​::​move(cont)[.](#cons-2.sentence-1)

[ð](#lib:stack,constructor__)

`template<class InputIterator>
  constexpr stack(InputIterator first, InputIterator last);
`

[3](#cons-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16669)

*Effects*: Initializes c withfirst as the first argument and last as the second argument[.](#cons-3.sentence-1)

[ð](#lib:stack,constructor___)

`template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]")<T> R>
  constexpr stack(from_range_t, R&& rg);
`

[4](#cons-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16682)

*Effects*: Initializes c with ranges​::​to<Container>(std​::​forward<R>(rg))[.](#cons-4.sentence-1)

#### [23.6.6.4](#cons.alloc) Constructors with allocators [[stack.cons.alloc]](stack.cons.alloc)

[1](#cons.alloc-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16689)

If uses_allocator_v<container_type, Alloc> is false the constructors in this subclause shall not participate in overload resolution[.](#cons.alloc-1.sentence-1)

[ð](#lib:stack,constructor____)

`template<class Alloc> constexpr explicit stack(const Alloc& a);
`

[2](#cons.alloc-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16699)

*Effects*: Initializes c with a[.](#cons.alloc-2.sentence-1)

[ð](#lib:stack,constructor_____)

`template<class Alloc> constexpr stack(const container_type& cont, const Alloc& a);
`

[3](#cons.alloc-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16710)

*Effects*: Initializes c with cont as the first argument and a as the
second argument[.](#cons.alloc-3.sentence-1)

[ð](#lib:stack,constructor______)

`template<class Alloc> constexpr stack(container_type&& cont, const Alloc& a);
`

[4](#cons.alloc-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16722)

*Effects*: Initializes c with std​::​move(cont) as the first argument and a as the second argument[.](#cons.alloc-4.sentence-1)

[ð](#lib:stack,constructor_______)

`template<class Alloc> constexpr stack(const stack& s, const Alloc& a);
`

[5](#cons.alloc-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16734)

*Effects*: Initializes c with s.c as the first argument and a as the second argument[.](#cons.alloc-5.sentence-1)

[ð](#lib:stack,constructor________)

`template<class Alloc> constexpr stack(stack&& s, const Alloc& a);
`

[6](#cons.alloc-6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16746)

*Effects*: Initializes c with std​::​move(s.c) as the first argument and a as the second argument[.](#cons.alloc-6.sentence-1)

[ð](#lib:stack,constructor_________)

`template<class InputIterator, class Alloc>
  constexpr stack(InputIterator first, InputIterator last, const Alloc& alloc);
`

[7](#cons.alloc-7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16759)

*Effects*: Initializes c withfirst as the first argument,last as the second argument, andalloc as the third argument[.](#cons.alloc-7.sentence-1)

[ð](#lib:stack,constructor__________)

`template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]")<T> R, class Alloc>
  constexpr stack(from_range_t, R&& rg, const Alloc& a);
`

[8](#cons.alloc-8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16774)

*Effects*: Initializesc with ranges​::​to<Container>(std​::​forward<R>(rg), a)[.](#cons.alloc-8.sentence-1)

#### [23.6.6.5](#mod) Modifiers [[stack.mod]](stack.mod)

[ð](#lib:push_range,stack)

`template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]")<T> R>
  constexpr void push_range(R&& rg);
`

[1](#mod-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16789)

*Effects*: Equivalent to c.append_range(std​::​forward<R>(rg)) if that is a valid expression,
otherwise ranges​::​copy(rg, back_inserter(c))[.](#mod-1.sentence-1)

#### [23.6.6.6](#ops) Operators [[stack.ops]](stack.ops)

[ð](#lib:operator==,stack)

`template<class T, class Container>
  constexpr bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
`

[1](#ops-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16805)

*Returns*: x.c == y.c[.](#ops-1.sentence-1)

[ð](#lib:operator!=,stack)

`template<class T, class Container>
  constexpr bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
`

[2](#ops-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16817)

*Returns*: x.c != y.c[.](#ops-2.sentence-1)

[ð](#lib:operator%3c,stack)

`template<class T, class Container>
  constexpr bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
`

[3](#ops-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16829)

*Returns*: x.c < y.c[.](#ops-3.sentence-1)

[ð](#lib:operator%3e,stack)

`template<class T, class Container>
  constexpr bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
`

[4](#ops-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16841)

*Returns*: x.c > y.c[.](#ops-4.sentence-1)

[ð](#lib:operator%3c=,stack)

`template<class T, class Container>
  constexpr bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
`

[5](#ops-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16853)

*Returns*: x.c <= y.c[.](#ops-5.sentence-1)

[ð](#lib:operator%3e=,stack)

`template<class T, class Container>
  constexpr bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
`

[6](#ops-6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16865)

*Returns*: x.c >= y.c[.](#ops-6.sentence-1)

[ð](#lib:operator%3c=%3e,stack)

`template<class T, [three_way_comparable](cmp.concept#concept:three_way_comparable "17.12.4 Concept three_­way_­comparable [cmp.concept]") Container>
  constexpr compare_three_way_result_t<Container>
    operator<=>(const stack<T, Container>& x, const stack<T, Container>& y);
`

[7](#ops-7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16878)

*Returns*: x.c <=> y.c[.](#ops-7.sentence-1)

#### [23.6.6.7](#special) Specialized algorithms [[stack.special]](stack.special)

[ð](#lib:swap,stack)

`template<class T, class Container>
  constexpr void swap(stack<T, Container>& x, stack<T, Container>& y)
    noexcept(noexcept(x.swap(y)));
`

[1](#special-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16893)

*Constraints*: is_swappable_v<Container> is true[.](#special-1.sentence-1)

[2](#special-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16897)

*Effects*: As if by x.swap(y)[.](#special-2.sentence-1)
