std::queue::emplace(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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: emplace returned reference, breaking compatibility with pre-C++17 containers. Correct behavior: returns decltype(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