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"
Call
What 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
Parameter
Description
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 Value
Description
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.
q.emplace(3, 'x') forwards 3 and 'x' to the std::string constructor that repeats a character. It builds "xxx" at the back.
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.
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.