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

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

Definition

In C++11, emplace() adds a new element to a std::stack by building it on top. You pass the constructor arguments, not a finished object, and the stack creates the element itself.

This is handy for pairs, tuples, and structs. Instead of st.push(std::make_pair(3, "low")) you write st.emplace(3, "low"). The new element becomes the top, just like push().

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

Syntax

Give emplace() the arguments for the constructor of the element type. It needs C++11 or newer.

C++
std::stack<std::pair<int, std::string>> st;

st.emplace(2, "write");     // builds pair<int, string>(2, "write")
st.emplace(8, "deploy");

auto newest = st.top();     // {8, "deploy"}
CallWhat it does
st.emplace(args...)Builds a new element from args on top of the stack.

emplace() returns nothing. The arguments must match a constructor of the element type. For simple types such as int, push() and emplace() behave the same.

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

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

Parameters

ParameterDescription
args...Zero or more arguments that are passed to the constructor of the element type T. The stack builds the new top element from them.

Return Value

Return ValueDescription
Nothingemplace() returns void.

C++ Examples

Example 1 — Emplace Pairs Without make_pair

Pass both values straight to emplace(). The newest pair sits on top.

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

int main() {
    std::stack<std::pair<int, std::string>> st;

    st.emplace(2, "write");
    st.emplace(8, "deploy");
    st.emplace(5, "review");

    while (!st.empty()) {
        std::cout << st.top().first << " " << st.top().second << '\n';
        st.pop();
    }
    return 0;
}

How It Works

  1. emplace(2, "write") passes 2 and "write" to the pair constructor. No temporary std::make_pair is needed.
  2. Each call places a new pair on top. The last emplace, {5, "review"}, is the top when the loop starts.
  3. The loop prints in reverse emplace order: review, deploy, write. A priority queue would have sorted by the number instead.

Example 2 — Emplace a Struct with a Constructor

When a struct has a constructor, emplace() calls it for you on top of the stack.

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

struct Frame {
    std::string name;
    int line;

    Frame(std::string n, int l) : name(std::move(n)), line(l) {}
};

int main() {
    std::stack<Frame> call;

    call.emplace("main", 1);
    call.emplace("parse", 12);
    call.emplace("eval", 40);

    std::cout << "Top frame: " << call.top().name
              << " (line " << call.top().line << ")\n";
    std::cout << "Frames: " << call.size() << '\n';
    return 0;
}

How It Works

  1. emplace("main", 1) calls Frame("main", 1) inside the stack. The code does not write Frame three times.
  2. Each new frame lands on top, like a call stack. The newest frame is eval.
  3. The output shows eval as the top frame. All three frames are still waiting, so size() prints 3.

Example 3 — Emplace Undo Steps as Pairs

Store a step id and a label. Emplace keeps the constructor call short.

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

int main() {
    using Step = std::pair<int, std::string>;
    std::stack<Step> undo;

    undo.emplace(1, "insert");
    undo.emplace(2, "replace");
    undo.emplace(3, "delete");

    while (!undo.empty()) {
        std::cout << "undo #" << undo.top().first
                  << ": " << undo.top().second << '\n';
        undo.pop();
    }
    return 0;
}

How It Works

  1. using Step = ... gives the pair a short name. Each emplace() builds one step on top.
  2. The newest step is number 3. That is the first one the loop undoes.
  3. Popping continues until the stack is empty. The steps come out newest first, which matches undo behavior.

Frequently Asked Questions

It builds a new element on top of the stack from constructor arguments. The new element becomes the top.
push() takes a finished object. emplace() takes the constructor arguments and builds the object in place, which can save a copy or a move. For plain numbers they behave the same.
emplace() was added in C++11. Every modern compiler supports it. Compile with -std=c++11 or newer.
No. For stack it returns void. You read the top element later with top().
No. A stack is LIFO. The newest emplaced element is always on top, regardless of its value.

Next: empty()

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

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.

7 people found this page helpful