C++ std::list emplace_front() Function

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

Definition

In C++11 and later, std::list::emplace_front() builds a new element directly at the start of a std::list. You pass the constructor arguments, and the object is made inside the new first node.

It is the in-place version of push_front(). Since C++17 it 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_front with the arguments for the element constructor.

C++
l.emplace_front("Ann", 30);   // std::pair<std::string, int>
l.emplace_front(3, '-');      // std::string(3, '-') = "---"
auto& r = l.emplace_front(7); // C++17: reference to the new element
CallWhat it does
l.emplace_front(args...)Builds T(args...) as the new first element.
auto& r = l.emplace_front(args...)In C++17 and later, the returned reference lets you use the new element at once.

Each new element goes in front of the older ones. The last call to emplace_front() makes the first element of the list.

Library declarations
C++
template<class... Args>
void emplace_front(Args&&... args);        // C++11

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

Parameters

ParameterDescription
args...The arguments that are forwarded to the constructor of the element type T.

Return Value

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

C++ Examples

Example 1 — emplace_front() a std::pair

Add two people to the front of a list. The newest one ends up first.

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

int main() {
    std::list<std::pair<std::string, int>> people;

    people.emplace_front("Ann", 30);
    people.emplace_front("Bob", 25);

    for (const auto& p : people) {
        std::cout << p.first << " " << p.second << '\n';
    }
    return 0;
}

How It Works

  1. The first call builds the pair ("Ann", 30) straight inside the list.
  2. The second call builds ("Bob", 25) and links it before Ann, because it is the front.
  3. The loop starts at the first node, so Bob is printed before Ann.

Example 2 — emplace_front() a String from Arguments

Use a count and a character to put a row of dashes in front of a word.

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

int main() {
    std::list<std::string> l = {"end"};

    l.emplace_front(3, '-');

    for (const std::string& s : l) {
        std::cout << s << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The list holds one string, "end".
  2. emplace_front(3, '-') calls std::string(3, '-') and builds "---" in a new first node.
  3. The loop prints the new first element and then the old one, giving --- end.

Example 3 — emplace_front() Returns a Reference

In C++17 you can change the new first element right away.

C++
#include <iostream>
#include <list>

int main() {
    std::list<int> l = {8};

    int& r = l.emplace_front(7);
    r *= 2;

    for (int n : l) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. emplace_front(7) adds a new first node holding 7 and returns a reference to it. This needs C++17.
  2. r *= 2 doubles the value inside the list, so the first element becomes 14.
  3. The old element 8 was not touched, so the loop prints 14 8.

Frequently Asked Questions

It builds a new element at the start of the list from the arguments you pass to the constructor. No temporary object is needed.
push_front() takes a finished object and copies or moves it. emplace_front() takes constructor arguments and builds the object in place.
Since C++17 it returns a reference to the new first element. In C++11 and C++14 it returns void.
No. A vector would have to shift all elements to add at the front. std::list and std::deque both have emplace_front().
No. Existing iterators, pointers, and references to other elements of the list stay valid.

Next: empty()

Check whether the list has no elements with std::list.

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.

9 people found this page helpful