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

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

Definition

In C++11, emplace() creates a new element directly inside a std::queue, at the back. You pass the arguments for the constructor and the queue builds the object itself.

push() needs a ready-made object, which may then be copied or moved. emplace() skips that step. It is handy for std::pair, std::string, and your own structs that take several values.

This function is a member function of std::queue<T>, declared in the <queue> header. It needs C++11 or newer (-std=c++11). You call it with a dot, for example q.emplace(...).

Syntax

Call emplace() with the same arguments you would give to the constructor of the element type.

C++
std::queue<std::pair<int, std::string>> q;

q.emplace(1, "login");     // builds pair(1, "login") at the back
q.emplace(2, "search");

std::string s = q.front().second;   // "login"
CallWhat it does
q.emplace(args...)Builds a new element at the back from args, passing them to the constructor of T.

Before C++17, emplace() returns void. Since C++17 it returns a reference to the new element, which is the same object that back() gives. You can use emplace() in place of push() almost anywhere. The speed gain shows for types that are costly to copy or move.

Library declarations
C++
template<class... Args>
void emplace(Args&&... args);            // C++11
template<class... Args>
reference emplace(Args&&... args);       // since C++17

// queue<T, Container> from <queue>
// It calls c.emplace_back(std::forward<Args>(args)...).

Parameters

ParameterDescription
args...Optional. Zero or more arguments that are forwarded to the constructor of the element type T. The new element is created from them at the back.

Return Value

Return ValueDescription
Nothing (before C++17)emplace() returns void in C++11 and C++14.
T& (C++17)Since C++17 it returns a reference to the newly created element at the back.

C++ Examples

Example 1 — Build Pairs in Place

Give the pair values straight to emplace(). You do not need to write std::make_pair or braces.

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

int main() {
    std::queue<std::pair<int, std::string>> q;
    q.emplace(1, "login");
    q.emplace(2, "search");

    std::cout << "Front: " << q.front().first << " " << q.front().second << '\n';
    std::cout << "Back: " << q.back().first << " " << q.back().second << '\n';
    std::cout << "Size: " << q.size() << '\n';
    return 0;
}

How It Works

  1. q.emplace(1, "login") passes 1 and "login" to the constructor of std::pair<int, std::string>. The pair is built right at the back of the queue.
  2. The second emplace() adds (2, "search") behind it. The first pair stays in front, so front() and back() are different pairs.
  3. .first and .second read the two parts of each pair. The size is 2, one entry for each call.

Example 2 — Count the Copies: push() vs emplace()

A small struct that prints a message each time it is built or moved shows the difference clearly.

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

struct Item {
    int id;
    Item(int i) : id(i) { std::cout << "construct " << id << '\n'; }
    Item(const Item& o) : id(o.id) { std::cout << "copy " << id << '\n'; }
    Item(Item&& o) noexcept : id(o.id) { std::cout << "move " << id << '\n'; }
};

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

    std::cout << "push:\n";
    q.push(Item(1));

    std::cout << "emplace:\n";
    q.emplace(2);
    return 0;
}

How It Works

  1. push(Item(1)) first builds a temporary Item (construct 1). The queue then moves that temporary into its storage (move 1).
  2. emplace(2) passes only the number 2. The queue calls the constructor directly on its own storage, so there is just one line: construct 2.
  3. No temporary object and no move happen in the second case. For big objects, this saved step can make a program faster.

Example 3 — Use the Reference Returned by emplace() (C++17)

Since C++17 you can keep the reference that emplace() gives back and change the new element at once.

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

int main() {
    std::queue<std::string> q;
    q.push("ab");

    std::string& r = q.emplace(3, 'x');
    std::cout << "Back: " << q.back() << '\n';

    r += "!";
    std::cout << "Back after edit: " << q.back() << '\n';
    std::cout << "Size: " << q.size() << '\n';
    return 0;
}

How It Works

  1. q.emplace(3, 'x') forwards 3 and 'x' to the std::string constructor that repeats a character. It builds "xxx" at the back.
  2. The call returns a reference to that new string, and r keeps it. This part needs C++17. In C++11 the return type is void, so use back() instead.
  3. r += "!" edits the element inside the queue, so back() now shows xxx!. The queue still holds two strings.

Frequently Asked Questions

It builds a new element in place at the back of the queue from the constructor arguments you give. It was added in C++11.
push() takes an object that already exists and copies or moves it in. emplace() takes the constructor arguments and builds the object inside the queue, which can save a copy or move.
C++11 or newer. Since C++17 the function also returns a reference to the new element. Before that it returns void.
At the back of the queue, just like push(). The element will leave after all older elements, so the FIFO order is not changed.
Not always. For int, double, and other simple types there is no difference. Use emplace() for pairs, strings, and structs that take several constructor arguments.

Next: empty()

Check if the queue has no elements. It returns true or false with std::queue.

empty() 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