C++ std::list emplace_back() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::list

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.

C++
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
CallWhat 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
C++
template<class... Args>
void emplace_back(Args&&... args);        // C++11

template<class... Args>
reference emplace_back(Args&&... args);   // since C++17

Parameters

ParameterDescription
args...The arguments that are forwarded to the constructor of the element type T. They must match one of its constructors.

Return Value

Return ValueDescription
reference (C++17)A reference to the new last element. Before C++17 the function returns void, so use back() instead.

C++ Examples

Example 1 — emplace_back() with Your Own Class

The two arguments go straight to the constructor of Point. No Point object is made first.

C++
#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

  1. Point has a constructor that takes two ints, a and b.
  2. emplace_back(1, 2) forwards 1 and 2 to that constructor, and the new Point is built inside the last node.
  3. 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.

C++
#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

  1. emplace_back("hello") builds a std::string from the text literal.
  2. emplace_back(3, 'a') calls std::string(3, 'a'), which makes "aaa".
  3. 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().

C++
#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

  1. emplace_back(10) adds a node with 10 and returns int&, a reference to that element. This needs C++17.
  2. r += 5 changes the element inside the list, because r is not a copy.
  3. So back() reads 15, and the list still has just one element.

Frequently Asked Questions

It builds a new element at the end of the list from the arguments you pass to the constructor. No temporary object is needed.
push_back() takes a finished object and copies or moves it. emplace_back() takes constructor arguments and builds the object in place.
Since C++17 it returns a reference to the new last element. In C++11 and C++14 it returns void.
No. Existing iterators, pointers, and references to other elements of the list stay valid.
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.

Next: emplace_front()

Build a new first element in place (C++11) with std::list.

emplace_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.

8 people found this page helpful