std::list::emplace(3)
π std::list::emplace(3) π
okay so bestie, welcome to the std::list::emplace(3) page from the C++ Standard Libary manual β¨ (yes, thatβs the manual name, spelling and all β weβre not doing a vibe check on the source, periodt). This is the page for the list member that emplaces stuff. Letβs get into it π«Ά.
π NAME π
std::list::emplace β std::list::emplace.
Thatβs it. Thatβs the name. No cap, itβs literally just the same thing twice π .
β¨ Synopsis β¨
Available since C++11 π¦:
template< class... Args >
iterator emplace( const_iterator pos, Args&&... args );
Okay so what does it do? It inserts a new element into the container directly before pos π.
The element is constructed through std::allocator_traits::construct, which uses placement-new to construct the element in-place at a location provided by the container. The arguments args... are forwarded to the constructor as std::forward<Args>(args).... And bestie, args... may directly or indirectly refer to a value in the container β eye contact, this is important π.
No iterators or references are invalidated. Not me being obsessed with a function that doesnβt wreck your iterators πβ¨.
πΈ Parameters πΈ
posβ iterator before which the new element will be constructed.argsβ arguments to forward to the constructor of the element.
π¦ Type requirements π¦
T(the containerβs element type) must meet the requirements ofEmplaceConstructible.
π Return value π
Iterator pointing to the emplaced element. Thatβs the whole return value, iconic behavior π .
β Complexity β
Constant. Thatβs it. Constant. Slay.
π Exceptions π
If an exception is thrown (e.g. by the constructor), the container is left unmodified, as if this function was never called β thatβs the strong exception guarantee, bestie. So like, no half-built mess left behind, fr fr π«Ά.
π Example π
Run this code:
// Run this code
#include <iostream>
#include <string>
#include <list>
struct A
{
std::string s;
A(std::string str) : s(std::move(str)) { std::cout << " constructed\n"; }
A(const A& o) : s(o.s) { std::cout << " copy constructed\n"; }
A(A&& o) : s(std::move(o.s)) { std::cout << " move constructed\n"; }
A& operator=(const A& other)
{
s = other.s;
std::cout << " copy assigned\n";
return *this;
}
A& operator=(A&& other)
{
s = std::move(other.s);
std::cout << " move assigned\n";
return *this;
}
};
int main()
{
std::list<A> container;
std::cout << "construct 2 times A:\n";
A two { "two" };
A three { "three" };
std::cout << "emplace:\n";
container.emplace(container.end(), "one");
std::cout << "emplace with A&:\n";
container.emplace(container.end(), two);
std::cout << "emplace with A&&:\n";
container.emplace(container.end(), std::move(three));
std::cout << "content:\n";
for (const auto& obj : container)
std::cout << ' ' << obj.s;
std::cout << '\n';
}
π Output π
construct 2 times A:
constructed
constructed
emplace:
constructed
emplace with A&:
copy constructed
emplace with A&&:
move constructed
content:
one two three
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR
LWG 2164β applied to C++11; behavior as published: it was not clear whether the arguments can refer to the container; correct behavior: clarified.
π See also π
insertβ inserts elements (public member function).emplace_backβ constructs an element in-place at the end (C++11) (public member function).
kisses π this page was served from http://cppreference.com, dated 2024.06.10, and itβs literally std::list::emplace(3) β bye bestie, go emplace something cute ππ¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms