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

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

Definition

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

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

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

Syntax

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

C++
std::queue<int> q;

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

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

empty() is fast and runs in constant time. It is the same as asking q.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

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

Parameters

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

Return Value

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

C++ Examples

Example 1 — Check an Empty Queue

A new queue is empty. After one push() it is not, and after pop() it is empty again. Use std::boolalpha to print true and false.

C++
#include <iostream>
#include <queue>

int main() {
    std::queue<int> q;

    std::cout << std::boolalpha;
    std::cout << "At start: " << q.empty() << '\n';

    q.push(5);
    std::cout << "After push: " << q.empty() << '\n';

    q.pop();
    std::cout << "After pop: " << q.empty() << '\n';
    return 0;
}

How It Works

  1. A queue that was just created holds nothing, so empty() returns true. std::boolalpha prints the word instead of 1.
  2. push(5) adds one element. Now the queue is not empty, so empty() returns false.
  3. pop() removes that only element. The queue is empty again, so the answer goes back to true.

Example 2 — Serve Everyone with while (!q.empty())

Because a queue has no iterators, the standard way to visit all elements is a loop that stops when the queue becomes empty.

C++
#include <iostream>
#include <queue>

int main() {
    std::queue<int> q;
    q.push(4);
    q.push(8);
    q.push(15);

    int total = 0;
    while (!q.empty()) {
        std::cout << "Serving " << q.front() << '\n';
        total += q.front();
        q.pop();
    }

    std::cout << "Total: " << total << '\n';
    std::cout << "Empty now: " << std::boolalpha << q.empty() << '\n';
    return 0;
}

How It Works

  1. The condition !q.empty() is true while at least one number is waiting, so the body runs once for each of the three numbers.
  2. Each round reads front() to print and add, then pop() removes it. The numbers are served in the order they were pushed: 4, 8, 15.
  3. After the third pop() the queue has nothing left, the loop stops, and empty() reports true. The total is 4 + 8 + 15 = 27.

Example 3 — A Safe tryServe() Helper (Strings)

Wrap the check in a small function. It returns false when there is nothing to serve, so the caller never touches an empty queue.

C++
#include <iostream>
#include <queue>
#include <string>

bool tryServe(std::queue<std::string>& q, std::string& out) {
    if (q.empty()) {
        return false;
    }
    out = q.front();
    q.pop();
    return true;
}

int main() {
    std::queue<std::string> line;
    line.push("Mia");
    line.push("Noah");

    std::string who;
    for (int i = 0; i < 3; ++i) {
        if (tryServe(line, who)) {
            std::cout << "Served " << who << '\n';
        } else {
            std::cout << "Line is empty.\n";
        }
    }
    return 0;
}

How It Works

  1. tryServe() takes the queue by reference, so it changes the real queue and not a copy. The first thing it does is test q.empty().
  2. When the queue has a name, the function copies front() into out, calls pop(), and returns true. The first two rounds serve Mia and Noah.
  3. On the third round empty() is true, so the function returns false before it reads anything. The program prints a message instead of crashing.

Frequently Asked Questions

It returns true if the queue has no elements and false if it has at least one. It never changes the queue.
Yes, the result is the same. empty() is the clearer way to write it, and it is always constant time for the standard containers.
No. empty() only asks a question. A std::queue has no clear() function. To remove everything, pop() in a loop or swap with an empty queue.
Calling front(), back(), or pop() on an empty queue is undefined behavior. The program may crash or print garbage, so test !q.empty() first.
A queue has no iterators, so use while (!q.empty()) { use q.front(); q.pop(); }. This visits every element once, in first-in, first-out order.

Next: front()

Read the oldest element, the next one that will leave the queue with std::queue.

front() 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.

9 people found this page helpful