Home C++ Lists Functions emplace_back() C++ std::list emplace_back() Function Definition In C++11 and later, std::list::emplace_back() builds a new element directly at the end of a std::list. You pass the constructor arguments, and the list makes the object inside its new node.
It is the in-place version of push_back(). Since C++17 it also returns a reference to the new element. Include <list> to use it.
This is a member of std::list<T>, a doubly linked list . Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax Call emplace_back with the arguments for the element constructor.
l.emplace_back(1, 2); // Point(1, 2)
l.emplace_back(3, 'a'); // std::string(3, 'a') = "aaa"
auto& r = l.emplace_back(10); // C++17: reference to the new element Call What it does l.emplace_back(args...)Builds T(args...) as the new last element. auto& r = l.emplace_back(args...)In C++17 and later, the returned reference lets you use the new element at once.
For plain numbers, push_back(5) and emplace_back(5) do the same. The gain comes with classes that have constructors with several arguments.
Library declarations template<class... Args>
void emplace_back(Args&&... args); // C++11
template<class... Args>
reference emplace_back(Args&&... args); // since C++17 Parameters Parameter Description args...The arguments that are forwarded to the constructor of the element type T. They must match one of its constructors.
Return Value Return Value Description reference (C++17)A reference to the new last element. Before C++17 the function returns void, so use back() instead.
Important
The arguments are passed to a constructor, so write emplace_back(1, 2), not emplace_back({1, 2}). Existing iterators and references stay valid after the call.
C++ Examples 1. Build a struct 2. Build strings 3. Returned reference (C++17) Example 1 — emplace_back() with Your Own Class The two arguments go straight to the constructor of Point. No Point object is made first.
#include <iostream>
#include <list>
struct Point {
int x, y;
Point(int a, int b) : x(a), y(b) {}
};
int main() {
std::list<Point> l;
l.emplace_back(1, 2);
l.emplace_back(3, 4);
for (const Point& p : l) {
std::cout << "(" << p.x << ", " << p.y << ")\n";
}
return 0;
} How It Works Point has a constructor that takes two ints, a and b.emplace_back(1, 2) forwards 1 and 2 to that constructor, and the new Point is built inside the last node.With push_back() you would write push_back(Point(1, 2)). The result is the same, but emplace_back() is shorter. Example 2 — emplace_back() Strings from Arguments One call uses a text literal, another uses a count and a character.
#include <iostream>
#include <list>
#include <string>
int main() {
std::list<std::string> l;
l.emplace_back("hello");
l.emplace_back(3, 'a');
for (const std::string& s : l) {
std::cout << s << ' ';
}
std::cout << '\n';
return 0;
} How It Works emplace_back("hello") builds a std::string from the text literal.emplace_back(3, 'a') calls std::string(3, 'a'), which makes "aaa".Both strings are linked in the order of the calls, so the output is hello aaa. Example 3 — emplace_back() Returns a Reference In C++17 you can change the new element right away, without calling back().
#include <iostream>
#include <list>
int main() {
std::list<int> l;
int& r = l.emplace_back(10);
r += 5;
std::cout << "back: " << l.back() << '\n';
std::cout << "size: " << l.size() << '\n';
return 0;
} How It Works emplace_back(10) adds a node with 10 and returns int&, a reference to that element. This needs C++17.r += 5 changes the element inside the list, because r is not a copy.So back() reads 15, and the list still has just one element. Frequently Asked Questions What does std::list::emplace_back() do? It builds a new element at the end of the list from the arguments you pass to the constructor. No temporary object is needed.
What is the difference between push_back() and emplace_back()? push_back() takes a finished object and copies or moves it. emplace_back() takes constructor arguments and builds the object in place.
What does emplace_back() return? Since C++17 it returns a reference to the new last element. In C++11 and C++14 it returns void.
Does emplace_back() invalidate iterators? No. Existing iterators, pointers, and references to other elements of the list stay valid.
Should I always use emplace_back() instead of push_back()? Not always. For simple values both are the same. Use emplace_back() when the element has a constructor with several arguments or is costly to copy.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion