[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,, and can be used[.](#general-1.sentence-2) #### [23.6.6.2](#defn) Definition [[stack.defn]](stack.defn) namespace std {template>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 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]") R>constexpr stack(from_range_t, R&& rg); template constexpr explicit stack(const Alloc&); template constexpr stack(const Container&, const Alloc&); template constexpr stack(Container&&, const Alloc&); template constexpr stack(const stack&, const Alloc&); template constexpr stack(stack&&, const Alloc&); templateconstexpr 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]") 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]") R>constexpr void push_range(R&& rg); templateconstexpr decltype(auto) emplace(Args&&... args){ return c.emplace_back(std::forward(args)...); }constexpr void pop() { c.pop_back(); }constexpr void swap(stack& s) noexcept(is_nothrow_swappable_v){ using std::swap; swap(c, s.c); }}; template stack(Container) -> stack; template stack(InputIterator, InputIterator) -> stack<*iter-value-type*>; template stack(from_range_t, R&&) -> stack>; template stack(Container, Allocator) -> stack; template stack(InputIterator, InputIterator, Allocator)-> stack<*iter-value-type*, deque<*iter-value-type*, Allocator>>; template stack(from_range_t, R&&, Allocator)-> stack, deque, Allocator>>; templatestruct uses_allocator, Alloc>: uses_allocator::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 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]") 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(std​::​forward(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 is false the constructors in this subclause shall not participate in overload resolution[.](#cons.alloc-1.sentence-1) [🔗](#lib:stack,constructor____) `template 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 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 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 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 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 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]") 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(std​::​forward(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]") 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(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 constexpr bool operator==(const stack& x, const stack& 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 constexpr bool operator!=(const stack& x, const stack& 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 constexpr bool operator< (const stack& x, const stack& 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 constexpr bool operator> (const stack& x, const stack& 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 constexpr bool operator<=(const stack& x, const stack& 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 constexpr bool operator>=(const stack& x, const stack& 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 constexpr compare_three_way_result_t operator<=>(const stack& x, const stack& 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 constexpr void swap(stack& x, stack& y) noexcept(noexcept(x.swap(y))); ` [1](#special-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L16893) *Constraints*: is_swappable_v 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)