std::stack::emplace(3)
๐ NAME ๐
Okay so bestie, welcome to std::stack::emplace(3) โ straight out of the C++ Standard Libary manual. The name is so iconic it said it twice: std::stack::emplace - std::stack::emplace. ๐
โจ Synopsis โจ
Here's the signature timeline, and yes the since / until labels are doing main character energy:
template< class... Args > (since C++11)
void emplace( Args&&... args ); (until C++17)
template< class... Args > (since C++17)
decltype(auto) emplace( Args&&... args );
So what does it do? Pushes a new element on top of the stack. The element is constructed in-place, i.e. no copy or move operations are performed. The constructor of the element is called with exactly the same arguments as supplied to the function.
Effectively calls c.emplace_back(std::forward<Args>(args)...);. ๐ซถ
๐ง Parameters ๐ง
argsโ arguments to forward to the constructor of the element.
๐ Return value ๐
- Until C++17:
(none). - Since C++17: The value or reference, if any, returned by the above call to
Container::emplace_back.
๐ฆ Complexity ๐ฆ
Identical to the complexity of Container::emplace_back. No extra mystery math, just vibes. ๐งฎโจ
๐ธ Example ๐ธ
Run this code, bestie:
// Run this code
#include <iostream>
#include <stack>
struct S
{
int id;
S(int i, double d, std::string s) : id{i}
{
std::cout << "S::S(" << i << ", " << d << ", \"" << s <<
"\");\n";
}
};
int main()
{
std::stack<S> stack;
const S& s = stack.emplace(42, 3.14, "C++"); // for return value
C++17 required
std::cout << "id = " << s.id << '\n';
}
๐จ๏ธ Output ๐จ๏ธ
S::S(42, 3.14, "C++")
id = 42
๐ Defect reports ๐
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 2783โ applied to C++17. Behavior as published:emplacereturned reference, breaking compatibility with pre-C++17 containers. Correct behavior: returnsdecltype(auto). Periodt.
๐ See also ๐
pushโ inserts element at the top (public member function).popโ removes the top element (public member function).
Okay that's the whole page, bestie. Mwah ๐ โ this vibe was served from http://cppreference.com, dated 2024.06.10, for std::stack::emplace(3). ๐ซถ
๐ฆ 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