std::queue::emplace(3)
π
std::queue::emplace(3) π
okay bestie, this page is from the C++ Standard Libary manual β yes, that spelling, keep it π β and itβs all about making a queue element appear in place like magic β¨
π NAME π
std::queue::emplace β std::queue::emplace
π¦ Synopsis π¦
template< class... Args > (since C++11)
void emplace( Args&&... args ); (until C++17)
template< class... Args > (since C++17)
decltype(auto) emplace( Args&&... args );
Pushes a new element to the end of the queue. 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.
It 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.
π Example π
// Run this code
#include <iostream>
#include <queue>
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::queue<S> queue;
const S& s = queue.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.
- DR
LWG 2783β applied to C++17. Behavior as published:emplacereturned reference, breaking compatibility with pre-C++17 containers. Correct behavior: returnsdecltype(auto).
π«Ά See also π«Ά
pushβ inserts element at the end (public member function)popβ removes the first element (public member function)
mwah π β source: http://cppreference.com, version/date: 2024.06.10, page: std::queue::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