std::deque::emplace(3)
π NAME π
std::deque::emplace β std::deque::emplace
Okay so this is the std::deque::emplace(3) page from the C++ Standard Libary manual, and it is literally all about std::deque::emplace. Iconic. π
πΈ Synopsis πΈ
Since C++11, bestie:
template< class... Args > (since C++11)
iterator emplace( const_iterator pos, Args&&... args );
So what does it do? It inserts a new element into the container directly before pos. No cap. β¨
The element is constructed through std::allocator_traits::construct, which typically uses placement-new to construct the element in-place at a location provided by the container. However, if the required location has been occupied by an existing element, the inserted element is constructed at another location at first, and then move assigned into the required location.
The arguments args... are forwarded to the constructor as std::forward<Args>(args).... args... may directly or indirectly refer to a value in the container.
Bestie, listen: all iterators (including theend()iterator) are invalidated. References are invalidated too, unlesspos == begin()orpos == end(), in which case they are not invalidated. Thatβs the whole rule, periodt. π
π 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 of MoveAssignable, MoveInsertable and EmplaceConstructible.
β Return value β
Iterator pointing to the emplaced element. Thatβs it. Slay.
π₯ Complexity π₯
Linear in the lesser of the distances between pos and either of the ends of the container.
π Exceptions π
If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move assignment operator of the value type, or if an exception is thrown while emplace is used to insert a single element at the either end, there are no effects (strong exception guarantee).
Otherwise, the effects are unspecified. So likeβ¦ stay aware, bestie. π
π Example π
// Run this code
#include <iostream>
#include <string>
#include <deque>
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::deque<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.
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(C++11) β constructs an element in-place at the end (public member function).
Source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::deque::emplace(3) β bye bestie, slay π
π¦ 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