C++ std::stack::empty() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++98

Definition

In C++, empty() tells you whether a std::stack has no elements. It returns true for an empty stack and false when at least one element is waiting on top.

It is the safety check for the whole container. top() and pop() are undefined behavior on an empty stack, so you ask empty() first. A stack has no iterators, so while (!st.empty()) is also the normal way to walk through every element.

This function is a member function of std::stack<T>, declared in the <stack> header. It has been available since the first C++ standard, so every compiler supports it. You call it with a dot, for example st.empty(...).

Syntax

Call empty() on the stack. It takes no arguments and returns a bool.

C++
std::stack<int> st;

bool a = st.empty();   // true, nothing inside

st.push(7);
bool b = st.empty();   // false, one element inside
CallWhat it does
st.empty()Returns true if the stack has no elements, otherwise false. It never changes the stack.

empty() is fast and runs in constant time. It is the same as asking st.size() == 0, but it reads more clearly. Since C++20 the function is marked [[nodiscard]], so a compiler may warn if you call it and ignore the answer.

Library declarations
C++
[[nodiscard]] bool empty() const;   // [[nodiscard]] since C++20

// stack<T, Container> from <stack>
// empty() returns c.empty()

Parameters

ParameterDescription
Noneempty() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
trueThe stack has no elements.
falseThe stack has at least one element.

C++ Examples

Example 1 — Check an Empty Stack

A new stack is empty. After one push() it is not. Use std::boolalpha to print true and false.

C++
#include <iostream>
#include <stack>

int main() {
    std::stack<int> st;

    std::cout << std::boolalpha;
    std::cout << "Before push: " << st.empty() << '\n';

    st.push(42);
    std::cout << "After push:  " << st.empty() << '\n';
    return 0;
}

How It Works

  1. A freshly created stack holds nothing, so empty() returns true.
  2. std::boolalpha makes cout print the words true and false instead of 1 and 0.
  3. After push(42) the stack has one element, so empty() returns false.

Example 2 — Walk Through Every Element with while (!st.empty())

You cannot loop over a stack with iterators, so use top(), pop(), and empty() together.

C++
#include <iostream>
#include <stack>

int main() {
    std::stack<int> st;
    st.push(4);
    st.push(9);
    st.push(2);

    while (!st.empty()) {
        std::cout << st.top() << ' ';
        st.pop();
    }
    std::cout << '\n';

    std::cout << std::boolalpha << "Now empty: " << st.empty() << '\n';
    return 0;
}

How It Works

  1. The loop runs while empty() is false. Each round prints the current top(), then pop() removes it.
  2. The newest value always comes out first, so the numbers appear in reverse push order: 2, 9, 4.
  3. After the third pop() the stack has no elements, empty() becomes true, and the loop stops. The last line confirms this.

Example 3 — A Safe Helper Function

Wrap the check in a small function that returns a default value when the stack is empty.

C++
#include <iostream>
#include <stack>

int topOr(const std::stack<int>& st, int fallback) {
    if (st.empty()) {
        return fallback;
    }
    return st.top();
}

int main() {
    std::stack<int> none;
    std::stack<int> some;
    some.push(3);
    some.push(8);
    some.push(5);

    std::cout << "Empty stack: " << topOr(none, -1) << '\n';
    std::cout << "Filled stack: " << topOr(some, -1) << '\n';
    return 0;
}

How It Works

  1. topOr() receives the stack by const reference. empty() and the const overload of top() are both allowed.
  2. For the empty stack, empty() is true, so the function returns the fallback -1 and never touches top().
  3. For the filled stack, empty() is false, so top() is safe and returns the newest value 5.

Frequently Asked Questions

It returns true if the stack has no elements, and false if it has at least one element.
Calling top() or pop() on an empty stack is undefined behavior. A quick if (!st.empty()) check keeps your program safe.
A stack has no iterators. Use while (!st.empty()) { use st.top(); st.pop(); }. This also removes the elements one by one from the stack.
Yes. Both mean the stack holds no elements. empty() is the clearer way to write it and is the style most C++ programmers prefer.
There is no clear() function. Assign a new empty stack, such as st = std::stack<int>(); or swap with an empty one. Calling empty() afterwards returns true.

Next: pop()

Remove the top element. It does not return the value with std::stack.

pop() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful