std::make_optional(3)
π std::make_optional β NAME π
okay so bestie, this is the std::make_optional(3) page from the C++ Standard Library manual, and it's literally just: std::make_optional β that's the name, that's the whole name section, we move π
β¨ SYNOPSIS β¨
first things first, you gotta know where this lives β it's defined in the header <optional> π¦
template< class T >
constexpr std::optional<std::decay_t<T>> make_optional( T&& value );
(1) (since C++17)
template< class T, class... Args >
constexpr std::optional<T> make_optional( Args&&... args );
(2) (since C++17)
template< class T, class U, class... Args >
constexpr std::optional<T> make_optional( std::initializer_list<U> il,
Args&&... args );
(3) (since C++17)
so there's three flavors, and here's the tea on each π
- Creates an
optionalobject fromvalue. Effectively callsstd::optional<std::decay_t<T>>(std::forward<T>(value)). lowkey just wraps your value, no cap π - Creates an
optionalobject constructed in-place fromargs.... Equivalent toreturn std::optional<T>(std::in_place, std::forward<Args>(args)...);. in-place construction, we love to see it π«Ά - Creates an
optionalobject constructed in-place fromilandargs.... Equivalent toreturn std::optional<T>(std::in_place, il, std::forward<Args>(args)...);. initializer_list girlies rise πΈ
π PARAMETERS π
valueβ the value you're constructing theoptionalobject with, like, the whole point πil,argsβ arguments that get passed straight to the constructor ofT
π RETURN VALUE π
The constructed optional object. that's it, that's the return value, she's built β¨
π EXCEPTIONS π
Throws any exception thrown by the constructor of T. if T's constructor is dramatic, make_optional is gonna be dramatic right along with it π
π§ NOTES π§
this is the iconic part: T need not be movable for overloads (2,3), thanks to guaranteed copy elision. not me being obsessed with copy elision fr fr π
π₯ EXAMPLE π₯
// Run this code
#include <iomanip>
#include <iostream>
#include <optional>
#include <string>
#include <vector>
int main()
{
auto op1 = std::make_optional<std::vector<char>>({'a','b','c'});
std::cout << "op1: ";
for (char c : op1.value())
std::cout << c << ',';
auto op2 = std::make_optional<std::vector<int>>(5, 2);
std::cout << "\nop2: ";
for (int i : *op2)
std::cout << i << ',';
std::string str{"hello world"};
auto op3 = std::make_optional<std::string>(std::move(str));
std::cout << "\nop3: " << std::quoted(op3.value_or("empty value")) << '\n';
std::cout << "str: " << std::quoted(str) << '\n';
}
β Possible output β
op1: a,b,c,
op2: 2,2,2,2,2,
op3: "hello world"
str: ""
(and yes, str is empty at the end β that's the std::move doing its thing, she gave everything she had π)
π SEE ALSO π
- constructor β constructs the
optionalobject (public member function)
that's the whole page bestie, go forth and wrap things in optional π
β¨
sign-off time π β this page is brought to you by cppreference.com, dated 2024.06.10, and it's the std::make_optional(3) page, 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