std::make_optional(3)

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

πŸ’– 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 πŸ‘‘

  1. Creates an optional object from value. Effectively calls std::optional<std::decay_t<T>>(std::forward<T>(value)). lowkey just wraps your value, no cap πŸ’…
  2. Creates an optional object constructed in-place from args.... Equivalent to return std::optional<T>(std::in_place, std::forward<Args>(args)...);. in-place construction, we love to see it 🫢
  3. Creates an optional object constructed in-place from il and args.... Equivalent to return std::optional<T>(std::in_place, il, std::forward<Args>(args)...);. initializer_list girlies rise 🌸

🌈 PARAMETERS 🌈

  • value β€” the value you're constructing the optional object with, like, the whole point πŸ’–
  • il, args β€” arguments that get passed straight to the constructor of T

πŸŽ€ 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 optional object (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