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

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

Definition

In C++, pop() removes the oldest element from the front of a std::queue. This is the dequeue step: the person at the front of the line leaves.

pop() removes but does not return the value. That is why the usual pattern has two steps: read with front() first, then call pop() to throw the element away.

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

Syntax

Call pop() on the queue. It takes no arguments.

C++
std::queue<int> q;
q.push(5);
q.push(6);

int oldest = q.front();   // read first: 5
q.pop();                  // then remove it

// front() is now 6
CallWhat it does
q.pop()Removes the front element. The next oldest element becomes the new front. Nothing is returned.

Each pop() takes constant time. Remember the two-step rule: pop() does not return the removed value, so call front() before pop() if you still need it. This design keeps the function safe when copying the value might fail.

Library declarations
C++
void pop();

// queue<T, Container> from <queue>
// It calls c.pop_front() on the underlying container.
// The return type is void: the removed value is not given back.

Parameters

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

Return Value

Return ValueDescription
Nothingpop() returns void. The queue shrinks by one, and the old front value is gone.
Undefined behaviorIf the queue is empty there is nothing to remove. Check empty() first.

C++ Examples

Example 1 — Remove the Oldest Element

Push three numbers, then pop once. The next number moves up to the front.

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

int main() {
    std::queue<int> q;
    q.push(5);
    q.push(6);
    q.push(7);

    std::cout << "Front before: " << q.front() << '\n';
    q.pop();
    std::cout << "Front after: " << q.front() << '\n';
    std::cout << "Size: " << q.size() << '\n';
    return 0;
}

How It Works

  1. The line starts as 5, 6, 7, and the oldest number 5 is at the front.
  2. pop() removes 5 from the front. The element 6 was next in line, so it becomes the new front().
  3. The size drops from 3 to 2. The back element 7 is untouched, because pop() only works on the front.

Example 2 — pop() Returns Nothing

Prove that pop() has a void result, then use the correct two-step pattern.

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

int main() {
    std::queue<int> q;
    q.push(5);
    q.push(6);
    q.push(7);

    std::cout << std::boolalpha;
    std::cout << "pop() returns void: "
              << std::is_void_v<decltype(q.pop())> << '\n';

    int got = q.front();
    q.pop();
    std::cout << "Got " << got << ", left " << q.size() << '\n';
    return 0;
}

How It Works

  1. decltype(q.pop()) is the type that a call to pop() would produce, without calling it. std::is_void_v checks that this type is void, so the output is true.
  2. Because nothing comes back, the program copies q.front() into the variable got before it removes the element.
  3. The second step, q.pop(), deletes the front value 5. The copy in got is safe, and two numbers are left in the queue.

Example 3 — Pop Only When the Queue Has Elements

Run more pops than there are elements, and use empty() to skip the pops that would be undefined behavior.

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

int main() {
    std::queue<int> q;
    q.push(1);
    q.push(2);

    for (int round = 1; round <= 4; ++round) {
        if (q.empty()) {
            std::cout << "Queue is empty, skip pop (round " << round << ")\n";
        } else {
            std::cout << "Popped " << q.front() << '\n';
            q.pop();
        }
    }
    return 0;
}

How It Works

  1. The loop tries four pops, but the queue only holds two numbers. The check q.empty() runs at the start of every round.
  2. In rounds 1 and 2 the queue is not empty. The program prints front() and then calls pop(), so 1 and 2 are removed in arrival order.
  3. In rounds 3 and 4 empty() is true, so the else branch is skipped. Without this guard, the extra pop() calls would be undefined behavior.

Frequently Asked Questions

It removes the front element, the oldest one in the queue. The next oldest element becomes the new front. It does not return the removed value.
No. pop() returns void. Read the value with front() first, store it in a variable, and then call pop().
It is undefined behavior. The program may crash or corrupt data. Always check that !q.empty() before calling pop().
No. A queue only removes from the front. To remove from both ends, use std::deque with pop_front() and pop_back().
queue::pop() removes the oldest element (first in, first out). priority_queue::pop() removes the highest-priority element, no matter when it was added.

Next: push()

Add one element at the back of the queue with std::queue.

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

11 people found this page helpful