std::optional::emplace(3)
π¦ std::optional::emplace(3) π¦
okay bestie, welcome to the C++ Standard Libary manual page for std::optional::emplace β yes, βLibaryβ is literally how itβs spelled up top and weβre keeping that energy. π
πΈ NAME πΈ
std::optional::emplaceβstd::optional::emplace, bestie. Thatβs the name. No cap.
β¨ Synopsis β¨
Two overloads, both with that since C++17 energy and constexpr since C++20 glow-up:
template< class... Args > (1) (since C++17)
T& emplace( Args&&... args );
(constexpr since C++20)
template< class U, class... Args > (since C++17)
T& emplace( std::initializer_list<U> ilist, Args&&... (2)
args );
(constexpr since C++20)
So whatβs the vibe? It constructs the contained value in-place. If *this already contains a value before the call, that contained value is destroyed by calling its destructor. π
- Initializes the contained value by direct-initializing (but not direct-list-initializing) with
std::forward<Args>(args)...as parameters. - Initializes the contained value by calling its constructor with
ilist,std::forward<Args>(args)...as parameters. This overload participates in overload resolution only ifstd::is_constructible<T, std::initializer_list<U>&, Args&&...>::valueis true.
π οΈ Parameters π οΈ
args...β the arguments to pass to the constructor.ilistβ the initializer list to pass to the constructor.
π§ Type requirements π§
Tmust be constructible fromArgs...for overload (1).Tmust be constructible fromstd::initializer_listandArgs...for overload (2).
π Return value π
A reference to the new contained value. Slay.
π Exceptions π
Any exception thrown by the selected constructor of T. If an exception is thrown, *this does not contain a value after this call (the previously contained value, if any, had been destroyed). So yeah, bestie β if it throws, the old value is gone and thereβs no value in there afterward. π«
π§ͺ Example π§ͺ
// Run this code
#include <iostream>
#include <optional>
struct A
{
std::string s;
A(std::string str) : s(std::move(str)), id{n++} { note("+ constructed"); }
~A() { note("~ destructed"); }
A(const A& o) : s(o.s), id{n++} { note("+ copy constructed"); }
A(A&& o) : s(std::move(o.s)), id{n++} { note("+ move constructed"); }
A& operator=(const A& other)
{
s = other.s;
note("= copy assigned");
return *this;
}
A& operator=(A&& other)
{
s = std::move(other.s);
note("= move assigned");
return *this;
}
inline static int n{};
int id{};
void note(auto s) { std::cout << " " << s << " #" << id << '\n'; }
};
int main()
{
std::optional<A> opt;
std::cout << "Assign:\n";
opt = A("Lorem ipsum dolor sit amet, consectetur adipiscing elit nec.");
std::cout << "Emplace:\n";
// As opt contains a value it will also destroy that value
opt.emplace("Lorem ipsum dolor sit amet, consectetur efficitur.");
std::cout << "End example\n";
}
π€ Output
Assign:
+ constructed #0
+ move constructed #1
~ destructed #0
Emplace:
~ destructed #1
+ constructed #2
End example
~ destructed #2
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. Periodt.
- P2231R1 β applied to C++20. Behavior as published:
emplacewas notconstexprwhile the required operations can beconstexprin C++20. Correct behavior: madeconstexpr.
π See also π
operator=β assigns contents (public member function).
okay bye bestie π β source: http://cppreference.com; version/date: 2024.06.10; page: std::optional::emplace(3). mwah πβ¨
π¦ 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